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 Test instructions: Give a circle, set the starting point to 0, give the circumference of L, in the circle there are n trees, in order to give the distance between clockwise and starting point, the number of apples on the tree. Give the basket size (up to the apple tree). Ask at least how far you can collect all the apples. Idea: Take the diameter of the beginning of the boundary, the apple tree in the left semicircle and the right semicircle respectively greedy, get to go distance. Finally, enumerate the apples (see the code) that were collected at the end of the whole lap. The minimum value is compared.
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;#defineMAXN 100050intD_l[maxn],d_r[maxn],l,n,k,t,x,a,l,r;Long Longtot_l[maxn],tot_r[maxn],ans,tmp;intMain () {scanf ("%d",&t); while(t--) {L=r=0; scanf ("%d%d%d",&l,&n,&k); for(intI=0; i<n;i++) {scanf ("%d%d",&x,&a); for(intj=0; j<a;j++){ if(x*2<L) d_l[++l]=x; Elsed_r[++r]=l-x; }} sort (d_l+1, d_l+l+1); Sort (D_r+1, d_r+r+1); for(intI=1; i<=l;i++){ if(i<=k) tot_l[i]=D_l[i]; Elsetot_l[i]=tot_l[i-k]+D_l[i]; } for(intI=1; i<=r;i++){ if(i<=k) tot_r[i]=D_r[i]; Elsetot_r[i]=tot_r[i-k]+D_r[i]; } ans= (Tot_l[l]+tot_r[r]) *2; for(intI=0; i<=k;i++) {tmp= (Tot_l[l-i]+tot_r[max (0, R (k-i))]) *2; Ans=min (ans,l+tmp); } printf ("%i64d\n", ans); } return 0;}
View Code
2015 Multi-school race second game 1004 Hdu (5303)