Test instructions: Enter T, representing the T Group sample, enter N,len, indicating the number of items and the length of the container. Enter n line a,v to indicate the length and value of an item. Each item can be placed on a container only half of the time (in order to balance, if it is an object, no matter how long it can be placed on the container), can not overlap, the container can put the maximum value is how much.
Analysis: dp[i][j][k] means that the first I item in a container of length J has a maximum value of k items at the edge.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<cmath>#include<cstdlib>#include<map>using namespacestd;structstick{intA, V;} s[1005];Long Longdp[4005][3];intMain () {intT; scanf ("%d", &t); inttt =1; intN, Len; while(t--) {scanf ("%d%d", &n, &Len); Len*=2; Long LongAns =0; for(intI=1; i<=n; i++) {scanf ("%d%d", &S[I].A, &s[i].v); S[I].A*=2; Ans= Max (ans, (Long Long) S[I].V); } memset (DP,0,sizeof(DP)); for(intI=1; i<=n; i++) { for(intJ=len; j>=s[i].a/2; j--) { for(intk=0; k<3; k++) { if(j>=s[i].a) Dp[j][k]= Max (Dp[j][k], dp[j-s[i].a][k]+s[i].v);//General Conditionsif(k>0) Dp[j][k]= Max (Dp[j][k], dp[j-s[i].a/2][k-1]+s[i].v),///Add the item to the edge, then this item occupies the amount of A/2, the optimal solution of this state is related to having k-1 items on the edge. Ans=Max (ans, dp[j][k]); }}} printf ("Case #%d:%lld\n", tt++, ans); } return 0;}
HDU 5543 Pick The sticks (01 backpack)