HDU-2639 Bone Collector II(01背包k解问题)
来源:互联网 发布:淘宝怎么去同款 编辑:程序博客网 时间:2024/06/04 00:29
Bone Collector II
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5236 Accepted Submission(s): 2771
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.
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.
Output
One integer per line representing the K-th maximum of the total value (this number will be less than 231).
Sample Input
3
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
Sample Output
12
2
0
题意:给你n个骨头的信息(价值和体积),给你一个体积为V的背包,要你求出这个背包装骨头得到的所有价值中第K大的解
k解问题。。。。。。刚开始看的时候很懵逼, 完全不知道处理的思路, 就去看学姐的博客去了
利用二维去处理 第二维就代表了第几次。
但处理过程 有点麻烦, 每放一个物品就需要 处理k次
code:
#include<stdio.h>#include<string.h>#include<bits/stdc++.h>int dp[1003][1003];int main (){ int n, v, K; int value[1003], weight[1003], New[1003], Old[1003]; int N; scanf("%d",&N); while(N--) { memset(dp, 0, sizeof(dp)); scanf("%d%d%d",&n,&v,&K); for (int i=0; i<n; i++) { scanf("%d",&value[i]); //价值 } for (int i=0; i<n; i++) { scanf("%d",&weight[i]); //体积 } for (int i=0; i<n; i++) { for (int j=v; j>=weight[i]; j--) { int k; for (k=1; k<=K; k++) Old[k] = dp[j][k]; for (k=1; k<=K; k++) New[k] = dp[j-weight[i]][k]+value[i]; Old[k] = -1; New[k] = -1; int a, b; a = b = k = 1; while (k<=K&&Old[a]!=-1||New[b]!=-1) { if (Old[a]>New[b]) { dp[j][k] = Old[a]; a++; } else { dp[j][k] = New[b]; b++; } if (dp[j][k]!=dp[j][k-1]) k++; } } } printf ("%d\n",dp[v][K]); }}
- HDU-2639 Bone Collector II(01背包k解问题)
- hdu 2639 Bone Collector II (01背包,第k优解问题)
- Hdu 2639 Bone Collector II (01背包第k优解)
- hdu 2639Bone Collector II 01背包第k大问题
- hdu 2639 Bone Collector II 01背包第K优解
- HDU 2639 Bone Collector II 01背包第K优解
- hdu oj 2639 Bone Collector II (01背包k优解)
- HDU - 2639 Bone Collector II(第K大01背包)
- hdu 2639 Bone Collector II 01背包 第k优解
- hdu 2639 Bone Collector II 第k大01背包
- HDU 2639 Bone Collector II 01背包第k优解
- Bone Collector II HDU - 2639 01背包,第k优解
- HDU 2639 Bone Collector II(01背包第K大解)
- hdu 2639Bone Collector II 背包的第K优解问题
- hdu 2639 Bone Collector II 01背包问题 求第K大最优值。。
- HDU 2639 Bone Collector II(01背包问题-第K优决策)
- hdu 2636 Bone Collector II 01背包(第k优解问题)
- HDU 2639 - Bone Collector II(01背包)
- VB机房收费系统01——使用前文档配置
- Hadoop2.6.0伪分布式集群搭建
- php 垃圾回收深入理解
- 设置电量条颜色
- PyQt5.6 + pycharm 配置 以及 pyinstaller 生成exe 傻瓜教程 100%成功
- HDU-2639 Bone Collector II(01背包k解问题)
- 安卓学习笔记---调用相机功能在安卓7.0以上报错android.os.FileUriExposedException
- Linux的ls命令
- ORA-22835:缓冲区对于CLOB到CHAR转换而言太小
- 十个jQuery代码片段助力Web开发
- FFmpeg的Android端调用库ffmpeg-android的简单使用
- 3 微服务实战系列
- 重新铸就Json
- 7-14 天梯地图(30 分)