• HDU2639(01背包第K大)


    Bone Collector II

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


    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]为容量为j的背包所获得的第k大价值。在01背包中 状态转移方程为 dp[j]=max(dp[j],dp[j-w[i]])+v[i],这个求的是第1大。我们用dp[j][1...k]表示第1大到第k大。
    那么dp[j][1]=max_1th(dp[j][1],dp[j-w[i]]+v[i]),dp[j][2]=max_2th(dp[j][1],dp[j-w[i]][1],dp[j][2],dp[j-w[i]][2]+v[i])( 注意:不是dp[j][2]=max(dp[j][2],dp[j-w[i][2]+v[i]) )
    dp[j][k]=max_kth(dp[j][1],...,dp[j][k],dp[j-w[i]][1]+v[i],...,dp[j-w[i]][k]+v[i])。
    /*
        Accepted    2639    858MS    5372K    831 B    G++    
    */
    #include"cstdio"
    #include"cstring"
    #include"algorithm"
    using namespace std;
    const int MAXN=1005;
    int dp[MAXN][MAXN];
    int n,W,K;
    int v[MAXN],w[MAXN];
    int vec[MAXN],cnt;
    bool comp(int x,int y)
    {
        return  x > y;
    }
    void KthZeroOnePack()
    {
        for(int i=0;i<n;i++)
        {
            for(int j=W;j>=w[i];j--)
            {
                cnt=0;
                for(int th=1;th<=K;th++)
                {
                    vec[cnt++]=dp[j][th];
                    vec[cnt++]=dp[j-w[i]][th]+v[i];
                }
                sort(vec,vec+cnt,comp);
                cnt=unique(vec,vec+cnt)-vec;
                for(int th=1;th<=min(cnt,K);th++)    dp[j][th]=vec[th-1];
            }
        }
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d%d",&n,&W,&K);
            memset(dp,0,sizeof(dp));
            for(int i=0;i<n;i++)    scanf("%d",&v[i]);
            for(int i=0;i<n;i++)    scanf("%d",&w[i]);
            KthZeroOnePack();
            printf("%d
    ",dp[W][K]);
        }
        return 0;
    }

    上面用了STL里的sort函数速度较慢...

     因为dp[j][1]...dp[j][k]与dp[j-w[i]][1]+v[i]...dp[j-w[i]][k]+v[i]是依次递减的,那么我们可以用两个数组将这两组数组保存起来,再O(N)的时间内求得第K大。

    /*
        Accepted    2639    171MS    5372K    966 B    G++
    */
    #include"cstdio"
    #include"cstring"
    #include"algorithm"
    #include"queue"
    using namespace std;
    const int MAXN=1005;
    int dp[MAXN][MAXN];
    int n,W,K;
    int v[MAXN],w[MAXN];
    int s1[MAXN],s2[MAXN];
    void KthZeroOnePack()
    {
        for(int i=0;i<n;i++)
        {
            for(int j=W;j>=w[i];j--)
            {
                for(int th=1;th<=K;th++)
                {
                    s1[th-1]=dp[j][th];
                    s2[th-1]=dp[j-w[i]][th]+v[i];
                }
                s1[K]=s2[K]=-1;
                int cnt=1;
                int cnt1=0,cnt2=0;
                while(cnt<=K&&(s1[cnt1]!=-1||s2[cnt2]!=-1))
                {
                    if(s1[cnt1]>s2[cnt2])    dp[j][cnt]=s1[cnt1++];
                    else    dp[j][cnt]=s2[cnt2++];
                    if(dp[j][cnt]!=dp[j][cnt-1])    cnt++;
                }
            }
        }
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d%d",&n,&W,&K);
            memset(dp,0,sizeof(dp));
            for(int i=0;i<n;i++)    scanf("%d",&v[i]);
            for(int i=0;i<n;i++)    scanf("%d",&w[i]);
            KthZeroOnePack();
            printf("%d
    ",dp[W][K]);
        }
        return 0;
    }
  • 相关阅读:
    2015抢票记事
    Provide your license server administrator with the following information.error code =-42,147
    微信支付现金红包接口
    SQL Server 触发器
    SQL增删查改注意的事项
    SQL while循环
    SQL SERVER 中is null 和 is not null 将会导致索引失效吗?
    HTML中head里的内容经浏览器解析后全到body里了
    sqLSERVER 计划缓存
    通用分页存储过程
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5187676.html
Copyright © 2020-2023  润新知