33OJ
登录 注册

#P17877. 糖葫芦 / tanghulu

    ID: 20668 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高+/省选− 上传者: 标签>贪心线段树平衡树颜色段均摊(珂朵莉树 ODT)洛谷原创O2优化洛谷月赛均摊分析STL

糖葫芦 / tanghulu

Background

Tanghulu (candied hawthorn skewers) is one of the most representative traditional snacks of old Beijing. Sweet, sour and glistening, it is often seen at the lively winter temple fairs and in the hutong alleys.

The key to making authentic Beijing tanghulu lies in "boiling the sugar" and "coating with sugar". First, wash and pit the hawthorns and skewer them on bamboo sticks; then boil white sugar with water until it bubbles, thickens, and turns a clear caramel color. When the syrup is just right, quickly dip the whole skewer into the hot, thick syrup and give it a light turn so that it is evenly coated with a thin layer of sugar; then take it out and let it cool, forming a thin, crisp and translucent sugar shell.

Problem Description

Little M plans to learn to make some tanghulu. He has already prepared the fruit skewers and boiled the syrup; next he needs to do the sugar-coating step.

Little M has prepared nn pots of syrup, where the ii-th (1≤i≤n1 \le i \le n) pot initially contains aia_i units of syrup. To make a tanghulu of length ll, he must choose a pot whose current remaining amount is at least ll and dip the skewer into it; after it is made, the remaining amount of that pot decreases by 11 unit (if the remaining amount drops to 00, that pot is removed).

Little M needs to make mm batches of tanghulu in increasing order of batch number. The ii-th (1≤i≤m1\le i\le m) batch has cic_i skewers, each of length bib_i. The tanghulu must be made one by one, and the (i+1)(i+1)-th batch can only start after all tanghulu of the ii-th batch are finished. When making each skewer, Little M can independently choose from all current pots of syrup.

Before starting, and before or after making any skewer, Little M may perform any number of merge operations. Merging has a given loss constant kk: each merge always loses kk units of syrup. In each merge, Little M chooses two pots with current remaining amounts x,yx,y satisfying x+y>kx+y>k, merges them into a new pot with remaining amount x+y−kx+y-k, and removes the two original pots.

Since merging causes loss, Little M wants to merge as few times as possible. Find the minimum number of merges needed to finish making all the tanghulu, or report that it is impossible to finish no matter what.

Input Format

This problem contains multiple test cases.

The first line of the input contains two integers c,tc,t, the testcase number and the number of test cases, respectively. In particular, if c=0c=0, this testcase is a sample.

Then tt test cases follow. For each test case:

  • The first line contains three positive integers n,m,kn,m,k, the initial number of syrup pots, the number of batches of tanghulu, and the amount of syrup lost in each merge, respectively.
  • The second line contains nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n, the initial amounts of the syrup pots.
  • Each of the next mm lines, the ii-th line, contains two positive integers bi,cib_i,c_i, the minimum remaining amount of syrup required to make each skewer in the ii-th batch, and the number of skewers in this batch, respectively.

Output Format

For each test case, output one line containing a single integer, the minimum number of merges needed. If it is impossible to finish all the making, output −1-1.

0 3
5 3 2
2 3 3 5 8
4 2
7 2
8 2
1 1 1
3
2 3
2 2 2
3 3
3 2
2 2
3
-1
0

Hint

【Sample 1 Explanation】

For the first test case, one valid plan is as follows:

  • In the first batch, use the pot with initial amount 55 to make two skewers; its remaining amount becomes 33.
  • In the second batch, use the pot with initial amount 88 to make two skewers; its remaining amount becomes 66.

At this point, the remaining amounts of the pots are 2,3,3,3,62,3,3,3,6.

For the third batch, operate in the following order:

  1. Merge the two pots with remaining amounts 6,36,3, getting 77 units of syrup.
  2. Merge the two pots with remaining amounts 7,37,3, getting 88 units of syrup.
  3. Use this pot to make one skewer; its remaining amount becomes 77.
  4. Merge the two pots with remaining amounts 7,37,3, getting 88 units of syrup.
  5. Use this pot to make the last skewer.

There are 33 merges in total. It can be proved that no valid plan uses fewer than 33 merges.

For the second test case, after making two skewers, the only pot of syrup has remaining amount 11, so the third skewer cannot be made, and no merge is possible either.

For the third test case, in the first batch use two pots to make one skewer each; after that, both pots have 22 units of syrup left. In the second batch, use the two pots to make one skewer each, and all the making is finished without any merge.

【Sample 2】

See tanghulu/tanghulu2.in and tanghulu/tanghulu2.ans in the problem directory.

This sample satisfies the constraints of testcases 1,21,2.

【Sample 3】

See tanghulu/tanghulu3.in and tanghulu/tanghulu3.ans in the problem directory.

This sample satisfies the constraints of testcases 3,43,4.

【Sample 4】

See tanghulu/tanghulu4.in and tanghulu/tanghulu4.ans in the problem directory.

This sample satisfies the constraints of testcases 5∼75\sim 7.

【Sample 5】

See tanghulu/tanghulu5.in and tanghulu/tanghulu5.ans in the problem directory.

This sample satisfies the constraints of testcases 8∼108\sim 10.

【Sample 6】

See tanghulu/tanghulu6.in and tanghulu/tanghulu6.ans in the problem directory.

This sample satisfies the constraints of testcases 11∼1311\sim 13.

【Sample 7】

See tanghulu/tanghulu7.in and tanghulu/tanghulu7.ans in the problem directory.

This sample satisfies the constraints of testcase 1616.

【Constraints】

Let NN be the sum of nn over all test cases within a single testcase, and MM be the sum of mm over all test cases within a single testcase. For all testdata:

  • 0≤c≤200\le c\le 20, 1≤t≤1031\le t\le 10^3;
  • 1≤n,m≤1051\le n,m\le 10^5, N,M≤5×105N,M\le 5\times 10^5;
  • 1≤k≤10131\le k\le 10^{13};
  • For all 1≤i≤n1\le i\le n, 1≤ai≤10131\le a_i\le 10^{13};
  • For all 1≤i≤m1\le i\le m, 1≤bi,ci≤10181\le b_i,c_i\le 10^{18}.

The specific constraints of each testcase are shown in the table below.

::cute-table{tuack} |Testcase No.|n≤n\le|N≤N\le|m≤m\le|M≤M\le|Special Property| |:----:|:----:|:----:|:----:|:----:|:----:| |1,21,2|55|5050|55|5050|A| |3,43,4|11|10310^3|10510^5|5×1055\times10^5|None| |5∼75\sim7|10510^5|2×1052\times10^5|^|2×1052\times10^5|B| |8∼108\sim10|^|^|11|10310^3|None| |11∼1311\sim13|^|^|22|2 0002\,000|^| |14,1514,15|^|^|100100|10510^5|^| |1616|^|^|10510^5|2×1052\times10^5|A| |17,1817,18|^|^|^|^|None| |19,2019,20|^|5×1055\times10^5|^|5×1055\times10^5|^|

Special Property A: For all 1≤i≤m1\le i\le m, ci=1c_i=1.

Special Property B: For all 1≤i≤m1\le i\le m, bi>max⁡1≤j≤najb_i>\max_{1\le j\le n}a_j.

Translated by QwenWork