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;
        }
    }










  • 相关阅读:
    重新排列数组
    一维数组的动态和
    aiohttp实现爬虫功能
    python django项目创建及前期准备(使用pycharm)
    vue 使用高德开放平台获取经纬度
    git切换分支时,该分支的修改被带到另一个分支
    vue刻度尺组件
    js将PDF转为base64格式,并在将base64格式PDF回显在页面中
    Linux系统(Centos)安装tomcat和部署Web项目
    linux下安装 tomcat 和配置防火墙开放8080端口
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/3526006.html
Copyright © 2011-2022 走看看