HDU 1171 Big Event in HDU——多重背包+二进制优化

5人阅读 评论(0) 收藏 举报
分类:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, val[100], cnt[100], a[500];
bool dp[250000];
int iabs(int x) { return x > 0 ? x : -x; }
int main() {
    while (~scanf("%d", &n) && n > 0) {
        for (int i = 1; i <= n; i++) scanf("%d%d", &val[i], &cnt[i]);
        int m = 0, sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += cnt[i]*val[i];
            int j = 0;
            while (cnt[i] >= (1<<j)) {
                a[++m] = (1<<j)*val[i];
                cnt[i] -= (1<<j);
                j++;
            }
            if (cnt[i] > 0) a[++m] = cnt[i]*val[i];
        }
        for (int i = 0; i <= sum; i++) dp[i] = false;
        dp[0] = true;
        for (int i = 1; i <= m; i++) {
            for (int j = sum; j >= a[i]; j--) {
                dp[j] = dp[j-a[i]];
            }
        }
        int ans = INF, ansa = 0, ansb = 0;
        for (int i = 0; i <= sum; i++) {
            if (!dp[i]) continue;
            if (iabs(sum-2*i) < ans) {
                ans = iabs(sum-2*i);
                ansa = i, ansb = sum - i;
            }
        }
        if (ansa < ansb) swap(ansa, ansb);
        printf("%d %d\n", ansa, ansb);
    }
    return 0;
}

查看评论

HDU 1171 Big Event in HDU (母函数)

题意:有n样物品,每样物品价值是v,件数是m。尽量把这些物品分成两堆使得两边总价值最接近。输出分得的两堆各自的价值。 利用母函数法来解决,因为分成两堆,而两堆中较小的一堆最大为所有物品总价值量的一半,...
  • IAccepted
  • IAccepted
  • 2014-04-28 16:42:39
  • 2992

杭电1171Big Event in HDU

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...
  • z8110
  • z8110
  • 2015-09-18 18:39:56
  • 300

HDU 1171 Big Event in HDU(母函数||DP)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...
  • jk13171217
  • jk13171217
  • 2014-07-30 16:43:31
  • 660

hdoj 1171 Big Event in HDU 【母函数】【01背包】

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others...
  • chenzhenyu123456
  • chenzhenyu123456
  • 2015-04-22 22:17:57
  • 530

Big Event in HDU(杭电1171)(多重背包)和(母函数)两种解法

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others...
  • hdd871532887
  • hdd871532887
  • 2014-11-19 20:35:31
  • 638

杭电ACM hdu 1171 Big Event in HDU 解题报告(母函数)

出处: Problem Description Nowadays, we all know that Computer College is the biggest department in HDU...
  • u012077163
  • u012077163
  • 2013-11-30 16:36:15
  • 2106

hdu 1171 Big Event in HDU 母函数解法。。以后再来用背包A一下

Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu...
  • Lionel_D
  • Lionel_D
  • 2015-03-04 16:50:30
  • 654

hdu2844 Coins(多重背包+二进制优化)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2844 题目大意: 有n种价值不同的硬币,他们的数量有限但不相同。现在要求出硬币组合,可组合出多少种...
  • aaaaacmer
  • aaaaacmer
  • 2015-09-18 14:10:08
  • 831

hdu1171Big Event in HDU(母函数)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...
  • bokzmm
  • bokzmm
  • 2015-12-09 19:22:23
  • 152

杭电1171 Big Event in HDU(母函数+多重背包解法)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...
  • u013634213
  • u013634213
  • 2014-08-24 02:02:51
  • 974
    个人资料
    持之以恒
    等级:
    访问量: 6万+
    积分: 5862
    排名: 5465
    最新评论