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

                 
  • 相关阅读:
    jqueryUI弹出框问题
    spring data jpa分页
    解决eclipse编辑js和html卡的问题
    web.xml添加编码过滤器
    Apache SolrCloud安装
    搭建zookeeper集群
    html页面读取PDF小案例
    .NET 使用Process调用7_zip解压文件
    .NET 中三种正确的单例写法
    Git 笔记
  • 原文地址:https://www.cnblogs.com/smilesundream/p/6642571.html
Copyright © 2011-2022 走看看