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]);    }}
阅读全文
1 0
原创粉丝点击