hdu1059 多重背包
来源:互联网 发布:丝绸之路大数据 编辑:程序博客网 时间:2024/05/18 03:26
如题:http://acm.hdu.edu.cn/showproblem.php?pid=1059
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.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
Output a blank line after each test case.
1 0 1 2 0 01 0 0 0 1 10 0 0 0 0 0
Collection #1:Can't be divided.Collection #2:Can be divided.
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define max(a,b)(a>b?a:b)
int num[7];
int f[20000*6+10];
void ZeroOnePack(int v,int c,int w)
{
int j;
for(j=v;j>=c;j--)
f[j]=max(f[j],f[j-c]+w);
}
void CompletePack(int v,int c,int w)
{
int j;
for(j=c;j<=v;j++)
f[j]=max(f[j],f[j-c]+w);
}
void MultiplePack(int v,int c,int w,int cnt)
{
if(cnt*c>=v)
{
CompletePack(v,c,w);
return;
}
int k=1;
while(k<cnt)
{
ZeroOnePack(v,k*c,k*w);
cnt-=k;
k*=2;
}
ZeroOnePack(v,cnt*c,cnt*w);
}
int main()
{
int count=0;
while(~scanf("%d%d%d%d%d%d",&num[1],&num[2],&num[3],&num[4],&num[5],&num[6])
&&(num[1]||num[2]||num[3]||num[4]||num[5]||num[6]))
{
memset(f,0,sizeof(f));
count++;
int i,sum=0 ,v;
for(i=1;i<=6;i++)
{
sum+=num[i]*i;
}
v=sum/2;
if(sum%2)
{
printf("Collection #%d:\nCan't be divided.\n\n",count);
continue;
}
for(i=1;i<=6;i++)
MultiplePack(v,i,i,num[i]);
if(f[v]==sum-v)
printf("Collection #%d:\nCan be divided.\n\n",count);
else
printf("Collection #%d:\nCan't be divided.\n\n",count);
}
return 0;
}
- hdu1059(多重背包)
- hdu1059(多重背包)
- hdu1059 Dividing 多重背包
- hdu1059 多重背包
- [hdu1059]Dividing 多重背包
- hdu1059 多重背包
- hdu1059 Dividing (多重背包)
- hdu1059(多重背包)
- hdu1059 多重背包
- hdu1059 多重背包
- hdu1059 Dividing 【多重背包】
- HDU1059 && POJ1014 :Dividing(多重背包)
- hdu1059 &poj1014 Dividing (多重背包)
- HDU1059 Dividing 多重背包 ACM
- poj1014 hdu1059 Dividing 多重背包
- hdu1059 Dividing (多重背包)
- hdu1059 Dividing(多重背包)
- HDU1059 && POJ1014 :Dividing(多重背包)
- php.ini max_file_uploads
- HTTP状态
- windows ubuntu双系统的一些小问题解决方案
- Android 自定义全局baseAdapter和ViewHolder
- UVa: 10391 - Compound Words
- hdu1059 多重背包
- 浅谈Unix和Linux下的int
- LA 2995(贪心+推理)
- hdu2046--骨牌铺方格
- android SDK更新不了的问题
- Linux下C编程+GDB调试
- 【史上最全】Java判断操作系统类型
- 资源大全(各行业)
- hdu 递推