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 }
时间: 2024-10-21 16:05:41

Bone Collector II(HDU 2639 DP)的相关文章

Bone Collector II(hdu 2639)

题意:求01背包的第k最优值 输入:第一行为T,下面是T组数据,每组数据有n,m,k 代表n件物品,m容量,和题目要求的k,下一行是n个物品的价值,再一行是n个物品的体积 输出:T行答案 /* 类似于归并排序中合并的做法,对于f[i][j]的k个最优值,从f[i-1][j]和f[i-1][j-w[i]]+v[i] 的k个最优值中选出最优的k个放入 */ #include<cstdio> #include<iostream> #include<cstring> #defi

Bone Collector II(01背包kth)

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.c

hdu 2639 Bone Collector II(01背包 第K大价值)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3355    Accepted Submission(s): 1726 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took par

【HDU2639】Bone Collector II(01背包第k优解)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2948    Accepted Submission(s): 1526 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa

hdu2639Bone Collector II(第K大背包)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3780    Accepted Submission(s): 1947 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa

HDU 2639 Bone Collector II (DP 第k优解)

题意:输入t,t组测试样例,每组样例输入 n, v, k. 接着输入n个物品的价值,再输入n个物品的体积.求k优解. 分析:dp[n][v][k]表示n个物品,在体积不超过v的情况,第k大的值是多少.dp[i][v][k]与dp[i-1][v][k]与dp[i-1][v-volume[i]]+value[i]有关. #include <iostream> #include <cstdio> #include <cstring> #include <algorith

[HDOJ2639]Bone Collector II(第k优01背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:求01背包的第k优解 dp(i, j)表示容量为j时的i优解 对于第二维的操作和01背包几乎一样,只是我们只需要关注每一次取的前k大个即可. 1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <cli

HDU 6149 Valley Numer II(状压DP)

题目链接 HDU6149 百度之星复赛的题目……比赛的时候并没有做出来. 由于低点只有15个,所以我们可以考虑状压DP. 利用01背包的思想,依次考虑每个低点,然后枚举每个状态. 在每个状态里面任意枚举不在这个状态中的两个点,如果能构成一个valley,那么更新答案. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i) #define dec(i,

BNU25359Escape Time II(状态压缩DP)

There is a fire in LTR ' s home again. The fire can destroy all the things in t seconds, so LTR has to escape in t seconds. But there are some jewels in LTR ' s rooms, LTR love jewels very much so he wants to take his jewels as many as possible befor