Bone Collector IITime
limit:5000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2948 Accepted Submission (s): 1526
Problem DescriptionThe Title of this problem be familiar,isn ' t it?yeah,if you had took part in the ' Rookie Cup ' competitio N,you must has seem this title. If you haven ' t seen it before,it doesn ' t matter,i would give you a link:
The link:http://acm.hdu.edu.cn/showproblem.php?pid=2602
Today We is not desiring the maximum value of bones,but the k-th maximum value of the bones. NOTICE That,we considerate and ways that get the same value of bones is the same. That means,it'll be a strictly decreasing sequence from the 1st maximum, 2nd maximum. To the k-th maximum.
If the total number of different values are less than k,just ouput 0.
Inputthe first line contain a integer T, the number of cases.
Followed by T cases, each case three lines, the first line contain both integer n, v, K (n <=, v <=, K & lt;=) 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 for each bone.
Outputone integer per line representing the k-th maximum of the total value (this number would 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
It's probably the value of the K-more that can be loaded.
The DP array adds one dimension as the value of the number L
DP[J][L] dp[j-vo[i]][l] + va[i] Use arrays A and b to put together to determine the size of the other 01 backpacks
#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 1011;int va [MAXN], Vo[maxn];int N, V, K;int dp[maxn][55];int a[55], B[55];int main () {int t; scanf ("%d", &t); while (t--) {scanf ("%d%d%d", &n, &v, &k); for (int i = 1; I <= n; ++i) {scanf ("%d", &va[i]); } for (int i = 1; I <= n; ++i) {scanf ("%d", &vo[i]); } memset (DP, 0, sizeof (DP)); for (int i = 1, i <= N; ++i) {for (int j = v; J >= Vo[i],--j) {for (int l = 1; l <= K; + +) L) {A[l] = dp[j][l]; B[L] = Dp[j-vo[i]][l] + va[i]; } a[k+1] = b[k+1] =-1; int x = 1, y = 1, w = 1; while (W <= k && (x <= k | | y <= k)) {if (a[x] > B[y]) {dp[j][ W] = a[x++]; } else{dp[j][w] = b[y++]; } if (w = = 1 | | dp[j][w]! = Dp[j][w-1]) {++w; }}}} printf ("%d\n", Dp[v][k]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"HDU2639" Bone Collector II (01 backpack K-Optimal solution)