[HDOJ1171]Big Event in HDU(01背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1171

许多有价值的物品,有重复。问如何将他们分成两堆,使两堆价值之差最小。

对价值求和,转换成01背包,做一次,相当于一堆选物品使得最接近一半。然后这个结果和用价值和作差的结果就是两堆的价值,此时价值只差最小。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19
20 using namespace std;
21
22 const int maxn = 555555;
23 int n;
24 int v[5555555];
25 int m;
26 int dp[maxn];
27
28 int main() {
29     // freopen("in", "r", stdin);
30     int vv, mm;
31     while(~scanf("%d", &n) && n >= 0) {
32         memset(dp, 0, sizeof(dp));
33         memset(v, 0, sizeof(v));
34         m = 1;
35         int half = 0;
36         for(int i = 0; i < n; i++) {
37             scanf("%d %d", &vv, &mm);
38             half += vv * mm;
39             while(mm--) v[m++] = vv;
40         }
41         for(int i = 1; i <= m; i++) {
42             for(int j = half / 2; j >= v[i]; j--) {
43                 dp[j] = max(dp[j], dp[j-v[i]]+v[i]);
44             }
45         }
46         printf("%d %d\n", half-dp[half/2], dp[half/2]);
47     }
48     return 0;
49 }
时间: 2024-08-28 20:39:14

[HDOJ1171]Big Event in HDU(01背包)的相关文章

(hdu step 3.3.1)Big Event in HDU(01背包:N件物品放在容量为V的背包中,第i件物品的费用是c[i],价值是w[i]。问所能获取的最大价值)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 854 Accepted Submission(s): 345 Problem Description Nowadays, we all know that Computer College is the biggest department in HDU.

HDU 1171 Big Event in HDU(01背包)

题目地址:HDU 1171 还是水题..普通的01背包.注意数组要开大点啊.... 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include &

HUD 1171 Big Event in HDU(01背包)

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

HDU 1171 Big Event in HDU(01背包)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 31928    Accepted Submission(s): 11169 Problem Description Nowadays, we all know that Computer College is the biggest departmen

hdoj1171 Big Event in HDU(01背包 || 多重背包)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意 老师有一个属性:价值(value).在学院里的老师共有n种价值,每一种价值value对应着m个老师,说明这m个老师的价值都为value.现在要将这些老师从人数上平分成两个院系,并且希望平分后两个院系老师的总价值A和B应尽可能地相等,求A和B的值(A>=B). 思路 由于每种老师的个数是有限的,所以使用多重背包解决.由于测试数据不是很严格,所以使用01背包也可以通过. 代码 01背包: 1

hdu 01背包(1171+2546+。。

1171 题意比较简单,这道题比较特别的地方是01背包中,每个物体有一个价值有一个重量,比较价值最大,重量受限,这道题是价值受限情况下最大,也就值把01背包中的重量也改成价值. //Problem : 1171 ( Big Event in HDU ) Judge Status : Accepted #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> us

Big Event in HDU 多重背包

B - Big Event in HDU 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 b

HDU-1171 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

HDU 1171 Big Event in HDU (多重背包变形)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27961    Accepted Submission(s): 9847 Problem Description Nowadays, we all know that Computer College is the biggest department