• (01背包 第k优解) Bone Collector II(hdu 2639)


     
     
     
    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
     
    思路:
    对于求最优解的情况,我们对每一种状态只保存了该状态下的最优解,忽略了其他解,进而实现状态之间的转移,而对于求第K优解的情况呢?其实只需要保存每一种状态下的前K优解,从这K个状态进行状态间的转移,同时去重,保存当前状态的K优解即可。(感觉时间复杂度还是挺高的)
     
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <map>
    #include <algorithm>
    using namespace std;
    
    const int N = 6600;
    const int INF = 0x3fffffff;
    const long long MOD = 1000000007;
    typedef long long LL;
    #define met(a,b) (memset(a,b,sizeof(a)))
    
    int dp[N][35];
    int a[N], b[N], c[N];
    ///dp[j][k] 代表容量为 j 的背包的第 k+1 优解
    
    int cmp(int a, int b)
    {
        return a > b;
    }
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            int i, j, k, n, v;
    
            scanf("%d%d%d", &n, &v, &k);
    
            met(a, 0);
            met(b, 0);
            met(dp, 0);
    
            for(i=1; i<=n; i++)
                scanf("%d", &a[i]);
            for(i=1; i<=n; i++)
                scanf("%d", &b[i]);
    
            for(i=1; i<=n; i++)
            {
                for(j=v; j>=b[i]; j--)
                {
                    int w = 0;
                    for(int z=0; z<k; z++) ///每次只需考虑前 k 优解的状态转换即可
                    {
                        c[w++] = dp[j][z];
                        c[w++] = dp[j-b[i]][z]+a[i];
                    }
    
                    sort(c, c+w, cmp);
                    w = unique(c, c+w) - c; 
                    for(int t=0; t<k && t<w; t++) ///t的范围, 既不能大于 k,也不能大于 w
                        dp[j][t] = c[t];
                }
            }
    
            printf("%d
    ", dp[v][k-1]);
    
        }
        return 0;
    }
  • 相关阅读:
    团队项目----数据库SQL语句学习总结与实践
    团队项目----德州扑克数据库设计之改进版
    期末项目《员工考勤管理系统》
    Applet
    Json
    study of javaserver faces lifecycle
    Session
    xml的用途,定义,原理,以及前景
    互联网应用于企业级应用的区别
    JavaEE体系架构
  • 原文地址:https://www.cnblogs.com/YY56/p/5476797.html
Copyright © 2020-2023  润新知