• HUD 2639 Bone Collector II


    Bone Collector II

    Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 5463    Accepted Submission(s): 2880


    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
     
     
    题目的意思就是求01背包的第k优解,则自然想到(我感觉一点都不自然)多一维,dp【j】【k】;
    状态dp【j】的前k个最优解,都是由dp[j][1....k]和dp[j-w[i]][1.....k]+v[i]转移过来(没有证明过,但是对的),可以用优先队列来维护。
    在求解dp[j][k]时,我们首先把dp[j][1....k]和dp[j-w[i]][1.....k]+v[i]统统放进优先队列(会自己从大到小排),然后我们依次拿出k个,放进dp[j][1.....k]就ok了,但是要避免重复。
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    using namespace std;
    int main()
    {
        int T;
        int dp[1005][35];
        cin >> T;
        priority_queue<int>q;//默认从大到小排
        while (T--)
        {
            memset(dp, 0, sizeof(dp));
            int n, vv, kk;
            cin >> n >> vv >> kk;
            int i, j, k;
            int v[105], w[105];
            for (i = 1; i <= n; i++)
                cin >> v[i];
            for (i = 1; i <= n; i++)
                cin >> w[i];
            for (i = 1; i <= n; i++)
            {
                for (j = vv; j >= w[i]; j--)//01背包的循环
                {
                    while (!q.empty()) q.pop();
                    for (k = 1; k <= kk; k++)
                    {//dp[j][1....k]和dp[j-w[i]][1.....k]+v[i]放进队列
                        q.push(dp[j][k]);
                        q.push(dp[j - w[i]][k] + v[i]);
                    }
                     k = 1;
                    while (1)
                    {
                        if (q.empty() || k == kk+1) break;
                        if (k > 1 && q.top() != dp[j][k-1])
                        {//这一步避免重复, q.top() == dp[j][k-1]要排除
                            dp[j][k] = q.top(); k++;
                        }
                        else if (k == 1)
                        {
                            dp[j][k] = q.top(); k++;
                        }
                        q.pop();
                    }
                }
            }
            cout << dp[vv][kk] << endl;
        }
        return 0;
    
    }
     
     
  • 相关阅读:
    KAFKA基础(四):Kafka架构深入(2)Kafka 生产者
    逆向工程,调试Hello World !程序(更新中)
    关于网络初级基础知识总结
    真!阿里P10整理的Java面试宝典,学了60%成功砍下了P8级Offer,真爽!
    【专升本计算机】甘肃省2020年专升本计算机测试题
    真!阿里P10整理的Java面试宝典,学了60%成功砍下了P8级Offer,真爽!
    python利用numpy存取文件
    泛在网技术考点整理
    [Python图像处理] 三十二.傅里叶变换(图像去噪)与霍夫变换(特征识别)万字详细总结
    汉诺塔问题
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271310.html
Copyright © 2020-2023  润新知