Test instructions
There are n apple trees on a ring of length L. Your basket capacity is K apples.
Every apple tree has a a[i apple.
Ask you how much distance you need to go from 0 to get all the apples.
Ideas:
We consider dp,dp[0][i] to represent the shortest distance of I apples clockwise.
Dp[1][i] represents the shortest distance I apples are counterclockwise.
So the apple is always the sum
So Ans=dp[0][i]+dp[sum-i] (0<=i<=sum)
Code:
#include "stdio.h" #include "algorithm" #include "string.h" #include "iostream" #include "queue" #include "map" #include " Vector "#include" string "#include" Cmath "using namespace std; #define LL __int64ll l;struct node{int x,s;} ap[123456];ll D P[2][123456];int CMP (node A,node b) {return a.x<b.x;} int main () {int t; cin>>t; while (t--) {int n,k; scanf ("%i64d%d%d", &l,&n,&k); for (int i=0; i<n; i++) scanf ("%d%d", &AP[I].X,&AP[I].S); Memset (Dp,0,sizeof (DP)); Sort (ap,ap+n,cmp); int tep=1,sum=0; TEP representative took a few apples because must be incremented for (int i=0; i<n; i++) {for (int j=0; j<ap[i].s; J + +) { if (tep-k<0) dp[0][tep]=dp[0][0]+min (l,2ll*ap[i].x); else Dp[0][tep]=dp[0][tep-k]+min (l,2ll*ap[i].x); tep++; }} tep=1; for (int i=n-1; i>=0; i--) {sum+=ap[i].s; for (int j=0; j<ap[i].s; j + +) {if (tep-k<0) dp[1][tep]=dp[1][0]+min (l,2ll* (l-ap[i].x)); Else Dp[1][tep]=dp[1][tep-k]+min (l,2ll* (l-ap[i].x)); tep++; }} ll Ans=999999999999999999ll; for (int i=0;i<=sum;i++) ans=min (Ans,dp[0][i]+dp[1][sum-i]); printf ("%i64d\n", ans); } return 0;}
[Multi-school 2015.02.1004 DP] HDU 5303 Delicious Apples