zoukankan      html  css  js  c++  java
  • Bone Collector II

    Bone Collector II
    Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3042 Accepted Submission(s): 1578

    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大的值,DP[i][j][k]表示放i件物品体积为V的时候第K大的值

    for(int j=V;j>=v[i];j--)
    {
        for(int s=1;s<=k;s++)
        {
            A[top++]=Dp[j-v[i]][s]+w[i];
            A[top++]=Dp[j][s];  
        }
    }

    表示将体积为V时,所有的情况,从中选出前K大的值,对于放每件物品所达到的体积都选出前K大的值,一直贪心到将所有的物品都放完,得到的DP[n][v][k]就是所求的.

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <queue>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int MAX =  1100;
    int Dp[MAX][35];
    int w[110],V[110];
    int A[35];
    int B[35];
    int main()
    {
        int T;
        int n,v,k;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d %d %d",&n,&v,&k);
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&w[i]);
            }
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&V[i]);
            }
            memset(Dp,0,sizeof(Dp));
            for(int i=1;i<=n;i++)//转化为01背包减少时间复杂度
            {
                for(int j=v;j>=V[i];j--)
                {
                    int s;
                    for( s=1;s<=k;s++)
                    {
                        A[s]=Dp[j-V[i]][s]+w[i];
                        B[s]=Dp[j][s];
                    }
                    A[s]=-1;
                    B[s]=-1;
                    int a=1,b=1;
                    for(s=1;s<=k&&(A[a]!=-1||B[b]!=-1);)//采用归并的方式,也可以用优先队列
                    {
                        if(A[a]>B[b])
                        {
                            Dp[j][s]=A[a];
                            a++;
                        }
                        else
                        {
                            Dp[j][s]=B[b];
                            b++;
                        }
                        if(Dp[j][s]!=Dp[j][s-1])
                        {
                            s++;
                        }
                    }
                }
            }
            printf("%d
    ",Dp[v][k]);
        }
        return 0;
    }
    
  • 相关阅读:
    Sencha touch 初体验
    Android开发——NDK开发入门
    Android开发——Activity(活动)的生命周期(上)
    Android开发——构建自定义组件【转】
    Android开发——使用Gallery实现“多级联动”
    Android开发——Android搜索框架(一)
    LINQ之路 6:延迟执行(Deferred Execution)
    .Net小笔记之.NET Framework的内容
    c#高级编程学习笔记之创建一个简化的链表类
    C#高级编程读书笔记之泛型的优点
  • 原文地址:https://www.cnblogs.com/juechen/p/5255977.html
Copyright © 2011-2022 走看看