Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20635    Accepted Submission(s): 5813

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.
 
Source

哎~dp好难..........

题意:每行六个数,若都为零则结束,否则,第几个数代表价值为几的东西有几个,东西数量不超过20万,问所有东西能不能分成两堆价值相等的。

能的话输出 Can..,否则Can't...,每组输出后面有个空行~

多重背包加优化..暂时还是不能深入的理解dp的奥义..烦恼ing...

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
#define ll long long using namespace std;
int dp[],w[];
int main(void)
{
int ans,sum,cnt = ;
while(scanf("%d",&w[]) != -)
{
sum = ;
ans = ;
if(w[] != ) ans= ;
sum += w[];
for(int i = ; i <= ; i++)
{
scanf("%d",&w[i]);
sum += w[i]*i;
if(w[i])
ans = ;
}
if(!ans)
break;
if(sum % == )
{
printf("Collection #%d:\nCan't be divided.\n\n",cnt++);
}
else
{
memset(dp,,sizeof(dp));
for(int i = ; i <= w[i] && i <= sum/; i++) dp[i] = ;
for(int i = ; i <= ; i++)
for(int j = sum/; j >= ; j--)
{
if(dp[j] == )
continue;
for(int k = ; k <= w[i] && k *i+j <= sum/; k++)
{
if(dp[k*i+j]) break;
dp[k*i+j] = ;
}
}
if(dp[sum/] == )
printf("Collection #%d:\nCan be divided.\n\n",cnt++);
else
printf("Collection #%d:\nCan't be divided.\n\n",cnt++);
} }
return ;
}

最新文章

  1. 树上启发式合并 (dsu on tree)
  2. DPDK内存管理(1)
  3. 获取HTML
  4. xUtils,butterknife...处理findviewbyid
  5. 配置Entity Framework连接Sql Server出现的一个异常
  6. Linux redirect the stdout to a file
  7. 【Android】日常开发android.jar文件中十五个重要的包概述
  8. 新浪微博、腾讯微博、QQ空间、人人网、豆瓣 一键分享API
  9. iOS8开发~Swift(一)入门
  10. SGU_390_Tickets(另类数位DP)
  11. 【一本通1329:【例8.2】细胞&amp;&amp;洛谷P1451 求细胞数量】
  12. 用canvas给视频图片添加特效
  13. Linux 最小系统制作
  14. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验五:按键模块④ &mdash; 点击,长点击,双击
  15. tmux 快捷操作
  16. Jmeter&#160;测试结果分析之聚合报告简介
  17. JAVA-数据库之加载JDBC驱动程序
  18. ChinaTest测试感悟
  19. 5.String StringBuffer StringBuilder
  20. Oracle 提示符

热门文章

  1. windows10 vs2019 + opencv 3.4.7环境搭建
  2. 2-sat——暴力染色输出方案hdu1814
  3. LUOGU P1505 [国家集训队]旅游 (树链剖分+线段树)
  4. Spring boot配置Dubbo三种方式
  5. windows环境下,svn未备份情况下重新恢复
  6. 7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树
  7. JDK源码阅读--HashMap
  8. flock文件锁的学习和应用
  9. Deepin折腾手记之安装常用软件
  10. PKU--1267 Cash Machine(多重背包)