hdu 2639 第k大背包

Bone Collector II

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1682    Accepted Submission(s): 863


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.
 
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.
 
Output
One integer per line representing the K-th maximum of the total value (this number will be less than 231).
 
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
//s[1100]可以用来储存01背包最大价值,题目要求的是第k大的
//所以用s[1100][110]来储存,s[j][k]表示体积为j第k大价值
//01背包就不用说了,关键是怎么处理那1....k大的价值
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
    int val;
    int V;
}p[110];
int s[1100][110],tmp[110];
int main()
{
    int n,v,k,kk,i,j,t,max;
    cin>>t;
    while(t--)
    {
        cin>>n>>v>>k;
        memset(tmp,0,sizeof(tmp));
        memset(s,0,sizeof(s));
        for(i=0;i<n;i++)
            cin>>p[i].val;
        for(i=0;i<n;i++)
            cin>>p[i].V;
        for(i=0;i<n;i++)
            for(j=v;j>=p[i].V;j--)
        {
            int cn=0;
            for(kk=1;kk<=k;kk++)
            {
                tmp[cn++]=s[j][kk];
                tmp[cn++]=s[j-p[i].V][kk]+p[i].val;
            }
            sort(tmp,tmp+cn);
            int ko=1;
            for(kk=cn-1;kk>=0;kk--)
            {
                if(ko>k)
                break;
                if(kk==cn-1||tmp[kk]!=tmp[kk+1])
                    s[j][ko++]=tmp[kk];
            }
        }
        cout<<s[v][k]<<endl;
    }
}
View Code
原文地址:https://www.cnblogs.com/ainixu1314/p/3383791.html