Bone Collector II
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
Author
teddy
Source
题目类型:01背包第k优解
算法分析:dp[i][j]表示恰好填满体积为i的背包的第j优解(value),每次递推的时候把两个状态的前i个最优解合并起来即可,最后输出dp[V][K]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
/************************************************* Author :supermaker Created Time :2015/12/3 14:36:11 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int dp[1666][36], c[1666], w[1666], ta[36], tb[36], N, V, K; void Kth_ZeroOnePack (int c, int w) { for (int i = V; i >= c; i--) { for (int k = 1; k <= K; k++) { ta[k] = dp[i][k]; tb[k] = dp[i-c][k] + w; } ta[K+1] = tb[K+1] = -1; for (int j = 1, k = 1, q = 1; j <= K && (ta[k] != -1 || tb[q] != -1); ) { if (ta[k] >= tb[q]) dp[i][j] = ta[k++]; else dp[i][j] = tb[q++]; if (dp[i][j] != dp[i][j-1]) j++; } } } int main() { //CFF; //CPPFF; int t; scanf ("%d", &t); while (t--) { scanf ("%d%d%d", &N, &V, &K); for (int i = 1; i <= N; i++) scanf ("%d", &w[i]); for (int i = 1; i <= N; i++) scanf ("%d", &c[i]); memset (dp, 0, sizeof (dp)); for (int i = 1; i <= N; i++) Kth_ZeroOnePack (c[i], w[i]); printf ("%d\n", dp[V][K]); } return 0; } |
- « 上一篇:hdu5115
- hdu3652:下一篇 »