• 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;
    }
    
    
  • 相关阅读:
    解析 AJAX 返回回来的 xml字符串
    JS 与 后台如何获取 Cookies
    鼠标上下滚轮事件
    MVC Control 返回各种数据
    ildasm 查看程序集 里面的图标的意思
    对象的序列化和反序列化 itprobie
    文件上传通用类 itprobie
    文件下载的四种方式 itprobie
    委托事件的实际运用 itprobie
    使用NPOI实现excel的导入导出 itprobie
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/10287788.html
Copyright © 2020-2023  润新知