hdu 1059 Dividing(多重背包)
2015-09-07 21:21
274 查看
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
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
#include<stdio.h> #include<string.h> int f[120000]; int V; int max (int m,int n) { return m>n?m:n; } void f1(int n,int m) { int i; for (i=V;i>=n;i--) f[i]=max(f[i],f[i-n]+m); } void f2 (int m,int n) { int i; for (i=m;i<=V;i++) f[i]=max(f[i],f[i-m]+n); } void f3(int cost,int amount,int weight) { int k; if (cost*amount>=V) f2(cost,weight); else { k=1; while (k<amount) { f1(k*cost,k*weight); amount-=k; k=k*2; } f1(amount*cost,amount*weight); } } int main () { int w[10]; int i,j=1; int sum; while (1) { sum=0; for (i=1;i<=6;i++) { scanf ("%d",&w[i]); sum+=w[i]*i; } if (sum==0) break; if ((sum%2)!=0) { printf ("Collection #%d:\nCan't be divided.\n\n",j++); continue; } V=sum/2; memset(f,0,sizeof(f)); for (i=1;i<=6;i++) f3(i,w[i],i); if (f[V]==V) printf ("Collection #%d:\nCan be divided.\n\n",j); else printf ("Collection #%d:\nCan't be divided.\n\n",j); j++; } return 0; }
相关文章推荐
- quartz的时间配置规则
- NSURLSessionConfiguration
- NSString2015-09-30转化为NSData
- java13
- (算法)判断字符串中是否包含HelloWorld
- Java的final关键字
- new/delete和malloc/free
- ASI和AFN的区别
- Meteor全栈开发平台
- 分享一个简单的简单的SQLHelper类
- java12
- 读取本地存储的图片
- Windows环境下MySQL5.5的安装与配置
- 生产者-消费者模型的java实现
- 网络请求
- Android数据存储——Cache存储
- DevExpress之ChartControl用法
- j2ee之Filter使用实例(页面跳转)
- java11
- C语言篇——小谈预处理命令