Bone Collector II (HDU - 2639)

题面

The title of this problem is familiar,isn’t it?yeah,if you had took part in the “Rookie Cup” competition,you must have seem this title.If you haven’t seen it before,it doesn’t matter,I will give you a link:

Here is the link: http://acm.hdu.edu.cn/showproblem.php?pid=2602

Today we are not desiring the maximum value of bones,but the K-th maximum value of the bones.NOTICE that,we considerate two ways that get the same value of bones are the same.That means,it will be a strictly decreasing sequence from the 1st maximum , 2nd maximum .. to the K-th maximum.

If the total number of different values is less than K,just ouput 0.

输入

The first line contain a integer T , the number of cases. Followed by T cases , each case three lines , the first line contain two integer N , V, K(N <= 100 , V <= 1000 , K <= 30)representing the number of bones and the volume of his bag and the K we need. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

输出

One integer per line representing the K-th maximum of the total value (this number will be less than 2^31).

样例输入

 13
 25 10 2
 31 2 3 4 5
 45 4 3 2 1
 55 10 12
 61 2 3 4 5
 75 4 3 2 1
 85 10 16
 91 2 3 4 5
105 4 3 2 1

样例输出

112
22
30

提示

思路

代码

 1using namespace std;
 2typedef long long ll;
 3const int inf = 0x3f3f3f3f;
 4const int N = 1e3+5;
 5
 6int w[N], c[N];
 7int T, n, v, m;
 8
 9int dp[N][N], a[N], b[N];
10
11int main(void) {
12    scanf("%d", &T);
13    while(T--) {
14        scanf("%d%d%d", &n, &v, &m);
15        for(int i=0; i<n; i++)
16            scanf("%d", &w[i]);
17        for(int i=0; i<n; i++)
18            scanf("%d", &c[i]);
19
20        memset(dp, 0, sizeof(dp));
21
22        for(int i=0; i<n; i++) {
23            for(int j=v; j>=c[i]; j--) {
24                for(int k=1; k<=m; k++) {
25                    a[k] = dp[j][k];
26                    b[k] = dp[j-c[i]][k]+w[i];
27                }
28                a[m+1] = b[m+1] = -1;
29                int di=1, ai=1, bi=1;
30                while(di<=m && (ai<=m || bi<=m)) {
31                    if(a[ai]>b[bi])
32                        dp[j][di] = a[ai++];
33                    else
34                        dp[j][di] = b[bi++];
35                    if(dp[j][di]!=dp[j][di-1])
36                        di++;
37                }
38            }
39        }
40        printf("%d\n", dp[v][m]);
41    }
42    return 0;
43}