hdu 2639 01 背包 存在k 个多解问题
2017-04-18 18:47
197 查看
题目地址
http://acm.hdu.edu.cn/showproblem.php?pid=2639
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4693 Accepted Submission(s): 2443
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
题目大意 给 背包总量 , 物品价值以及 物品的重量, 和 想求的第k 个优解问题, 总的来说 仍是 01 背包,
我们仍可以安装01背包的思路来, 但是 要保存 k 解问题, 即 01 背包 一维数组解最优的问题, 那么我们就可以 用一个二维数组, 来存, 用行来表示背包的容量, 用列表示
第几个k解问题 将结果存到二维数组中, 即可
思路:
1.用两个数组, 分别存 物品放与不放的结果,
2. 扫描 全部的结果, 将二者进行比较 依次放入, 则当 全部扫描完后, 则最终的结果 就是在当前 最开始 第一个for 循环 条件下 的最优解
3. 整个for 循环, 结束 将每次的最优解都存在这个二维数组当中, 就可以 根据 要求 直接输出 结果就可以
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#define max(a,b) ((a)>(b)?(a):(b))
#define N 1010
using namespace std;
int we
;
int v
;
int a
;
int b
;
int dp
[100];
int main()
{
int T,n,m,k,i,j,l,x,y;
while(~scanf("%d",&T))
{
while(T--)
{
memset(dp,0,sizeof(dp));
memset(we,0,sizeof(we));
memset(v,0,sizeof(v));
cin>>n>>m>>k;
for(i=0;i<n;i++)
cin>>v[i];
for(i=0;i<n;i++)
cin>>we[i];
for(i=0;i<n;i++)
{
for(j=m;j>=we[i];j--)
{
for(l=0;l<k;l++)// 先把每一种的放不放的结果 存下来
{
a[l]=dp[j-we[i]][l]+v[i]; //放
b[l]=dp[j][l];//不放
}
a[l]=b[l]=-1;// 目的是 作为结束条件 没有结束条件,wrong 会结束不了
x=y=0;
for(int num=0;num<k&&(x<k||y<k);)
{
dp[j][num]=a[x]>=b[y]? a[x++]:b[y++];
// cout<<dp[j][num]<<endl;
if(dp[j][num]!=dp[j][num-1])
num++;
}
}
}
// for(i=0;i<=m;i++)
// {
// for(j=0;j<=k;j++)
// cout<<dp[i][j]<<" ";
// cout<<endl;
// }
cout<<dp[m][k-1]<<endl;
}
}
return 0;
}
http://acm.hdu.edu.cn/showproblem.php?pid=2639
Bone Collector II
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4693 Accepted Submission(s): 2443
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
题目大意 给 背包总量 , 物品价值以及 物品的重量, 和 想求的第k 个优解问题, 总的来说 仍是 01 背包,
我们仍可以安装01背包的思路来, 但是 要保存 k 解问题, 即 01 背包 一维数组解最优的问题, 那么我们就可以 用一个二维数组, 来存, 用行来表示背包的容量, 用列表示
第几个k解问题 将结果存到二维数组中, 即可
思路:
1.用两个数组, 分别存 物品放与不放的结果,
2. 扫描 全部的结果, 将二者进行比较 依次放入, 则当 全部扫描完后, 则最终的结果 就是在当前 最开始 第一个for 循环 条件下 的最优解
3. 整个for 循环, 结束 将每次的最优解都存在这个二维数组当中, 就可以 根据 要求 直接输出 结果就可以
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#define max(a,b) ((a)>(b)?(a):(b))
#define N 1010
using namespace std;
int we
;
int v
;
int a
;
int b
;
int dp
[100];
int main()
{
int T,n,m,k,i,j,l,x,y;
while(~scanf("%d",&T))
{
while(T--)
{
memset(dp,0,sizeof(dp));
memset(we,0,sizeof(we));
memset(v,0,sizeof(v));
cin>>n>>m>>k;
for(i=0;i<n;i++)
cin>>v[i];
for(i=0;i<n;i++)
cin>>we[i];
for(i=0;i<n;i++)
{
for(j=m;j>=we[i];j--)
{
for(l=0;l<k;l++)// 先把每一种的放不放的结果 存下来
{
a[l]=dp[j-we[i]][l]+v[i]; //放
b[l]=dp[j][l];//不放
}
a[l]=b[l]=-1;// 目的是 作为结束条件 没有结束条件,wrong 会结束不了
x=y=0;
for(int num=0;num<k&&(x<k||y<k);)
{
dp[j][num]=a[x]>=b[y]? a[x++]:b[y++];
// cout<<dp[j][num]<<endl;
if(dp[j][num]!=dp[j][num-1])
num++;
}
}
}
// for(i=0;i<=m;i++)
// {
// for(j=0;j<=k;j++)
// cout<<dp[i][j]<<" ";
// cout<<endl;
// }
cout<<dp[m][k-1]<<endl;
}
}
return 0;
}
相关文章推荐
- hdu2639 Bone Collector II 01背包边形,第k大价值
- HDU 2191 (01背包问题 13.12.02)
- HDU 2639 Bone Collector II(01背包变型)
- HDU 3466 Proud Merchants (01背包排序问题)
- hdu 1574 RP问题 dp 01背包
- hdu 2639 Bone Collector II (dp 01背包求第k优解)
- hdu 2639 第k大01背包
- HDU 1574 RP问题(01背包变形)
- hdu 1574 RP问题 01背包的变形
- 01背包及完全背包问题(51Nod - 1085,HDU 1114 Piggy-Bank)
- HDU 2955 Robberies(经典01背包概率问题)
- HDU 2955 Robberies(01背包的概率问题)
- HDU 2639 Bone Collector II(DP:01背包)
- HDU 2639 01背包求第k大
- hdu 2639 Bone Collector II(01背包求第k优解)
- HDU 2639 第K大背包问题
- 陈老师的多校联合20140811||HDU 3236 ||2009年武汉站G题 01背包问题
- hdu 2602 Bone Collector (01 背包问题)
- 背包问题小总结 习题(动态规划01背包(第k优解)完全背包,多重背包)acm杭电HDU2639,HDU2602,HDU1114,HDU2191
- HDU 2639 Bone Collector II [动态规划 第k大01背包]