Test instructions: An n-tree with a circle of l, each tree coordinates XI, an AI apple, and a basket of size k to put all the apples back, asking at least how many roads to walk
Solution: By the Magic DP teaches a person (actually I compare water, originally thought to the left greedy, the right greedy a bit in the last turn a circle on the water problem =-=)
#include <stdio.h> #include <string.h> #include <algorithm> #define LL __int64#define MIN (b) ((a) < (b)? (a):(B)) const int maxn=100005;using namespace STD;LL dp[3][maxn];struct aaa{int x,a;} Tree[maxn];int CMP (AAA a,aaa b) {return a.x<b.x;} int main () {int t; int l,n,k; while (scanf ("%d", &t)!=-1) {while (t--) {scanf ("%d%d%d", &l,&n,&k); for (int i=0;i<n;i++) scanf ("%d%d", &tree[i].x,&tree[i].a); Sort (tree,tree+n,cmp); dp[0][0]=dp[1][0]=0; int fr,ed=1; for (int i=0;i<n;i++)//Clockwise each Apple takes the path of the walk {for (int j=0;j<tree[i].a;j++) {fr=ed-k>0?ed-k:0; dp[0][ed]=dp[0][fr]+ (2*tree[i].x>l?l:2*tree[i].x); ed++; }} ed=1; ll Sum=0; for (int i=n-1;i>=0;i--)//counter-clockwise eachThe Apple takes the road to walk {sum+=tree[i].a; for (int j=0;j<tree[i].a;j++) {fr=ed-k>0?ed-k:0; dp[1][ed]=dp[1][fr]+ (l-tree[i].x) >l?l:2* (l-tree[i].x)); ed++; }} ll lenth=1000000000; for (ll i=0;i<=sum;i++) lenth=min (Lenth,dp[0][i]+dp[1][sum-i]); Find the smallest printf ("%i64d\n", lenth); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5303 Delicious Apples (DP)