您的位置:首页 > 其它

HDU-1059 Dividing(DP)

2015-04-26 09:28 225 查看

Dividing

http://acm.hdu.edu.cn/showproblem.php?pid=1059

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0


[align=left]Sample Output[/align]

Collection #1:
Can't be divided.

Collection #2:
Can be divided.


初学DP,只知道01背包,没看过多重背包,只是感觉应该这样做,超时n次,网上看到别人的解法和分析证明收获颇多

/article/5405504.html这个解法还是看不懂,待以后懂的更多了再来看

#include <cstdio>
#include <cstring>
#include <algorithm>
#define LOCAL
using namespace std;

const int MAXN=60005;
int m,maxv,n[9];
int dp[MAXN];
int mod[7]={1,42,42,14,84,210,42};

int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif // LOCAL
int kase=0;
while(scanf("%d%d%d%d%d%d",&n[1],&n[2],&n[3],&n[4],&n[5],&n[6]),(n[1]||n[2]||n[3]||n[4]||n[5]||n[6])) {
printf("Collection #%d:\n",++kase);
//优化法一:		 http://poj.org/bbs?problem_id=1014 //POJ上给出的一种优化,不明白为什么
/*
for(int i=1;i<=6;++i)
if(n[i]>60) {
if(1==(1&n[i]))
n[i]=61;
else
n[i]=60;
}
*/

//优化法二:自己苦想半天想出来的取模优化,
//          看了		 http://blog.csdn.net/gg_gogoing/article/details/38453273 //          才知道取模原来都是错的...
//          同时又看到:取模前不为0,取模结果为0时,重新赋为模可避免问题(不知道能不能避免全部错误的发生)

//1+2+3+4+5+6=21,则对i和21的最小偶公倍数取模,刚好能消去21*2n(n=0,1,2……),每一堆都能分得21*n
for(int i=1;i<=6;++i) {
bool flag=true;
if(0==n[i])
flag=false;
n[i]%=mod[i];
if(0==n[i]&&flag)
n[i]=mod[i];
}
int mini=n[1];
for(int i=2;i<=6;++i)
if(n[i]<mini)
mini=n[i];
if(1==(mini&1))
--mini;
for(int i=1;i<=6;++i)
n[i]-=mini;

maxv=0;
for(int i=1;i<=6;++i)
maxv+=n[i]*i;
if(1==(maxv&1)) {
printf("Can't be divided.\n\n");
continue;
}
m=maxv/2;
memset(dp,0,sizeof(dp));
for(int i=1;i<=6;--n[i]) {
if(n[i]==0) {
++i;
++n[i];
continue;
}
for(int j=m;j>=i;--j) {
dp[j]=max(dp[j-i]+i,dp[j]);
}
}
if(m!=dp[m])
printf("Can't be divided.\n\n");
else
printf("Can be divided.\n\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: