Dividing
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 file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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 collection, 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
题目类型:多重背包
算法分析:使用dp[i]表示恰好组成体积i时所具有的最大价值,递推方程为dp[i] = max (dp[i], dp[i-w[i]] + w[i]),初始化为dp[0] = 0,其他都为-1,最后判断dp[v] == v是否成立即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define LL long long #define ULL unsigned long long #define DB(ccc) cout << #ccc << " = " << ccc << endl const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 2e6 + 66; //c[i]表示第i个物品的花费 w[i]表示第i个物品的价值 //m[i]表示第i个物品的数量 dp[i]表示前i物品组成的最大价值 int c[maxn], w[maxn], m[maxn], dp[maxn]; int V, N;//分别表示背包的总体积和可选的物品的种数 void ZeroOnePack (int c, int w) { for (int i = V; i >= c; i--) dp[i] = max (dp[i], dp[i-c] + w); } void CompletePack (int c, int w) { for (int i = c; i <= V; i++) dp[i] = max (dp[i], dp[i-c] + w); } void MultiPack (int c, int w, int m) { if (c * m >= V) { CompletePack (c, w); return ; } int k = 1; while (k < m) { ZeroOnePack (k * c, k * w); m -= k; k *= 2; } ZeroOnePack (m * c, m * w); } void Solve ()//可依照题目变化,这里表示背包不需要装满,如果只有dp[0]赋值为0,则表示恰好装满时的最大价值 { memset (dp, -1, sizeof (dp)); dp[0] = 0; for (int i = 1; i <= N; i++) MultiPack (c[i], w[i], m[i]); } int main() { // CFF; N = 6; int flag = 1; while (scanf ("%d%d%d%d%d%d", &m[1], &m[2], &m[3], &m[4], &m[5], &m[6]) != EOF) { V = 0; for (int i = 1; i <= 6; i++) { V += i * m[i]; c[i] = w[i] = i; } if (!V) break; printf ("Collection #%d:\n", flag++); if (V % 2) puts ("Can't be divided.\n"); else { V /= 2; Solve (); if (dp[V] == V) puts ("Can be divided.\n"); else puts ("Can't be divided.\n"); } } return 0; } |