Big Event in HDU

题目链接

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

题意

有n个物品,给出他们的价值和对应的数量,把它们分成两部分,使他们的价值尽可能接近。

分析

很明显的用动态规划,但是这两天一直在做母函数的题,发现母函数也能解,提交正确以后好像发现了新世界,

用母函数求出最后的多项式后,他们的每一项的次数都是可能出现的价值,然后找出最接近的两个值。

代码

#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n;
    while (scanf("%d", &n))
    {
        if (n < 0)break;
        vector<int>sum, value;
        int sumValue = 0;
        for (int i = 0; i < n; i++)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            value.push_back(a);
            sum.push_back(b);
            sumValue += a * b;
        }
        int c1[300000] = {0}, c2[300000] = {0};
        for (int i = 0; i <= sum[0]*value[0]; i += value[0])
            c1[i] = 1;
        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j <= sumValue; j++)
                for (int k = 0; k + j <= sumValue && k <= sum[i]*value[i]; k += value[i])
                    c2[k + j] += c1[j];
            for (int j = 0; j <= sumValue; j++)
            {
                c1[j] = c2[j];
                c2[j] = 0;
            }
        }
        vector<int>v;
        for (int i = 0; i <= sumValue; i++)
            if (c1[i] != 0)
            {
                v.push_back(i);
            }

        int len = v.size();
        if (len % 2 == 1)
            printf("%d %d\n", v[len / 2], v[len / 2]);
        else
            printf("%d %d\n", v[len / 2], v[len / 2 - 1]);//价值大的先输出来 

    }

    return 0;
}


posted @ 2017-04-14 12:39  -梦里不知身是客  阅读(196)  评论(0编辑  收藏  举报