01背包(第k优解)
Bone Collector II
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7165 Accepted Submission(s): 3802
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.
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.
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
2
0
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int dp[][];
int val[] , vol[]; int main()
{
int t ;
scanf("%d" , &t);
while(t--)
{
int n , v , kk , a[] , b[];
scanf("%d%d%d" , &n , &v , &kk);
memset(dp , , sizeof(dp));
for(int i = ; i <= n ; i++)
{
scanf("%d" , &val[i]);
}
for(int i = ; i <= n ; i++)
{
scanf("%d" , &vol[i]);
}
for(int i = ; i <= n ; i++)
{
for(int j = v ; j >= vol[i] ; j--)
{
for(int k = ; k <= kk ; k++)//将所有解存起来
{
a[k] = dp[j][k];
b[k] = dp[j-vol[i]][k] + val[i] ;
}
// 对所有解进行排序
a[kk+] = - ;
b[kk+] = - ;
int k = ;
int x = , y = ;
while(k <= kk && (a[x] != - || b[y] != -))
{
if(a[x] > b[y])
{
dp[j][k] = a[x];
x++ ;
}
else
{
dp[j][k] = b[y];
y++;
}
if(dp[j][k] != dp[j][k-]) k++ ;
}
}
}
cout << dp[v][kk] << endl ; } return ;
}
01背包(第k优解)的更多相关文章
- HDU 2639 (01背包第k优解)
/* 01背包第k优解问题 f[i][j][k] 前i个物品体积为j的第k优解 对于每次的ij状态 记下之前的两种状态 i-1 j-w[i] (选i) i-1 j (不选i) 分别k个 然后归并排序并 ...
- (01背包 第k优解) Bone Collector II(hdu 2639)
http://acm.hdu.edu.cn/showproblem.php?pid=2639 Problem Description The title of this problem i ...
- HDU 3639 Bone Collector II(01背包第K优解)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 杭电 2639 Bone Collector II【01背包第k优解】
解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[ ...
- hdu2639 01背包第K优解
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...
- 01背包-第k优解
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...
- HDU2639Bone Collector II[01背包第k优值]
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 2639 背包第k优解
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 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 (01背包,求第k优解)
这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...
随机推荐
- Jmeter性能测试结果分析:响应时间为什么是下降的趋势?
测试图数据库:边的插入,递增并发量,6000并发平均响应时间比7000的并发的平均响应时间还要大? 7000并发的99%用户响应时间是70.99,平均响应时间怎么就是38.59了? 一共两 ...
- java: Set类及子类:TreeSet有序子类,HashSet无序子类:重复元素
Set类及子类: TreeSet有序子类: HashSet无序(散列)子类 HashSet子类的内容是没有顺序的,单个元素也不会重复的(对象除外). Set<String> allSet ...
- man da'te
DATE(1) 用户命令 DATE(1) 名称 日期-打印或设置系统日期和时间 简介 date [OPTION]... ...
- LeetCode--105--从前序与中序遍历序列构造二叉树(python)
根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,2 ...
- darknet-yolov3模型预测框size不正确的原因
问题描述:预测框的中心位置正常,但是预测的框的width和height不正常. 解决方法:使得训练的配置cfg和测试中cfg的输入width, height, anchorbox保持一致! 问题是我在 ...
- MySQL两个时间相减
SELECT TIMESTAMPDIFF(MONTH,'2009-10-01','2009-09-01'); interval可是: SECOND 秒 SECONDS MINUTE 分钟 MINUTE ...
- WWW基本概念
1.Internet 2.Intranet 3.万维网 注:万维网不等同于因特网,它只是因特网的一项服务. 4.TCP/IP 5.HTTP 注:HTTP是运行在应用层的一项服务. 注:服务器在没有用户 ...
- poj 3613Cow Relays
Description For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a rel ...
- Leetcode 3. Longest Substring Without Repeating Characters(string 用法 水题)
3. Longest Substring Without Repeating Characters Medium Given a string, find the length of the long ...
- Qt中图元对象的多重集成
在继承自定义QGraphicsItem图元对象时,有时需要用到信号/槽机制,由于QGraphicsItem非QObject的子类 所以需要多重继承QObject,有一点需要特别注意:就是继承的顺序,一 ...