zoukankan      html  css  js  c++  java
  • Big Event in HDU

    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.
     
    SampleInput
    2
    10 1
    20 1
    3
    10 1 
    20 2
    30 1
    -1
     
    SampleOutput
    20 10
    40 40、


    所有的物品的总价值是all,则当找到小于all/2的最大的分法时,就得出了答案,也就转换成了一个背包问题。但是我以前看的背包问题是填充一个表,但是在这里这个表太大了,然后看了别人的一些东西,
    原来还可以这样来填充,不需要填充整个表。。。。我好水啊!!!



    #include<iostream>
    using namespace std;
    int max(int a,int b)
    {
        if(a>b) return a;
        else return b;
    }
    void main()
    {
        int n;
        int p,q;
        int a[5000];
        int b[125000];
        int all;
        while(cin>>n)
        {
            if(n<0) break;
            int num=0;
            all=0;
            for(int i=0;i<n;i++)
            {
                cin>>p>>q;
                for(int j=0;j<q;j++)
                {
                    a[num]=p;
                    all+=p;
                    num++;
                }
            }
            for(i=0;i<all/2;i++)
                b[i]=0;
            for(i=0;i<num;i++)
            {
                for(int j=all/2;j>=a[i];j--)
                {
                    b[j]=max(b[j],b[j-a[i]]+a[i]);
                }
            }
            cout<<all-b[all/2]<<' '<<b[all/2]<<endl;
        }
    }










  • 相关阅读:
    第一次作业
    java基础随笔09
    java基础随笔08
    java基础随笔07
    java基础随笔06
    java基础随笔05
    java基础随笔04
    java基础随笔03
    java基础随笔02
    java基础随笔01
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/3526006.html
Copyright © 2011-2022 走看看