Delicious Apples
Time limit:5000/3000 MS (java/others) Memory limit:524288/524288 K (java/others)
Total submission (s): 1057 Accepted Submission (s): 354
problem DescriptionThere isNApple trees planted along a cyclic road, which isLmetres long. Your Storehouse is built at position0On that cyclic road.
TheITh tree is planted at positionxi , clockwise from position0. There isai Delicious Apple (s) on theITh tree.
You are only having a basket which can contain at the mostKApple (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?
1≤N,K≤105,AI≥1, a1 + A2+ + An≤105
1≤L≤9
0≤x[i]≤L
There is less than-huge testcases, and less than, small testcases.
InputFirst line:TThe number of testcases.
ThenTTestcases 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 Input210 3 22 28 25 110 4 12 28 25 10 10000
Sample Output1826
SourceMulti-university Training Contest 2 problem solving: The core idea is greed, the key is to enumerate the last remaining K apples. First divides the circle into two halves, uses sum[i] to calculate to I minimum need many long road why is sum[i] = Sum[i-k] + l[i]? This is actually a backward push! It is clear that the last of the ten may be from the beginning of the shortage of k, why not push from the front? Obviously, whether it is pushed from the front or behind the push, it contains a k length of the number of intervals is certain, but from the back to get the benefit of will be less! It embodies the greedy thought in the legend. Obviously pushed from behind, the value of the front part is less finally we need to enumerate the K-length intervals, spanning two semicircular ll ret = (Sum[lsize][0] + sum[rsize][1]) <<1; This sentence means purely using the shortest path of all apples without wrapping round the way , yes shortest, because the front greedy.
int a = Lsize-i; What does that mean? Indicates that I is selected from the left semicircle so that the remainder does not cross the circle, and continues using the return strategy of taking the original path
int b = Max (0,rsize-(k-i)); The rest of the k-i a strategy of taking the original path back.
ret = min (ret, ((Sum[a][0] + sum[b][1]) <<1) + Len); So this k, we used to take the way back, using a round method
So much, Len.
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 Const intMAXN =100010;5LL d[maxn],tot,sum[maxn][2];6Vector<ll>L,r;7 intMain () {8 intKase,n,k,m,pos;9 LL Len;Tenscanf"%d",&Kase); One while(kase--) { Ascanf"%d%d%d",&pos,&n,&K); - l.clear (); - r.clear (); theLen =Pos; - for(inti = tot =0; I < n; ++i) { -scanf"%d%d",&pos,&m); - for(intj =0; J < M; ++j) +d[tot++] =Pos; - } + for(inti =0; i < tot; ++i) { A if(d[i]*2<Len) L.push_back (D[i]); at ElseR.push_back (Len-d[i]); - } - sort (L.begin (), L.end ()); - sort (R.begin (), R.end ()); - intLsize = L.size (), rsize =r.size (); -sum[0][0] = sum[0][1] =0; in for(inti =0; i < lsize; ++i) - if(i +1<= K) Sum[i +1][0] =L[i]; to ElseSum[i +1][0] = Sum[i-k +1][0] +L[i]; + for(inti =0; i < rsize; ++i) - if(i +1<= K) sum[i+1][1] =R[i]; the Elsesum[i+1][1] = sum[i-k+1][1] +R[i]; *LL ret = (sum[lsize][0] + sum[rsize][1]) <<1; $ for(inti =0; I <= lsize && i <= K; ++i) {Panax Notoginseng intA = lsize-i; - intb = Max (0, Rsize-(K-i)); theret = min (ret, (sum[a][0] + sum[b][1]) <<1) +Len); + } Acout<<ret<<Endl; the } + return 0; -}View Code
Multi-university Training Contest 2 hdu 5303 Delicious Apples