小白算法练习 简单背包专题002 01背包k优解 hdu dp
来源:互联网 发布:css工具 ubuntu 编辑:程序博客网 时间:2024/05/22 13:18
Bone Collector II
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5107 Accepted Submission(s): 2689
Problem Description
The title of this problem is familiar,isn't it?yeah,if 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.
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.
Input
The first line contain a integer T , the number of 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.
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.
Output
One integer per line representing the K-th maximum of the total value (this number will be less than 231).
Sample Input
35 10 21 2 3 4 55 4 3 2 15 10 121 2 3 4 55 4 3 2 15 10 161 2 3 4 55 4 3 2 1
Sample Output
1220
代码:
#include<iostream>#include<algorithm>#include<cstring>using namespace std;bool cmp(int a,int b){ return a>b;}int main(){ int T; cin>>T; while(T--) { int v[1008]={0}; int w[1008]={0}; int dy[1008]={0}; int dn[1008]={0}; int dp[1008][31 ]={0}; int N=0,M=0,K=0; cin>>N>>M>>K; for(int i=0;i<N;i++) { cin>>w[i]; } for(int i=0;i<N;i++) { cin>>v[i]; } int Max=-1; int k; for(int i=0;i<N;i++) { for(int j=M;j>=v[i];j--) { for(k=1;k<=K;k++) { dy[k]=dp[j-v[i]][k]+w[i]; dn[k]=dp[j][k]; } dy[k]=-1; dn[k]=-1; int x=1,y=1,z=1; while(z<=K && (x<=K || y<=K)) { if(dy[x]>dn[y]) { dp[j][z]=dy[x]; x++; } else if(dy[x]<=dn[y]) { dp[j][z]=dn[y]; y++; } if(dp[j][z]!=dp[j][z-1]) z++; } } } cout<<dp[M][K]<<endl; } return 0;}
阅读全文
0 0
- 小白算法练习 简单背包专题002 01背包k优解 hdu dp
- 小白算法练习 简单背包问题专题001 hdu 01背包 dp
- 小白算法练习 简单背包专题003 完全背包 hdu lanqiao 包子凑数 dp
- 小白算法练习 简单背包问题专题004 多重背包 二进制化 POJ dp
- DP专题->01背包
- dp专题-简单基础dp-背包
- hdu 1059 练习练习dp(多重背包)
- HDU 5117(简单背包dp)
- 【背包专题】K
- 【背包专题】01背包
- 【背包专题】01背包
- hdu 2639 01背包第k优解
- hdu 2639 01背包-K优解
- hdu 2639 Bone Collector II (dp 01背包求第k优解)
- DP 01背包 hdu 2602
- hdu 2602 DP 01背包
- hdu 2602 01背包DP
- hdu 2955 (dp 01背包)
- 广度优先算法展示相关/如何在代码中计算当前顶点相对于源点的层数
- Linux环境变量文件及配置
- github删除仓库及文件夹
- HTTP 传输内容的压缩
- iOS 一一 项目中的常见文件
- 小白算法练习 简单背包专题002 01背包k优解 hdu dp
- 如何解决POST、GET请求时中文乱码
- 给placeholder改外观
- Android 小米6.0APP启动异常
- Stanford CoreNLP 介绍
- VS2013+OpenCV开源库使用入门
- Material Design的使用
- linux编译动态库和环境绑定
- Ubuntu12升14常见问题汇总