Bone Collector II
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4401 Accepted Submission(s): 2286
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 20
这几天心情不好,一直没有写(估计写了也没有人看),你奋斗的 路上避免不了有一些人傻逼,贱人,他们阻挡你前进的步伐,让他影响 心情好几天,不要在意了,继续朝着自己的梦想前进吧! 这个本质上还是01背包,其实如果真正理解了01背包,这一题的理解还是 不是很难的,这里用到一个归并排序(这个不是我们讲的重点); dp[v]=max(dp[j],dp[j-c[i]]+p[i]);其实我们计算最大值时,我们肯定需要 计算出所有的值然后记录最大值就行了,我们需要用a,b数组把计算的结果保 存下来,然后用归并排序保存1-k的数据,然后进行i物品的计算 在计算i物品带来的价值情况,我们把上一次保存的1-k的值,每一种 情况都讨论一下加i与不加i的情况,解释可能不太清楚,具体还要自己好好 想想 我感觉这个问题本质上还是一维方面的变形
#include<cstdio> #include<cstring> int dp[1100][100]; int a[100],b[100]; int c[1100],p[1100]; int main() { int t; scanf("%d",&t); while(t--) { int n,v,k,i; scanf("%d%d%d",&n,&v,&k); for(i=1;i<=n;++i) scanf("%d",&p[i]); for(i=1;i<=n;++i) scanf("%d",&c[i]); memset(dp,0,sizeof(dp)); int j,g; for(i=1;i<=n;++i) { for(j=v;j>=c[i];--j) { for(g=1;g<=k;++g) { a[g]=dp[j][g]; b[g]=dp[j-c[i]][g]+p[i]; } a[g]=b[g]=-1; int xx,yy; xx=yy=g=1; while(g<=k&&(xx<=k||yy<=k)) { if(a[xx]<b[yy]) { dp[j][g]=b[yy]; ++yy; } else { dp[j][g]=a[xx]; xx++; } if(dp[j][g]!=dp[j][g-1]) ++g; } } } printf("%d\n",dp[v][k]); } return 0; }