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;
    }
    
  • 相关阅读:
    C#算法,二叉树,单链表,反向链表,stack栈
    系统架构师学习笔记_第十一章(上)_连载
    系统架构师学习笔记_第四章(下)_连载
    [项目过程中所遇到的各种问题记录]ORM篇——使用NHibernate配置对象实体的一些小问题
    含HTML标记的内容分页 (C#)
    PowerShell在SharePoint 2010自动化部署中的应用(1)代码获取
    走向ASP.NET架构设计第一章:走向设计
    asp.net 遍历XML文件节点信息
    系统架构师学习笔记_第一章_连载
    AOP 你想干什么 IOC 你服务什么
  • 原文地址:https://www.cnblogs.com/juechen/p/5255977.html
Copyright © 2011-2022 走看看