Bone Collector II(HDU 2639 DP)

Posted 御心飞行

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bone Collector II(HDU 2639 DP)相关的知识,希望对你有一定的参考价值。

Bone Collector II

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


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
记录每种状态的第k解;
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 int dp[1002][32];
 7 int val[102],vol[102];
 8 int n,v,K;
 9 int a[32],b[32];
10 int main()
11 {
12     freopen("in.txt","r",stdin);
13     int i,j,k;
14     int x,y,z;
15     int T;
16     scanf("%d",&T);
17     while(T--)
18     {
19         scanf("%d%d%d",&n,&v,&K);
20         for(i=1;i<=n;i++)
21             scanf("%d",&val[i]);
22         for(i=1;i<=n;i++)
23             scanf("%d",&vol[i]);
24         memset(dp,0,sizeof(dp));
25         for(i=1;i<=n;i++)
26         {
27             for(j=v;j>=vol[i];j--)
28             {
29                 for(k=1;k<=K;k++)        //记录每种状态的k优解
30                 {
31                     a[k]=dp[j][k];            
32                     b[k]=dp[j-vol[i]][k]+val[i];
33                 }
34                 a[k]=b[k]=-1;        //存储的内容已经按照从小到大排序好了,然后合并到dp数组中去
35                 x=y=z=1;
36                 while(z<=K&&(a[x]!=-1||b[y]!=-1))
37                 {
38                     if(a[x]>b[y])
39                     {
40                         dp[j][z]=a[x];
41                         x++;
42                     }
43                     else
44                     {
45                         dp[j][z]=b[y];
46                         y++;
47                     }
48                     if(dp[j][z-1]!=dp[j][z])
49                         z++;
50                 }
51             }
52         }
53         printf("%d\n",dp[v][K]);
54     }
55     return 0;
56 }

 

以上是关于Bone Collector II(HDU 2639 DP)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2639 Bone Collector II

HDU2639Bone Collector II[01背包第k优值]

HDU 2639 Bone Collector II(01背包变形第K大最优解)

背包专题A - Bone Collector II hdu2639 01背包的第k个最优解

Bone Collector II

Bone Collector II(01背包kth)