HDU 5303 Delicious Apples (Greedy enumeration), hdu5303
Delicious Apples
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission (s): 199 Accepted Submission (s): 54
Problem Description There are N Apple trees planted along a cyclic road, which is L Metres long. Your storehouse is built at position 0 On that cyclic road.
The I Th tree is planted at position X I , Clockwise from position 0 . There are Ai Delicious apple (s) on I Th tree.
You only have a basket which can contain at most K Apple (s). You are to start from your storehouse, pick all the apples and carry them back to your storehouse using your basket. What is your minimum distance traveled?
1 ≤ n, k ≤ 105, ai ≥ 1, a1 + a2 +... + an ≤ 105
1 ≤ L ≤ 109
0 ≤ x [I] ≤ L
There are less than 20 huge testcases, and less than 500 small testcases.
InputFirst line: T , The number of testcases.
Then T Testcases follow. In each testcase:
First line contains three integers, L, n, K .
Next N Lines, each line contains Xi, ai .
OutputOutput total distance in a line for each testcase.
Sample Input
210 3 22 28 25 110 4 12 28 25 10 10000
Sample Output
1826
Source2015 Multi-University Training Contest 2
Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5303
There is a ring with a length of L, n apple trees, and a basket containing a maximum of k apples. After the installation, you have to return to the starting point and unload the basket before starting, the clockwise location of n apple trees and the number of apples are given, and the minimum distance of All apples is obtained.
Question Analysis: Obviously, only under certain special conditions, that is, there are still apples on both sides, and the last apple can be installed at a time and far from the start. In this case, it may be better for us to directly circle, that is to say, the whole circle can be bypassed at most once, so we can first greedy for both sides, and the data on the question shows that the number of apples is at most 1e5, obviously, we can "discrete" the apple and use x [I] to record the position from the I apple to the start point. Then we can sort the position from small to large, and select a small distance first, at the time of selection, we used dis [I] to record the smallest distance of an apple on the side of the ticket. Similar to the principle of backpack counting, the answer should be multiplied by 2 because it is round-trip, finally, when k> = I, the number of apples in the entire circle is enumerated. szl-I indicates that only the number of apples on the left side is taken. szr-(k-I) indicates that only the apple trees on the right are taken, you can see it by drawing a picture. Note that the value on the right may be negative, and the value must be the same as 0, then the answer is (disl [szl-I] + disr [szr-(k-I)]) * 2 + L, which is more intuitive. The last step is to minimize the number of wa points. One is to use long, the other is to use negative numbers and 0, and the third is to reset each time.
# Include <cstdio> # include <cstring> # include <vector> # include <algorithm> # define ll long longusing namespace std; int const MAX = 1e5 + 5; int L, n, k; ll x [MAX], disl [MAX], disr [MAX]; vector <ll> l, r; int main () {int T; scanf ("% d", & T); while (T --) {memset (disl, 0, sizeof (disl); memset (disr, 0, sizeof (disr); l. clear (); r. clear (); scanf ("% d", & L, & n, & k); int cnt = 1; for (int I = 1; I <= n; I ++) {Ll pos, num; scanf ("% lld", & pos, & num); for (int j = 1; j <= num; j ++) x [cnt ++] = (ll) pos; // discrete operation} cnt --; for (int I = 1; I <= cnt; I ++) {if (2 * x [I] <L) l. push_back (x [I]); else r. push_back (L-x [I]); // record location} sort (l. begin (), l. end (); sort (r. begin (), r. end (); int szl = l. size (), szr = r. size (); for (int I = 0; I <szl; I ++) disl [I + 1] = (I + 1 <= k? L [I]: disl [I + 1-k] + l [I]); for (int I = 0; I <szr; I ++) disr [I + 1] = (I + 1 <= k? R [I]: disr [I + 1-k] + r [I]); ll ans = (disl [szl] + disr [szr]) * 2; for (int I = 0; I <= szl & I <= k; I ++) {int p1 = szl-I; int p2 = max (0, szr-(k-I); ans = min (ans, 2 * (disl [p1] + disr [p2]) + L );} printf ("% I64d \ n", ans );}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.