您的位置:首页 > 其它

HDU 1059:Dividing

2017-11-13 19:42 120 查看


Dividing

查看原题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 27786    Accepted Submission(s): 7983


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 c
4000
olletcion, 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

题意:

已知六个物品的价格分别为1,2,3,4,5,6给出六个物品的数量,问这些物品的价格是否能平分!

解题思路:(多重背包)

根据输入值,可求出总的价格。

首先判断这个数的奇偶,若为奇数一定不能平分。若为偶数,取其一半作为多重背包的限制条件V,在这一限制条件下求最大价值。若这个最大值等于V,则可以平分。

如无法理解,可以联想为01背包,但此时价格w和体积v是同一个值。在这里我把他们分开了,便于理解,实质上是同一个值v。

动态转移方程:dp[i]=max(dp[i],dp[i-v]+w);     //i表示金额(可认为动态转移方程下的背包体积便于理解),dp[i]表示这一金额下的最大价值。v价值(若你把i认为动态转移方程下的背包体积,则这个v可认为是自己的体积),w价值。

源代码:

#include<cstdio>
#include<cstring>
const int N=60005;
int max(int a, int b){return a>b?a:b;}
int dp
;
int V;
void MultiplePack(int n,int v,int w)//数量,体积,价格(实质数量,价格,价格)
{
if(n*v>=V)//完全背包
{
for(int i=v;i<=V;i++)
dp[i]=max(dp[i],dp[i-v]+w);
return ;
}
int k=1;
int nC=n;
while(k<=nC)//01背包,在这里做一个优化
{
for(int i=V;i>=k*v;i--)
dp[i]=max(dp[i],dp[i-k*v]+k*w);
nC-=k;
k*=2;
}
for(int i=V;i>=nC*v;i--)
dp[i]=max(dp[i],dp[i-nC*v]+nC*w);
}
int main()
{
int w[6],t=0;
while(scanf("%d%d%d%d%d%d",&w[0],&w[1],&w[2],&w[3],&w[4],&w[5]),w[0]+w[2]+w[3]+w[4]+w[5])
{
memset(dp,0,sizeof(dp));
int sum=0;

for(int i=0;i<6;i++)
sum+=w[i]*(i+1);
V=sum/2;
if(sum%2) printf("Collection #%d:\nCan't be divided.\n\n",++t);//为奇不能平分
else
{
for(int i=0;i<6;i++)
{
if(w[i])
{
MultiplePack(w[i],i+1,i+1);//分别表示数量,价值,价值
}
}
if(dp[V]==V)  printf("Collection #%d:\nCan be divided.\n\n",++t);
else printf("Collection #%d:\nCan't be divided.\n\n",++t);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: