HDU 2639(01背包求第K大值)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2639
Bone Collector II
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5817 Accepted Submission(s): 3067
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
2
0
#include<bits/stdc++.h>
using namespace std;
#define max_v 10005
int v[max_v],w[max_v];
int dp[max_v][];//dp[j][k] 代表背包容量为j时 第k大值
int a[max_v],b[max_v];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,c,m;
scanf("%d %d %d",&n,&c,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&v[i]);
}
for(int i=;i<=n;i++)
{
scanf("%d",&w[i]);
}
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
for(int j=c;j>=w[i];j--)
{
int k;
// set <int,greater<int> > s;
for(k=;k<=m;k++ )
{
// a[k]=dp[j-w[i]][k]+v[i];
// b[k]=dp[j][k];
b[k]=dp[j-w[i]][k]+v[i];
a[k]=dp[j][k];
//s.insert(b[k]);
//s.insert(a[k]);
}
/* set<int,greater<int> >::iterator it;
int z=1;
for(it=s.begin();it!=s.end();it++)
{
dp[j][z++]=*it;
}
//使用set集合会超时 偷懒是不可能的了
*/ int x,y,z;
x=y=z=;
a[k]=b[k]=-;
while(z<=m&&(x<=m||y<=m))
{
if(a[x]>b[y])
{
dp[j][z]=a[x++];
}else
{
dp[j][z]=b[y++];
}
if(dp[j][z]!=dp[j][z-])
{
z++;
}
}
}
}
printf("%d\n",dp[c][m]);
}
return ;
}
HDU 2639(01背包求第K大值)的更多相关文章
- HDU 2639 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 ...
- Bone Collector II---hdu2639(01背包求第k优解)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639求01背包的第k大解.合并两个有序序列 选取物品i,或不选.最终的结果,是我们能在O(1)的时间内 ...
- 关于01背包求第k优解
引用:http://szy961124.blog.163.com/blog/static/132346674201092775320970/ 求次优解.第K优解 对于求次优解.第K优解类的问题,如果相 ...
- HDU 2639 01背包(分解)
http://acm.hdu.edu.cn/showproblem.php?pid=2639 01背包第k优解,把每次的max分步列出来即可 #include<stdio.h> #incl ...
- POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8807 Accepted ...
- 动态求区间K大值(权值线段树)
我们知道我们可以通过主席树来维护静态区间第K大值.我们又知道主席树满足可加性,所以我们可以用树状数组来维护主席树,树状数组的每一个节点都可以开一颗主席树,然后一起做. 我们注意到树状数组的每一棵树都和 ...
- Bone Collector II HDU - 2639 01背包第k最大值
题意: 01背包,找出第k最优解 题解: 对于01背包最优解我们肯定都很熟悉 第k最优解的话也就是在dp方程上加一个维度来存它的第k最优解(dp[i][j]代表,体积为i能获得的第j最大价值) 对于每 ...
- HDU 1171 Big Event in HDU【01背包/求两堆数分别求和以后的差最小】
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- jQuery基础(DOM篇,append(),after(),prepend(),insertAfter(),节点删除,遍历方法each())
1.DOM创建节点及节点属性 创建流程比较简单,大体如下: - 创建节点(常见的:元素.属性和文本) - 添加节点的一些属性 - 加入到文档中 流程中涉及的一点方法: - 创建元素:d ...
- JS将秒换成时分秒实现代码 [mark]
将秒换成时分秒的方法有很多,在本文将为大家介绍下,使用js的具体的实现思路,有需要的朋友可以参考下,希望对大家有所帮助 http://www.jb51.net/article/41098.htm fu ...
- Vue-学习。
---恢复内容开始--- Vue.js 与 Angular.js 非常相似,只要学过了Angular.js然后在学Vue.js就非常的简单. 什么是Vue? 相比Angularjs和ReactJS,V ...
- 标准的Flask启动文件
最近有一些同学问了我一些项目结构的问题 所以今天给大家专门讲解 解耦后的项目 目录我会分为两种方式:一种是普通解耦 一种是多mvc解耦 首先 我没先建立我们程序的文件夹并且在这个文件夹内写一个和这个文 ...
- 编写带有点击特效的UIButton
编写带有点击特效的UIButton 效果: 源码: // // ViewController.m // Button // // Created by XianMingYou on 15/1/18. ...
- NSCopying简析
NSCopying简析 用到NSCopying的时候并不多,但还是有必要知道最基本的用途,比方说数组的拷贝操作,需要注意的是,数组的拷贝操作并不是执行了 copy 方法,而是需要执行 initWith ...
- linux下jira搭建&破解(转自:https://www.cnblogs.com/zpw-1/p/9553358.html)
写在前面 网络类似文章不少,但是同样的路,别人走可能一马平川,自己走可能磕磕绊绊.记录一下自己搭建过程的一路踩坑历程[目前还记得的]. 一.环境准备 1,jira7.3的运行是依赖java环境的,也就 ...
- wxpython 编程触发菜单或按钮事件
最近逐步熟悉wxpython,编写了几个小小功能的GUI程序,GUI中免不了会有在代码中触发控件事件的业务需求.在其他Gui界面的语言中有postevent.triggerevent 调用事件名称的函 ...
- Office 365实现单点登录系列(2)—Azure AD Connect安装与配置
前言 第一篇文章我已经为大家分享了在在Azure上搭建域控服务器的方法,如果大家本地已经有了域环境,可以直接从这一篇文章开始阅读.Azure AD Connect的前身是DirSync,是专门用于目录 ...
- 生成器-yield初接触
什么是生成器? 生成器的实质就是迭代器 在python中有三种方式来获取生成器 1. 通过生成器函数 2. 通过各种推导式实现生成器 3. 通过数据的转换也可以获取生成器 将函数中的return换成y ...