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 ...
随机推荐
- Android 对保存在 sharedpreference的重要数据进行编解码
有时候为了登录方便会将用户名和密码保存在 sharedpreference里面,可是如果不加以处理密码将以明文保存. 在Android中java层提供了工具类:android.util.Base64; ...
- 【JZOJ3636】【BOI2012】Mobile(mobile)
Mission 著名的手机网络运营商Totalphone 修建了若干基站收发台,以用于把信号网络覆盖一条新建的高速公路.因为Totalphone 的程序员总是很马虎的,所以,基站的传功功率不能独立设置 ...
- NOIP模拟 7.04
魔术球问题弱化版(ball.c/.cpp/.pas) [题目描述] 假设有 n 根柱子,现要按下述规则在这 n 根柱子中依次放入编号为 1,2,3,…的球. (1)每次只能在某根柱子的最上面放球. ( ...
- linux下Nginx安装Zend Optimizer组件步骤
注意:Zend Optimizer 在php5.3以上的版本已经集成了,所以php5.3以上的版本没必要安装了.而推出了Zend Guard Loader. http://www.zend.com/e ...
- Sublime Text3 安装less
1.安装Sublime 插件 (1)安装LESS插件:因为Sublime不支持Less语法高亮,所以,先安装这个插件,方法: ctrl+shift+p>install Package>输入 ...
- .net WebServer示例及调用(接口WSDL动态调用 JAVA)
新建.asmx页面 using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...
- R语言因子
R语言因子 因子是它们用于将数据进行分类并将其存储为级别的数据对象.它们可以同时存储字符串和整数.它们在具有唯一值的有限数目的列是有用的. 例如,"male, "Female&qu ...
- springmvc restful风格操作
ssm框架 controller: package com.sgcc.controller; import java.util.ArrayList; import java.util.List; im ...
- @Transactional注解事务
打了这个注解的类或者方法表示该类里面的所有方法或者这个方法的事务由spring处理,来保证事务的原子性,即方法里面对数据库操作,如果失败则spring负责回滚操作,成功提交操作 一.特性 1.serv ...
- node写简单的爬虫(二)
上次我们已经成功的爬取了网站上的图片,现在我们把爬取的图片存储到本地 首先引入request var request=require('request'); http.get(url, functio ...