HDU 1059 完全背包

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22808    Accepted Submission(s): 6444

Problem Description

Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.

Input

Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0‘‘. The maximum total number of marbles will be 20000.

The last line of the input file will be ``0 0 0 0 0 0‘‘; do not process this line.

Output

For each colletcion, output ``Collection #k:‘‘, where k is the number of the test case, and then either ``Can be divided.‘‘ or ``Can‘t be divided.‘‘.

Output a blank line after each test case.

Sample Input

1 0 1 2 0 0

1 0 0 0 1 1

0 0 0 0 0 0

Sample Output

Collection #1:
Can‘t be divided.

Collection #2:
Can be divided.

Source

Mid-Central European Regional Contest 1999

题解:6种石头拥有不同价值  现在要求你等分

输入 n1...ni....n6  代表 价值为i的石头有ni个  若能等分按要求输出 反之亦然

题意:背包容量为总价值的一半  若能满足f[m]==m则说明能够等分

完全背包的二进制优化实现

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #define ll __int64
 6 using namespace std;
 7 int n[10];
 8 int m;
 9 int jishu;
10 int size[60005];
11 int value[60005];
12 int coun=1;
13 int f[60005];
14 void slove(int q)
15 {
16     coun=1;
17    for(int i=1;i<=q;i++)
18   {
19      int c=n[i],v=i;
20     for (int k=1; k<=c; k<<=1)
21     {
22         value[coun] = k*v;
23         size[coun++] = k*v;
24         c -= k;
25     }
26     if (c > 0)
27     {
28        value[coun] = c*v;
29        size[coun++] = c*v;
30     }
31 }
32 }
33 int main()
34 {    jishu=0;
35     while(scanf("%d %d %d %d %d %d",&n[1],&n[2],&n[3],&n[4],&n[5],&n[6])!=EOF)
36     {
37         if(n[1]==0&&n[2]==0&&n[3]==0&&n[4]==0&&n[5]==0&&n[6]==0)
38         break;
39         memset(f,0,sizeof(f));
40         memset(size,0,sizeof(size));
41         memset(value,0,sizeof(value));
42         m=n[1]+n[2]*2+n[3]*3+n[4]*4+n[5]*5+n[6]*6;
43         if(m%2)
44         {
45             printf("Collection #%d:\nCan‘t be divided.\n\n",++jishu);
46         }
47         else
48         {
49           slove(6);
50             m=m/2;
51             for(int i=1;i<=coun-1;i++)
52             for(int k=m;k>=value[i];k--)
53             {
54                 f[k]=max(f[k],f[k-size[i]]+value[i]);
55             }
56          if(f[m]==m)
57         printf("Collection #%d:\nCan be divided.\n\n",++jishu);
58             else
59          printf("Collection #%d:\nCan‘t be divided.\n\n",++jishu);
60
61         }
62
63     }
64     return 0;
65 }
时间: 2024-12-29 13:08:23

HDU 1059 完全背包的相关文章

HDU 1059 多重背包+二进制优化

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16909    Accepted Submission(s): 4729 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

hdu 1059 (多重背包) Dividing

这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的两份. 联想到多重背包,稍微用二进制优化一下.(最近身体不适,压力山大啊) 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #define inf 70000 5 using namespace s

hdu 1059 多重背包

题意:价值分别为1,2,3,4,5,6的物品个数分别为a[1],a[2],a[3],a[4],a[5],a[6],问能不能分成两堆价值相等的. 解法:转化成多重背包 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 7 int dp[120010]; 8 int a[10]; 9 10 int

HDU 1059 Dividing(多重背包)

HDU 1059 Dividing(多重背包) http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意: 现在有价值为1,2,3,4,5,6的6种物品, 它们的数量为num[i]( 1<=i<=6 )个. 现在要问的是能否把所有的的物品分成两份且这两份物品的价值总和相同 ? 分析: 首先我们求出所有物品的价值和sum_val, 如果sum_val是奇数, 那么明显不能分. 那么sum_val为偶时, 我们令m=sum_val/2. 我能只要看看从所有

hdu 1059 Dividing

题目: 链接:点击打开链接 题意: 判断是否能够平分弹珠. 算法: 多重背包. 思路: 模板...dp[i]中i表示花费.. 代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; int n[7]; int dp[120010]; int V; void bag_01(int c,int w)//01背包 { for(int i=V; i>=c; i--) dp

hdu 1171 Big Event in HDU(母函数|多重背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:有n种物品,给出每种物品的价值和数目,要将这些物品尽可能的分成相等的两份A和B且A>=B ,输出A,B. 母函数可以过,但感觉最直接的方法应该是多重背包. 母函数的话,也是按总价值的一半求,从一半到小枚举,直到找到系数不为0的就是B. #include <stdio.h> #include <iostream> #include <map> #include <

HDU Flowers 完全背包

Flowers Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2709    Accepted Submission(s): 1811 Problem Description As you know, Gardon trid hard for his love-letter, and now he's spending too much

HDU 3033 分组背包

http://www.hgy413.com/1319.html 简介DeviceIoControl的三种通信方式 HDU 3033 分组背包,布布扣,bubuko.com

hdu 4381(背包变形)

题意: 给定n个块,编号从1到n,以及m个操作,初始时n个块是白色. 操作有2种形式: 1 ai xi : 从[1,ai]选xi个块,将这些块涂白. 2 ai xi:从[ai,n]选xi个块,将这些块涂白. 可以忽略某些操作且如果区间内没有足够的黑块(黑块用于涂白),则不能进行这个操作. 分析: 写写画画一看就知道这道题是一个背包问题. “恰好装满背包”. 以下摘自题解: 本题难点在于正确处理两种操作,不妨假设只有一种操作,那么这种操作如果是1的话那么就把操作按照a从小到大排序,每次都尽量往最左