Delicious Apples
Time limit:5000/3000 MS (java/others) Memory limit:524288/524288 K (java/others)
Total Submission (s): 199 Accepted Submission (s):
problem DescriptionThere isApple trees planted along a cyclic road, which ismetres long. Your Storehouse is built at positionOn that cyclic road.
TheTh tree is planted at position, clockwise from position. There isDelicious Apple (s) on theTh tree.
You are only having a basket which can contain at the mostApple (s). You is to start from your storehouse, pick all the apples and carry them back to your storehouse using your basket. What is your minimum distance travelled?
There is less than-huge testcases, and less than, small testcases.
InputFirst line: the number of testcases.
then testcases follow. In each testcase:
First line contains three integers, .
Next lines, each line contains .
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
SourceMulti-university Training Contest 2
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5303
The main topic: there is a ring long as L. n Apple tree, a basket of up to pack K apples, loaded to go back to the beginning to unload and then set off. Give the n apple tree a clockwise position and the number of apples. To pick the smallest distance an apple can take
Topic Analysis: Obviously. Only under certain special conditions, that is, both sides have apples and can be loaded at once and the last Apple is far from the starting point. Such a case. It may be better to circle directly, that is to say, the circle is rounded at most. So we can be greedy on both sides first. The data on the topic shows that the number of apples is up to 1e5, and it is clear that we can "scatter" the apples, using x[i to record the location of the first Apple to the starting point. Then to the position from small to large, first select the distance is smaller. When selected, use Dis[i] to record the minimum distance of I apples, similar to the principle of backpack counting. The answer is to multiply by 2, because it is back and forth, and at the end of K>=i. Enumerating around the whole circle, Szl-i says that only the number of apples on the left is taken, SZR-(k-i) means only the apple tree on the right. You can see it by drawing a picture. Note that the right side here may be negative. and 0 to take the biggest, then the answer is (Disl[szl-i] + DISR[SZR-(k-i)]) * 2 +l, here in fact the drawing more intuitive. Finally take the smallest can, note that there are several WA points, one is to use a long long. Two is said before the occurrence of negative and 0 to take large, three is to clear 0 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 ma In () {int T; scanf ("%d", &t); while (t--) {memset (DISL, 0, sizeof (DISL)); memset (DISR, 0, sizeof (DISR)); L.clear (); R.clear (); scanf ("%d%d%d", &l, &n, &k); int cnt = 1; for (int i = 1; I <= n; i++) {ll pos, num; scanf ("%lld%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 position} 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); }}
HDU 5303 Delicious Apples (Greedy enumeration good question)