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; }
相关文章推荐
- ACM学习历程—HDU 1059 Dividing(dp && 多重背包)
- DP(完全背包二进制优化) Problem T:Dividing(HDU 1059)
- HDU 1059 Dividing dp背包题解
- HDU 1059 Dividing(dp多重背包)
- HDU 1059 Dividing 【DP背包】
- hdu 1059 Dividing DP,多重背包 测试数据很水
- Hdu 1059 Dividing(dp)
- HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)
- hdu 1059 Dividing(多重DP)
- hdu 1059 Dividing(多重DP)
- hdu 1059 Dividing--DP-多重背包问题
- HDU1059-Dividing-DP(多重背包)
- hdu 1059 Dividing 多重背包
- HDU 1059 Dividing 多重背包
- HDU - 1059 Dividing(完全背包+多重背包)
- HDU1059 【DP·二进制数优化】
- hdoj 1059 Dividing【DP-多重背包】
- hdu 1059 Dividing(二进制转化优化) 分组背包
- hdu 1059 Dividing 多重背包
- HDU 1059 Dividing (多重背包)