杭电-2639Bone Collector II
2015-10-25 20:12
295 查看
Bone Collector II
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3208 Accepted Submission(s): 1663
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
One integer per line representing the K-th maximum of the total value (this number will be less than 231).
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
12 2 0
01背包的变形体,这次不是让背包的容量最大了,是要求出给定的第K大
我不是很理解,大概意思就是说,把每一种容量的背包的前K种方法都写出来!
下面是我借鉴的别人的解释:
题意:给出一行价值,一行体积,让你在v体积的范围内找出第k大的值.......(注意,不要 和它的第一题混起来,它第一行是价值,再是体积)
思路:首先dp[i][j]代表的是在体积为i的时候第j优解为dp[i][j]......那么,我们就可以这样思考,i对应体积,那么如果只是一维的dp[i],代表的应该是体积为i时的最大值,那么同理,dp[i][1]代表的是体积为i时的最大值,那么我们就可以退出两种动态,dp[i][m],dp[i-s[i][0]][m]+s[i][1].....然后把这两种状态开个两个数组分别保存起来,再合并出体积为i时的前k优解......依次后推,直到dp[v][k].......
下面代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int f[1010][33]; int main() { int N,i,j,v,n,k,val[110],vol[110]; scanf("%d",&N); while(N--) { scanf("%d%d%d",&n,&v,&k); for(i=0;i<n;i++) scanf("%d",&val[i]); for(i=0;i<n;i++) scanf("%d",&vol[i]); memset(f,0,sizeof(f)); int a[50],b[50],kk; for(i=0;i<n;i++) for(j=v;j>=vol[i];j--) { for(kk=0;kk<k;kk++) { a[kk]=f[j-vol[i]][kk]+val[i]; b[kk]=f[j][kk]; } a[k]=b[k]=-1; int x=0,y=0,z=0; while(z<k&&(a[x]!=-1||b[y]!=-1)) { if(a[x]>b[y]) f[j][z]=a[x++]; else f[j][z]=b[y++]; if(f[j][z]!=f[j][z-1]) z++; } } printf("%d\n",f[v][k-1]); } return 0; }
相关文章推荐
- main()与_tmain()区别
- 需求分析过程
- POJ 3094 Quicksum(简单的问题)
- BZOJ1051[HAOI2006]受欢迎的牛
- 南邮OJ 1015 最大公约数和最小公倍数
- Free Candies
- java中开源日志记录工具log4j
- JAVA常见面试题
- Python + Requests + Unittest接口自动化测试
- 手机网站字号自适应
- Qt Gradle
- UIViewController 中的 willMoveToParentViewController和didMoveToParentViewController的使用
- 矩阵的存储
- sql 存储过程 循环
- jre运行报Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
- Android后退事件的处理
- codeforces585D. Lizard Era: Beginning
- 【转】三星8552 手机提示升级系统 完成后重启 开机画面一直停留在三星的LOGO 一闪一闪 怎么办
- poj_2441 状态压缩dp
- ASCII排序 nyoj4