zoukankan      html  css  js  c++  java
  • dp--hdu1171(01背包)

    hdu1171

    题目

    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
    

    题目大意

    给一个T,表示下面有T行,文件直到T<0时结束。每行两个数w,n,表示有n个价值为w的物品。要求将这所有的物品尽量均分,即分完差值最小,输出分成两堆后,各堆的价值

    思路

    将总价值的一半设为背包的容量,那这是一道简单的01背包问题

    代码

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <cmath>
    #include <sstream>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <vector>
    #include <queue>
    #include <iomanip>
    #include <stack>
    
    using namespace std;
    
    typedef long long LL;
    const int INF = 0x3f3f3f3f;
    const int MAXN = 200005;
    const int MOD = 1e9;
    
    int main()
    {
        int T;
        while(cin >> T && T > 0)
        {
            int dp[MAXN], w[MAXN];
            memset(dp, 0, sizeof(dp));
            int num = 0, N = 0, V = 0;
            for(int i = 0;i < T;++i)
            {
                int a, b;
                cin >> a >> b;
                N += b;
                V += a * b;
                while(b--)
                    w[++num] = a;
            }
            for(int i = 1;i <= N;++i)
                for(int j = V / 2;j >= w[i];--j)
                dp[j] = max(dp[j], dp[j - w[i]] + w[i]);
            cout << V - dp[V / 2] << " " << dp[V / 2] << endl;
        }
        return 0;
    }
    
    
  • 相关阅读:
    Git本地仓库push至GitHub远程仓库每次输入账户密码问题解决(亲测可行)
    Laravel5.5+ 区分前后端用户登录
    word 中Sentences、Paragraph等含义和用法
    Word转图片word
    Word文档编号工具,Word标题,图、表手动编号工具
    Word电子扫描器 Word文档转换为图片Pdf,Word文档扫描成Pdf工具
    如何用vba给一个word表格的最后插入一行
    PPT电子扫描仪 ppt转换为图片Pdf工具
    Word文档只读加密工具
    在c#应用程序中使用IrisSkin2.dll美化界面
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/10287788.html
Copyright © 2011-2022 走看看