Test instructions: A ring length of l, with n trees above, baskets can be loaded with K apples at a time;
Given the location of each tree and the number of apples on the tree, the minimum total distance from which all apples are shipped back to the origin;
Thinking: The ring is divided into two halves, and if there is a circle around the case can only be once;
A single Apple is treated as an object;
Consider the case of non-winding: each half lap takes the farthest apple; Sum[i] indicates the cost of taking the first apple (distance);
Consider the situation around the circle: Enumerate two half laps left the case of K apples, and finally around a circle;
The minimum value in the circle and the circle is the evaluated value;
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intt,n,m,l,num1,num2,cnt;Long Longleft[500010],right[500010];Long Longsum1[500010],sum2[500010];Long Longapple[500010];Long Longans;intMain () {inti,j,k,a,b; scanf ("%d",&t); while(t--) {CNT=0; num1=num2=0; memset (Apple,0,sizeof(Apple)); memset (SUM1,0,sizeof(SUM1)); memset (sum2,0,sizeof(sum2)); scanf ("%d%d%d",&l,&n,&k); for(i=0; i<n;i++) {scanf ("%d%d",&a,&b); for(j=1; j<=b;j++) {apple[++cnt]=a;//the location of each Apple}} k=min (k,cnt); for(i=1; i<=cnt;i++){ if(apple[i]*2<=l) {left[++num1]=apple[i];//Left Half Circle } Else{right[++num2]=l-apple[i];//Right Half circle}} sort ( left+1, left+num1+1);//from small to largeSort (right+1, right+num2+1); for(i=1; i<=num1;i++){ if(i<=k) sum1[i]=left[i];//when less than K, take the farthest cost ElseSum1[i]=sum1[i-k]+left[i];//Take the farthest out first, and consider the remaining } for(i=1; i<=num2;i++){ if(i<=k) sum2[i]=Right[i]; Elsesum2[i]=sum2[i-k]+Right[i]; } ans=(Long Long) (Sum1[num1]+sum2[num2]) *2;//the cost of not going around the loop for(i=0; i<=num1&&i<=k;i++) {ans=min (ans, (Long Long) (L +2*sum1[num1-i]+2*sum2[num2-(K-i)]);//There's a total of K left left} printf ("%lld\n", ans); } return 0;}
HDU 5303 Delicious Apples (backpack)