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

                 
  • 相关阅读:
    【SQL Server性能优化】SQL Server 2008之表压缩
    SQL Server 锁的排队机制
    通过DAC来连接SQL Server
    通过SQL Server的数据库邮件功能功能发送邮件
    【SQL 编程你也行】BOM按节点排序
    【SQL Server数据迁移】32位的机器:SQL Server中查询ORACLE的数据
    《女孩梦三十》
    在论坛中出现的比较难的sql问题:26(动态行专列+合并字符串、补足行数)
    【SQL Server高可用性】数据库镜像:同一台机器的两个不同实例上配置数据库镜像 + 另一台见证服务器
    【SQL Server高可用性】数据库复制:修改表结构、新增表、新增存储过程 会被复制到订阅服务器?
  • 原文地址:https://www.cnblogs.com/smilesundream/p/6642571.html
Copyright © 2011-2022 走看看