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 ...
随机推荐
- 【linux配置】在VMware中为Redhat HAT配置本地yum源
在VMware中为Redhat HAT配置本地yum源 今天准备使用CM安装大数据环境,到需要几台机器都使用同一套yum源才可以,所以想到将Redhat镜像文件拷贝到虚拟机中,在挂起使用,最后通过ht ...
- golang数组 排序和查找
package main import "fmt" func BubbleSort(arr *[5]int){ fmt.Println("排序前arr=",(* ...
- Function相关的小知识
重载 相同函数名,不同参数列表的多个函数,在调用时可自动根据传入参数的不同,选择对应的函数执行.为什么使用重载: 减轻API的名字,减轻调用者的负担.何时使用重 ...
- SQLServer —— 变量的使用
一.局部变量的定义与赋值 定义语法: -- 声明一个局部变量 DECLARE @变量名 数据类型 -- 声明多个局部变量 DECLARE @变量名1 数据类型1, @变量名2 数据类型2 赋值语法: ...
- RabbitMQ默认端口
4369 (epmd), 25672 (Erlang distribution)5672, 5671 (AMQP 0-9-1 without and with TLS)15672 (if manage ...
- JDBC操作数据库实例
jdbc操作数据库实例 1.jdbc创建数据库 1.1 前提条件 1.拥有创建和删除表的权限 2.数据库已经启动,且可用 1.2 jdbc创建数据库表的步骤: 导包:导入需要进行数据库编程的 JDBC ...
- HTML/CSS学习之 三列布局,其中左侧和右侧的部分宽度固定,中间部分宽度随浏览器宽度的变化而自适应变化
第一种方法:绝对定位 <!DOCTYPE html> <html> <head> <title>三列布局</title> <link ...
- MyBatis动态SQL(一)
MyBatis 的强大特性之一便是它的动态 SQL.动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似.在 MyBatis 之前的版本中,有很多元素需要花时间了解.MyBatis 3 ...
- hdu 6201 【树形dp||SPFA最长路】
http://acm.hdu.edu.cn/showproblem.php?pid=6201 n个城市都在卖一种书,该书的价格在i城市为cost[i],商人打算从某个城市出发到另一个城市结束,途中可以 ...
- Visual Studio中,无法嵌入互操作类型“……”,请改用适用的接口的解决方法
解决方案:选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型”设置为False,问题轻松解决. 问题分析: 1.”嵌入互操作类型”中的嵌入就是引进.导入的意思,类似于c#中using,c中 ...