Dividing
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 55704 | Accepted: 14251 |
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 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.
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.
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
#include <iostream>
#define INF 100000000
using namespace std;
int f[240005]; //f[j]相当于f[i][j]: 考虑1...i个物品,恰好放到容量为j,所能达到的最大价值
int v; //背包容量
//处理一个完全背包 (该种物品不限量)
void complete_pack(int *a, int c, int w)
{
for(int i = c; i <= v; i++)
a[i] = max(a[i], a[i - c] + w);
}
//处理一个 01背包 (该种物品只有一个)
void zeroone_pack(int *a, int c, int w)
{
for(int i = v; i >= c; i--)
a[i] = max(a[i], a[i - c] + w);
}
//处理一个多重背包 (该种物品指定上限)
void mutiple_pack(int *a, int c, int w, int m)
{
//该种物品足以塞满背包-->转化为完全背包
if(c * m >= v){
complete_pack(a, c, w);
return;
}
/*二进制思想拆分:多重背包中的一个物品--变成-->0-1背包中的多个物品
容量:2^0 2^1 2^2 2^k m-∑前面 ***保证k达到最大值
价值:2^0*c 2^1*c 2^2*c 2^k*c (m-∑前面 )*c
*/
int k = 1;
while(k < m)
{
zeroone_pack(a, k * c, k * w);
m = m - k;
k = 2 * k;
}
zeroone_pack(a, c * m, w * m);
}
int main()
{
//freopen("d:/data.in","r",stdin);
//freopen("d:/data.out","w",stdout);
int sum, i, c[7], w[7], m[7],cas = 0;
while(scanf("%d%d%d%d%d%d", &m[1], &m[2], &m[3], &m[4], &m[5], &m[6])){
if(m[1] == 0 && m[2] == 0 && m[3] == 0 && m[4] == 0 && m[5] == 0 && m[6] == 0)
break;
sum = 0;
for(i = 1; i <= 6; i++){
c[i] = w[i] = i;
sum += c[i] * m[i];
}
printf("Collection #%d:\n", ++cas);
if(sum & 1){
puts("Can't be divided.\n");
}else{
sum /= 2;
v = sum;
for(i = 1; i <= sum; i++)
f[i] = -INF;
f[0] = 0;
for(i = 1; i <= 6; i++)
mutiple_pack(f, c[i], w[i], m[i]);
if(f[v] < 0){
puts("Can't be divided.\n");
}else{
puts("Can be divided.\n");
}
}
}
system("pause");
return 0;
}
下面是一个解决单一01背包问题的方法:bone collecter
#include<stdio.h>
#include<string.h>
int f[2000],V;
void ZeroOnePack(int cost,int weight)
{
int v;
for(v=V;v>=cost;v--)
if(f[v]<f[v-cost]+weight)
f[v]=f[v-cost]+weight;
}
int main()
{
int i,t,n,c[1001],w[1001];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&V);
for(i=0;i<n;i++)
scanf("%d",&w[i]);
for(i=0;i<n;i++)
scanf("%d",&c[i]);
memset(f,0,sizeof(f));
for(i=0;i<n;i++)
ZeroOnePack(c[i],w[i]);
printf("%d\n",f[V]);
}
return 0;
}