zoukankan      html  css  js  c++  java
  • hdu(1171)多重背包

    hdu(1171)

    Big Event in HDU

    Time Limit:
    10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 19752    Accepted Submission(s): 6900
    

    Problem Description
    Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
    The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
     

    Input
    Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
    A test case starting with a negative integer terminates input and this test case is not to be processed.
     

    Output
    For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
     

    Sample Input
    2 10 1 20 1 3 10 1 20 2 30 1 -1
     

    Sample Output
    20 10 40 40
    分析:多重背包问题,先计算总价值sum,然后取其一半V=sum/2;剩下的就是多重背包问题了
    程序:
    #include"stdio.h"
    #include"string.h"
    int f[250005];
    int p[100],h[100];
    int max(int a,int b)
    {
        return a>b?a:b;
    }
    int main()
    {
        int n,i,j,k,sum,V;
        while(scanf("%d",&n),n>=0)
        {
            sum=0;
            for(i=1;i<=n;i++)
            {
                scanf("%d%d",&p[i],&h[i]);
                sum+=p[i]*h[i];
            }
            V=sum/2;
            for(i=0;i<=V;i++)
                f[i]=0;
            for(i=1;i<=n;i++)
            {
                for(j=V;j>=p[i];j--)
                {
                    for(k=0;k<=h[i]&&k*p[i]<=j;k++)
                        f[j]=f[j]>f[j-k*p[i]]+k*p[i]?f[j]:f[j-k*p[i]]+k*p[i];
    
                }
            }
            printf("%d %d
    ",sum-f[V],f[V]);
        }
        return 0;
    }
    
    运行时间2309ms
    还有一种可快速的方法就是利用二进制混合背包:
    程序:
    #include"string.h"
    #include"stdio.h"
    int f[250020],V;
    void zopack(int v,int p)
    {
        int i;
        for(i=V;i>=v;i--)
            f[i]=f[i]>f[i-v]+p?f[i]:f[i-v]+p;
    }
    void allpack(int v,int p)
    {
        int i;
        for(i=v;i<=V;i++)
           f[i]=f[i]>f[i-v]+p?f[i]:f[i-v]+p;
    
    }
    void multiplepack(int v,int p,int h)
    {
        if(v*h>=V)
        {
            allpack(v,p);
            return;
        }
        int k=1;
        while(k<h)
        {
            zopack(k*v,k*p);
            h-=k;
            k<<=1;
        }
        if(h>0)
        {
            zopack(h*v,h*p);
        }
    }
    int main()
    {
        int n,i,v[111],h[111];
        while(scanf("%d",&n),n>=0)
        {
            int sum=0;
            for(i=0;i<n;i++)
            {
                scanf("%d%d",&v[i],&h[i]);
                sum+=v[i]*h[i];
            }
            V=sum/2;
            for(i=0;i<=V;i++)
                f[i]=-999;
            f[0]=0;
            for(i=0;i<n;i++)
            {
                multiplepack(v[i],v[i],h[i]);
            }
            int ans=-1;
            for(i=V;i>=0;i--)
            {
                if(f[i]>=0)
                {
                    ans=f[i];
                    break;
                }
            }
            printf("%d %d
    ",sum-ans,ans);
        }
        return 0;
    }

    时间只用31~64ms
  • 相关阅读:
    NSArray的几个方法的简单使用
    内存管理常用知识点总结
    iOS是开发之 对数组中的元素排序后输出
    iOS开发之——单例的几种设计方式
    图论二:图的存储
    图论一:基本概念
    树状数组(hdu-4325,hdu-1166,pat-1057)
    pat1079+1086+1090+1094(树的遍历)感想
    pat 甲级 1086(树的遍历||建树)
    static关键字的功能
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348270.html
Copyright © 2011-2022 走看看