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

    Big Event in HDU

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


    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
     题意:给定一堆物品,每个物品都有一个价值,且不可分割,现在分成两堆,请使得两堆价值差最小,如果价值差不能为0,则需保证第一堆价值>=第二堆。
    模型:0-1背包问题,,求得物品总价值SUM后,可以将问题转换成两个船,其中一个船最大载重sum/2,
    现在求该船最大载重,,即是第二堆物体重量,问题得证
    核心算法 dp[j]=max(dp[j],dp[j-a[i]]+a[i]);dp[j]表示容量j的情况下最大载重,要注意逆序运算
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int a[5001],dp[1250001];
    int main()
    {
        int n;
        while(~scanf("%d",&n)&&n>0)
        {
            int sum=0,p=0,val,num;
            for(int i=1;i<=n;i++)
            {
                cin>>val>>num;
                for(int j=1;j<=num;j++)
                    a[++p]=val;
                sum+=val*num;
            }
            memset(dp,0,sizeof(dp));
            for(int i=1;i<=p;i++)
                for(int j=sum/2;j>=a[i];j--)
                   dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
            cout<<sum-dp[sum/2]<<" "<<dp[sum/2]<<endl;
        }
        return 0;
    }

                 
  • 相关阅读:
    用rpm安装软件的常用步骤
    将应用发布到WasLiberty的两种方法
    安装 ibm-java-x86_64-sdk-6.0-9.3.x86_64.rpm 的三步骤
    人是科技的第一生产力。不重视人的价值,不尊重人的需求,不解放人的生产力,必将被互联网时代快速淘汰。
    Java保存简单偏好的类
    实用快捷键Win+L=锁屏
    判断一件事有无技术含量的标准
    主动去平事 别等事找人
    看了某些蛊惑人心的招聘广告,实在忍不住想要提醒那些跃跃欲奉献的后生们
    ubuntu16.04在英文状态下安装中文语言包的过程(法二:命令行的方式)
  • 原文地址:https://www.cnblogs.com/smilesundream/p/6642571.html
Copyright © 2011-2022 走看看