hdu2639 Bone Collector II(背包中第 k 大值)

Bone Collector II

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


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
 
分析:01背包的思想:d[i][j] 表示容量为 i ,第 j 大的值;
 
View Code
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #define MAXN 110
 6 #define MAXV 1010
 7 #define MAXK 50
 8 
 9 using namespace std;
10 
11 int cost[MAXN],weight[MAXN],d[MAXV][MAXK],t1[MAXK],t2[MAXK];
12 
13 int main()
14 {
15     int test,n,v,k,i,j,p;
16     scanf("%d",&test);
17     while(test--)
18     {
19         scanf("%d%d%d",&n,&v,&k);
20         for(i=0; i<n; i++)
21             scanf("%d",&weight[i]);
22         for(i=0; i<n; i++)
23             scanf("%d",&cost[i]);
24         memset(d,0,sizeof(d));
25         for(p=0; p<n; p++)
26         {
27             for(i=v; i>=cost[p]; i--)
28             {
29                 for(j=1; j<=k; j++)
30                 {
31                     t1[j]=d[i-cost[p]][j]+weight[p];//不要想到二维费用背包,这里完全不是同一回事
32                     t2[j]=d[i][j];
33                 }
34                 t1[j]=t2[j]=-1;//不需要排序,每次都是从大到小合并的
35                 int x,y,z;
36                 x=y=z=1;
37                 while(z<=k&&(t1[x]!=-1||t2[y]!=-1))//起初我是 x<=k||y<=k;如果t1、t2的后面全是零,x和z将不会增加
38                 {
39                     if(t1[x]>t2[y])
40                     {
41                         d[i][z]=t1[x];
42                         x++;
43                     }
44                     else
45                     {
46                         d[i][z]=t2[y];
47                         y++;
48                     }
49                     if(d[i][z-1]!=d[i][z]) //起初自己写了个合并太真戳了,后来网上看了下,很精简!!
50                         z++;
51                 }
52             }
53         }
54         printf("%d\n",d[v][k]);
55     }
56     return 0;
57 }

 

posted @ 2012-08-13 22:05  mtry  阅读(828)  评论(0)    收藏  举报