Bone Collector II
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5701 Accepted Submission(s): 3007
Problem 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
Author
teddy
Source
Recommend
题意:01背包的变种,询问的不是能装物品的最大价值,而是第k大价值。
思路:思考一下普通的01背包每个状态保存的是什么东西,一般来说是最大值或者是最小值,而这一次我们要求的是第k大的值。再想一下每次状态转移的时候我们是选取两者中的最值(对于一个物品选或者不选两种选择),现在求第k大值的话我们在状态转移的时候不止要保存最大值,需要保存的是前k大的值。那么我们可以再开一维,dp[j][k]表示体积为j时第k大的价值。我们可以开数组A和B,A存储选择一个物品的前k大价值,B存储不选择这一个物品的前k大价值。这两个数组中存储的数都是由大到小的有序数组,那么状态转移的时候只需要将这两个数组有序合并就可以了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int dp[1005][33];
int A[33],B[33];
int main()
{
int n,i,j,k,v,kk;
int a,b,c;
int w[105],d[105];
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d",&n,&v,&k);
for(i=0;i<n;i++)
scanf("%d",&d[i]);
for(i=0;i<n;i++)
scanf("%d",&w[i]);
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
{
for(j=v;j>=w[i];j--)
{
for(kk=1;kk<=k;kk++)
{
A[kk]=dp[j-w[i]][kk]+d[i];
B[kk]=dp[j][kk];
}
A[kk]=-1;
B[kk]=-1;
a=b=c=1;
while(c<=k&&(A[a]!=-1||B[b]!=-1))
{
if(A[a]>B[b])
{
dp[j][c]=A[a++];
}
else
dp[j][c]=B[b++];
if(dp[j][c]!=dp[j][c-1])
c++;
}
}
}
printf("%d\n",dp[v][k]);
}
}