跳转至

HDU-1171 Big Event in HDU(单调队列优化背包DP)

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

题意是一些设备,存在数量限制,不同的设备价值不同,需要将这些设备尽可能地平分,但是要保证A不小于B。可以在处理输入地时候计算出总的价值,然后除以2当作背包,也就是在一半地价值下所能装的最大价值。这道题和以往不一样是没给出物品地重量,其实可以认为设备地价值和重量是一样地。

对于数据地估计就是50\times 50 \times 100,多重背包,利用单调队列进行优化,时间复杂度在6.25\times 10^6,肯定不会超时。

另外一个坑点是输入n是负数的时候停止,而不是-1,测试用例很具有迷惑性。

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>

using namespace std;

int n, m;
vector<int> weight(55), num(55);
vector<int> dq_pos(250005), dq_val(250005), d(250005);

int multiPack()
{
    for (int i = 0; i < n; ++i) {
        for (int a = 0; a < weight[i]; ++a) {
            int start = 0, end = 0;
            for (int j = 0; j * weight[i] + a <= m; ++j) {
                int tmp = d[j * weight[i] + a] - j * weight[i];
                while (start < end && dq_val[end - 1] <= tmp) --end;
                dq_pos[end] = j;
                dq_val[end++] = tmp;
                d[j * weight[i] + a] = dq_val[start] + j * weight[i];
                if (dq_pos[start] == j - num[i]) ++start;
            }
        }
    }

    return d[m];
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    while ((cin >> n) && n > 0) {
        int sum = 0;
        for (int i = 0; i < n; ++i) {
            cin >> weight[i] >> num[i];
            sum += weight[i] * num[i];
        }
        m = sum / 2;
        int res = multiPack();
        cout << (sum - res) << ' ' << res << endl;
        fill(d.begin(), d.end(), 0);
    }

    return 0;
}