Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2639
Test instructions: Give a line of value, a row of volume, let you in the range of V volume to find the value of the K-large
Analysis: Dp[i][j][k] represents the first I item volume of J when the first K solution. Then for each state dp[i][j] need to maintain a good pre-K solution.
Each take or not take the article I item according to the former K excellent solution each time, use the array A to accept the article I item, the array B record does not take, so
The array A, B, all the X solutions that can make up J, and finally take the first K-optimal solution in X to record it. The rest is definitely not the former K-U solution ...
#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<cstdlib>#include<vector>#include<Set>#include<map>#defineLL Long Long#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineN 10010using namespacestd;intp[ the],w[ the],dp[1010][ -],a[ -],b[ -];intMain () {intt,n,v,k; scanf ("%d",&t); while(t--) {scanf ("%d%d%d",&n,&v,&k); Memset (DP,0,sizeof(DP)); for(intI=1; i<=n; i++) scanf ("%d",&P[i]); for(intI=1; i<=n; i++) scanf ("%d",&W[i]); for(intI=1; i<=n; i++) { for(intl=0; j>=w[i]; j--) { for(intL=1; l<=k; l++)//The composition volume of J is nothing but j-w[i] state plus Part I and do not take the first part I, up to 2k{A[l]=dp[j-w[i]][l]+P[i]; B[L]=Dp[j][l]; } a[k+1]=-1; B[k+1]=-1; intx, Y, Z X=y=z=1; while(z<=k&& (a[x]!=-1|| b[y]!=-1))//take the largest K solution in up to 2k and deposit it in an orderly way { if(a[x]>B[y]) {Dp[j][z]=A[x]; X++; } Else{Dp[j][z]=B[y]; Y++; } if(dp[j][z]!=dp[j][z-1]) z++; }}} printf ("%d\n", Dp[v][k]); }}
View Code
hdu2639 (backpack for K-optimal solution)