Description
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.
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.
Input
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.
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.
Output
One integer per line representing the K-th maximum of the total value (this number will be less than 2
31).
Sample Input
3 5 10 2 1 2 3 4 5 5 4 3 2 1 5 10 12 1 2 3 4 5 5 4 3 2 1 5 10 16 1 2 3 4 5 5 4 3 2 1
Sample Output
12 2 0
解题报告:
这题实质上是求01背包的第k个最优解。首先要推出递推方程,用dp[i][j][k]表示前i个物体中选出总质量不超过j的总价值第k大的价值。
那么就有递推方程dp[i][j][1~k] = max_different_k(dp[i-1][j][1~k], dp[i-1][j-w[i]][1~k] +v[i])。
为什么第i轮前k大的结果一定是在第i-1轮前k大的结果中产生呢?
因为
任意有 dp[i][j][m](m > k) < dp[i][j][k]
所以
dp[i-1][j][m] (m > k) < dp[i-1][j][k],
dp[i-1][j-w[i]][m] + v[i] < dp[i-1][j-w[i]][k] + v[i]
所以只考虑上一轮前k大的结果,从这里面进行状态转移就可以了。
观察dp方程,可以用发现可以用滚动数组实现。
dp[j][1~k] = max_different_k(dp[j][1~k], dp[j-w[i]][1~k] +v[i])。
前k大的结果要去重。
任意有 dp[i][j][m](m > k) < dp[i][j][k]
所以
dp[i-1][j][m] (m > k) < dp[i-1][j][k],
dp[i-1][j-w[i]][m] + v[i] < dp[i-1][j-w[i]][k] + v[i]
所以只考虑上一轮前k大的结果,从这里面进行状态转移就可以了。
观察dp方程,可以用发现可以用滚动数组实现。
dp[j][1~k] = max_different_k(dp[j][1~k], dp[j-w[i]][1~k] +v[i])。
前k大的结果要去重。
#include <stdio.h>
#include <string.h>
int v[1005], w[1005], dp[1005][32], a[32], b[32], T, t, n, W, k, i, j, c1, c2, c3;
int main()
{
scanf("%d", &T);
while(T --){
scanf("%d%d%d", &n ,&W, &k);
memset(dp, 0, sizeof(dp));
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (i = 0; i < n; i ++)
scanf("%d", v + i);
for (i = 0; i < n; i ++)
scanf("%d", w + i);
for (i = 0; i < n; i ++){
for (j = W; j >= w[i]; j --){
// dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
for (t = 1; t <= k; t ++){
a[t] = dp[j][t];
b[t] = dp[j-w[i]][t] + v[i];
}
//merge and unique
c1 = c2 = c3 = 1;
while(c1 <= k && (c2 <= k || c3 <= k)){
if(a[c2] >= b[c3]){
dp[j][c1] = a[c2++];
}
else{
dp[j][c1] = b[c3++];
}
if(dp[j][c1] != dp[j][c1-1]){
c1 ++;
}
}
}
}
printf("%d\n", dp[W][k]);
}
return 0;
}