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
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.
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.
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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int dp[][];
int val[],vol[];
int n,v,K;
int a[],b[];
int main()
{
freopen("in.txt","r",stdin);
int i,j,k;
int x,y,z;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&v,&K);
for(i=;i<=n;i++)
scanf("%d",&val[i]);
for(i=;i<=n;i++)
scanf("%d",&vol[i]);
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)
{
for(j=v;j>=vol[i];j--)
{
for(k=;k<=K;k++) //记录每种状态的k优解
{
a[k]=dp[j][k];
b[k]=dp[j-vol[i]][k]+val[i];
}
a[k]=b[k]=-; //存储的内容已经按照从小到大排序好了,然后合并到dp数组中去
x=y=z=;
while(z<=K&&(a[x]!=-||b[y]!=-))
{
if(a[x]>b[y])
{
dp[j][z]=a[x];
x++;
}
else
{
dp[j][z]=b[y];
y++;
}
if(dp[j][z-]!=dp[j][z])
z++;
}
}
}
printf("%d\n",dp[v][K]);
}
return ;
}
Bone Collector II(HDU 2639 DP)的更多相关文章
- (01背包 第k优解) Bone Collector II(hdu 2639)
http://acm.hdu.edu.cn/showproblem.php?pid=2639 Problem Description The title of this problem i ...
- Bone Collector II(hdu 2639)
题意:求01背包的第k最优值 输入:第一行为T,下面是T组数据,每组数据有n,m,k 代表n件物品,m容量,和题目要求的k,下一行是n个物品的价值,再一行是n个物品的体积 输出:T行答案 /* 类似于 ...
- 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&quo ...
- hdu 2639 Bone Collector II(01背包 第K大价值)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 3639 Bone Collector II(01背包第K优解)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU2639Bone Collector II(01背包变形)
01背包,求第k大. 以前看k短路的时候看过代码以为懂了 = =结果还是跑去看了别人的代码才会.果然要自己写一遍才行啊 0.0难得1A.. 每次把可能的2k种求出来,求前k个.注意要不一样的k个数.. ...
- HDU 2639 Bone Collector II (dp)
题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...
- hdu 2639 Bone Collector II (01背包,求第k优解)
这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...
- [HDOJ2639]Bone Collector II(第k优01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:求01背包的第k优解 dp(i, j)表示容量为j时的i优解 对于第二维的操作和01背包几 ...
随机推荐
- 函数:灵活即强大 - 零基础入门学习Python018
函数:灵活即强大 让编程改变世界 Change the world by program 上节课我们基本介绍了Python函数的用法,非常简单.这节课我们主要针对函数的参数和返回值进一步深入学习.学习 ...
- jquery 做出专业的界面,SHOW 一下最近的成果~~~
最近在项目中把整个UI框架重新做了一下,都是用Jquery实现的,没有使用EXT.EasyUI那一类的UI框架再也不用担心版权问题啦~~~~~~ 接下来我会在博客中把常用的功能分享出来,先上一下动态T ...
- mongoexport导出数据
mongoexport用法: /***** Export MongoDB data to CSV, TSV or JSON files.options: --help ...
- C 程序提升效率的10种方法
本文向你介绍规范你的C代码的10种方法(引用地址http://forum.eepw.com.cn/thread/250025/1). 1. 避免不必要的函数调用 考虑下面的2个函数: void s ...
- DJANGO的HTTPRESPONSE流式输出
在项目当中遇到的问题,网上有样例代码,但都不行,后来,发现在了1.5版本之后,新的STREAMHTTPRESPONSE对象, 搞定. from django.http import HttpRespo ...
- poj2975--Nim
题意:对于一个给定的取石子游戏,有多少种先手策略获胜? Ans:若无法获胜,则输出0. 若能获胜我们只要找到一堆石子,使得我们能取它的一部分让总和的异或和变为0.我们先将整个游戏的值异或起来为s 则a ...
- shell 脚本FTP自动上传文件
下面的脚本 会把本地的文本文件压缩后, 上传到FTP服务器上. 里面有一点小逻辑, 就是上传的文本文件 是 日期时间.txt 形式的, 一天写一个日志文件, 今天的文件不上传, 只上传 老的日志文件. ...
- .Net XML操作 <第二篇>
一.XML文件操作中与.Net中对应的类 微软的.NET框架在System.xml命名空间提供了一系列的类用于Dom的实现. 以下给出XML文档的组成部分对应.NET中的类: XML文档组成部分 对应 ...
- [置顶] Android EditText/TextView使用SpannableString显示复合文本
在Android中EditText用于编辑文本,TextView用于显示文本,但是有时候我们需要对其中的文本进行样式等方面的设置.Android为我们提供了SpannableString类来对指定文本 ...
- C# 调用外部程序,并获取输出和错误信息
1. 同步模式 public void exec(string exePath, string parameters) { System.Diagnostics.ProcessStartInfo ps ...