HDU2639(01背包第K大)
Bone Collector II
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3437 Accepted Submission(s): 1773
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.
/*
Accepted 2639 858MS 5372K 831 B G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
int dp[MAXN][MAXN];
int n,W,K;
int v[MAXN],w[MAXN];
int vec[MAXN],cnt;
bool comp(int x,int y)
{
return x > y;
}
void KthZeroOnePack()
{
for(int i=;i<n;i++)
{
for(int j=W;j>=w[i];j--)
{
cnt=;
for(int th=;th<=K;th++)
{
vec[cnt++]=dp[j][th];
vec[cnt++]=dp[j-w[i]][th]+v[i];
}
sort(vec,vec+cnt,comp);
cnt=unique(vec,vec+cnt)-vec;
for(int th=;th<=min(cnt,K);th++) dp[j][th]=vec[th-];
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&W,&K);
memset(dp,,sizeof(dp));
for(int i=;i<n;i++) scanf("%d",&v[i]);
for(int i=;i<n;i++) scanf("%d",&w[i]);
KthZeroOnePack();
printf("%d\n",dp[W][K]);
}
return ;
}
上面用了STL里的sort函数速度较慢...
因为dp[j][1]...dp[j][k]与dp[j-w[i]][1]+v[i]...dp[j-w[i]][k]+v[i]是依次递减的,那么我们可以用两个数组将这两组数组保存起来,再O(N)的时间内求得第K大。
/*
Accepted 2639 171MS 5372K 966 B G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"queue"
using namespace std;
const int MAXN=;
int dp[MAXN][MAXN];
int n,W,K;
int v[MAXN],w[MAXN];
int s1[MAXN],s2[MAXN];
void KthZeroOnePack()
{
for(int i=;i<n;i++)
{
for(int j=W;j>=w[i];j--)
{
for(int th=;th<=K;th++)
{
s1[th-]=dp[j][th];
s2[th-]=dp[j-w[i]][th]+v[i];
}
s1[K]=s2[K]=-;
int cnt=;
int cnt1=,cnt2=;
while(cnt<=K&&(s1[cnt1]!=-||s2[cnt2]!=-))
{
if(s1[cnt1]>s2[cnt2]) dp[j][cnt]=s1[cnt1++];
else dp[j][cnt]=s2[cnt2++];
if(dp[j][cnt]!=dp[j][cnt-]) cnt++;
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&W,&K);
memset(dp,,sizeof(dp));
for(int i=;i<n;i++) scanf("%d",&v[i]);
for(int i=;i<n;i++) scanf("%d",&w[i]);
KthZeroOnePack();
printf("%d\n",dp[W][K]);
}
return ;
}
HDU2639(01背包第K大)的更多相关文章
- HDU 2639 Bone Collector II【01背包 + 第K大价值】
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 2639(01背包第K大)
http://acm.hdu.edu.cn/showproblem.php?pid=2639 http://blog.csdn.net/lulipeng_cpp/article/details/758 ...
- hdu2639 01背包第K优解
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...
- HDU 2639 (01背包第k优解)
/* 01背包第k优解问题 f[i][j][k] 前i个物品体积为j的第k优解 对于每次的ij状态 记下之前的两种状态 i-1 j-w[i] (选i) i-1 j (不选i) 分别k个 然后归并排序并 ...
- HDU2639[背包第K大]
题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=2639] 题意:求第k大背包. 题解:利用二路归并的思想,求解第K大的值. #include<bi ...
- 杭电 2639 Bone Collector II【01背包第k优解】
解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[ ...
- Bone Collector II HDU - 2639 01背包第k最大值
题意: 01背包,找出第k最优解 题解: 对于01背包最优解我们肯定都很熟悉 第k最优解的话也就是在dp方程上加一个维度来存它的第k最优解(dp[i][j]代表,体积为i能获得的第j最大价值) 对于每 ...
- HDU 3639 Bone Collector II(01背包第K优解)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- Java中的BigInteger在ACM中的应用
Java中的BigInteger在ACM中的应用 在ACM中的做题时,常常会遇见一些大数的问题.这是当我们用C或是C++时就会认为比較麻烦.就想有没有现有的现有的能够直接调用的BigInter,那样就 ...
- Appium 从 0 到 1 搭建移动 App 功能自动化测试平台 (1):模拟器中运行 iOS 应用
转载:https://testerhome.com/topics/4960 在上一篇文章中,我对本系列教程的项目背景进行了介绍,并对自动化测试平台的建设进行了规划. 在本文中,我将在已准备就绪的iOS ...
- 转:学习linux驱动经典书籍
Linux驱动学习的最大困惑在于书籍的缺乏,市面上最常见的书为<linux_device_driver 3rd Edition>,这是一本很经典的书,无奈Linux的东东还是过于庞大,这本 ...
- 认识Gulp
gulp详细入门教程:http://www.ydcss.com/archives/18 安装gulp 前提:已经安装node.js.npm $ npm install gulp --save-dev ...
- RDLC报表 报表数据 栏 快捷键
血淋淋的教训啊,这段时间用RDLC报表,创建报表后会在右边加载[服务器资源管理器][工具栏][报表数据]一些栏目 如图 [服务器资源管理器][工具栏]还好[报表数据]栏在开发的时候不小心点了X给关掉了 ...
- 九度OJ 1105:字符串的反码 (翻译)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4929 解决:1529 题目描述: 一个二进制数,将其每一位取反,称之为这个数的反码.下面我们定义一个字符的反码.如果这是一个小写字符,则它 ...
- Recurrent neural networks are very powerful, because they combine two properties
https://www.cs.toronto.edu/~hinton/csc2535/notes/lec10new.pdf Distributed hidden state that allows t ...
- Dynamic Web Module to 3.0 报错
一.问题 使用maven项目创建的webapp项目Dynamic Web Module 默认是2.3. 当我们要切换到3.0的时候出现这个错误. 二.解决 1.点击进入Navigator view ( ...
- CoreData使用
1.如果想创建一个带有coreData的程序,要在项目初始化的时候勾选中 2.创建完成之后,会发现在AppDelegate里多出了几个属性,和2个方法 <span style="fon ...
- ubuntu 12.04安装alsa-lib、alsa-utils【转】
1. alsa-lib ./configure sudo make install 注意:默认是安装到/usr/这个目录下面,但是我测试多了多次,安装了alsa-lib之后,系统就没有声音了,也没有找 ...