hdu 2639 Bone Collector II(01背包 第K大价值)
Bone Collector II
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3355 Accepted Submission(s):
1726
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.
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.
the total value (this number will be less than 231).
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int T,i,j,n,m,v,k,kk;
int dp[][],val[],vol[],A[],B[];
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));
int a,b,c;
for(i=; i<n; i++)
for(j=v; j>=vol[i]; j--)
{
for(kk=; kk<=k; kk++) //从最优的向后循环
{
A[kk]=dp[j-vol[i]][kk]+val[i]; //选中i物体
B[kk]=dp[j][kk]; //不选i物体
}
A[kk]=-; //-1标记为结尾
B[kk]=-;
a=b=c=;
while(c<=k&&(A[a]!=-||B[b]!=-))
{
if(A[a]>B[b]) //大的数排在前面
dp[j][c]=A[a++];
else
dp[j][c]=B[b++];
if(dp[j][c]!=dp[j][c-]) //如果两数相同,则只记录一次
c++;
}
}
printf("%d\n",dp[v][k]);
}
return ;
}
hdu 2639 Bone Collector II(01背包 第K大价值)的更多相关文章
- HDU - 2639 Bone Collector II (01背包第k大解)
分析 \(dp[i][j][k]\)为枚举到前i个物品,容量为j的第k大解.则每一次状态转移都要对所有解进行排序选取前第k大的解.用两个数组\(vz1[],vz2[]\)分别记录所有的选择情况,并选择 ...
- 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 Bone Collector II(01背包变型)
此题就是在01背包问题的基础上求所能获得的第K大的价值. 详细做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,事实上就 ...
- HDU 2639 Bone Collector II (01背包,第k解)
题意: 数据是常规的01背包,但是求的不是最大容量限制下的最佳解,而是第k佳解. 思路: 有两种解法: 1)网上普遍用的O(V*K*N). 2)先用常规01背包的方法求出背包容量限制下能装的最大价值m ...
- hdu–2369 Bone Collector II(01背包变形题)
题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- 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
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 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 ...
随机推荐
- Header解析
不管是作为后端还是前端开发人员,对于web请求的过程和参数都是需要了解的. 下面是对一次简单的http请求的header分析,作为自己的一个总结,也希望对大家有所帮助. 以Chrome为例: 我们对h ...
- TypeScript类型检查机制
类型推断 指不需要指定变量的类型,TS编译器可以根据某些规则自动推断出类型. 什么时候会有类型推断? 声明变量时没有指定类型 函数默认参数 函数返回值 ...... let a; // 这时自动推断为 ...
- 剑指offer 1-6
1. 二维数组中的查找 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 分析 ...
- 【JZOJ4790】【NOIP2016提高A组模拟9.21】选数问题
题目描述 在麦克雷的面前有N个数,以及一个R*C的矩阵.现在他的任务是从N个数中取出R*C个,并填入这个矩阵中.矩阵每一行的法值为本行最大值与最小值的差,而整个矩阵的法值为每一行的法值的最大值.现在, ...
- Python sorted
sorted函数: iterable:是可迭代类型;cmp:用于比较的函数,比较什么由key决定,有默认值,迭代集合中的一项;key:用列表元素的某个属性和函数进行作为关键字,有默认值,迭代集合中的一 ...
- Sublime text 3 如何格式化HTML/css/js代码
Sublime Text 3 安装Package Control 原来Subl3安装Package Control很麻烦,现在简单的方法来了 一.简单的安装方法 使用Ctrl+`快捷键或者通过Vi ...
- H5+ 分享到微信、朋友圈代码示例
h5+分享到微信.朋友圈代码示例 在使用分享功能的时候会莫名的分享失败,debug时发现是图片过大的问题. 图片过大时ios平台上返回错误码-8,安卓上返回错误码-3(我测试是这样) 因此如果第一次分 ...
- 【NS2】添加mUDP、mUdpSink和mTcpSink模块
根据柯老师的教材可知,mUDP是UDP的延伸,除了具有UDP的功能外,还能记录所发送的包的信息.mUdpSink可以把接收到的包的信息记录 到文件中.mTcpSink是TCPsink的延伸,除了具有T ...
- 【NS2】学习点滴
1 $ns duplex-link-op $n2 $n3 queuePos 0.5#此命令用于设置在NAM中显示的队列方向#经测试,发现: # queuePos 0.5表示包从上到下进入队列# que ...
- LeetCode Weekly Contest 6
leetcode现在每周末举办比赛,这是今天上午参加的比赛的题解.题目难度不算大,两个easy,一个medium,一个hard.hard题之前接触过,所以做得比较顺利. 1. Sum of Left ...