Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 69575   Accepted: 18138

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 file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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 collection, 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

 
题目大意:
  有很多很多的物品,但是总数不过20000。
  然后每个物品的价值在1~6之间
  很多组数据
  每组数据分别输出价值为1~6的物品的个数
  然后叫你判断是否能被分成价值为两堆的物品
 
思路:
  背包dp,难的一比、、
  这我提交了有4遍才过,两遍tle,一遍pre
  这题坑很多
  读完题后,很多人可能认为这是一道搜索题
  然后开始暴力搜索,最后tle成**。
  我们来分析一下这道题的解法
  首先,它要你把物品分成两堆,而且价值要相等
  所以,我们就有了一个优化
  当物品总价值%2==1时,判定为不能均分
  如果%2==0的话
  我们开始dp
  这个dp是一个裸的多重背包dp
  我们用当前物品的价值推所有价值的是否存在
  价值为i的物品当前层次由价值为i-1的物品当前层次推出
  然后判断物品总价值/2的价值是否存在就好了
  因为物品总价值/2能被凑出来,那么另一半肯定也能
  反之则不能
  但是有点坑
  如果你真的是这样顺着跑了下来,可能会超时
  所以我们要在dp的过程中判定是否有物品总价值/2存在
  这样的一个小优化就不会超时就能ac了
  感觉应该还有更好的解法,但是我想不出来了。、、。
  还有一点
  就是我的一遍pre
  要注意,每组数据之间有一个换行
 
来,上代码:

#include<cstdio>
#include<cstring> using namespace std; int a[],sum_value=,ci[][],now=; bool dp[][]; bool Check_Answer()
{
memset(dp,false,sizeof(dp));
dp[][]=true;
for(int i=;i<=;i++)
{
for(int j=;j<=sum_value;j++)
{
if(dp[i][sum_value]) return true;
if(dp[i-][j])
{
for(int v=;v<=a[i];v++) dp[i][j+ci[i][v]]=true;
}
}
}
if(dp[][sum_value]) return true;
else return false;
} int main()
{
bool if_break;
while()
{
if_break=true,sum_value=,now++;
for(int i=;i<=;i++)
{
scanf("%d",&a[i]);
if(a[i]) if_break=false;
sum_value+=a[i]*i;
for(int j=;j<=a[i];j++) ci[i][j]=i*j;
}
if(if_break) break;
if(sum_value%)
{
printf("Collection #%d:\nCan't be divided.\n\n",now);
continue;
}
sum_value/=;
if(Check_Answer()) printf("Collection #%d:\nCan be divided.\n\n",now);
else printf("Collection #%d:\nCan't be divided.\n\n",now);
}
return ;
}

另附我的一个失败代码:

#include<cstdio>
#include<cstring> using namespace std; int a[],sum_value,num,ci[],flag; bool dp[]; int main()
{
int now=;
while()
{
now++;
memset(dp,false,sizeof(dp));
sum_value=,num=,flag=;
bool if_break=true;
for(int i=;i<=;i++)
{
scanf("%d",&a[i]);
sum_value+=a[i]*i,num+=a[i];
if(a[i]!=)
{
if_break=false;
for(int j=flag;j<flag+a[i];j++) ci[j]=i;
flag+=a[i];
}
}
if(if_break) break;
if(sum_value%)
{
printf("Collection #%d:\nCan't be divided.\n\n",now);
continue;
}
sum_value/=;
int max_now=;
dp[]=true;
for(int i=;i<=num;i++)
{
int max_now_=max_now;
for(int j=;j<=max_now;j++)
{
if(dp[j])
{
dp[j+ci[i]]=true;
if(j+ci[i]==sum_value)
{
printf("Collection #%d:\nCan be divided.\n\n",now);
if_break=true;
break;
}
if(j+ci[i]>max_now_&&j+ci[i]<=sum_value) max_now_=j+ci[i];
}
}
max_now=max_now_;
if(if_break) break;
}
if(if_break) continue;
printf("Collection #%d:\nCan't be divided.\n\n",now);
}
return ;
}

AC日记——Dividing poj 1014的更多相关文章

  1. AC日记——Tree poj 3237

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9233   Accepted: 2431 Description ...

  2. AC日记——Crane poj 2991

    POJ - 2991 思路: 向量旋转: 代码: #include <cmath> #include <cstdio> #include <cstring> #in ...

  3. AC日记——pigs poj 1149

    POJ - 1149 思路: 最大流: 代码: #include <cstdio> #include <cstring> #include <iostream> # ...

  4. Dividing POJ - 1014 多重背包二进制优化

    多重背包模型  写的时候漏了一个等号找了半天 i<<=1 !!!!!! #include<iostream> #include<cstdio> #include&l ...

  5. AC日记——Dining poj 3281

    [POJ-3281] 思路: 把牛拆点: s向食物连边,流量1: 饮料向t连边,流量1: 食物向牛1连边,流量1: 牛2向饮料连边,流量1: 最大流: 来,上代码: #include <cstd ...

  6. AC日记——Two poj 1849

    Two 思路: 树形DP求直径: 答案是边权总和*2-直径: dp[i][1]::以i为根的子树中最长的路径: dp[i][0]::以i为根的子树中次长的路径: 来,上代码: #include < ...

  7. AC日记——Oulipo poj 3461

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37958   Accepted: 15282 Description The ...

  8. 证明 poj 1014 模优化修剪,部分递归 有错误

    这个问题是存在做.我发现即使是可行的一个问题,但不一定正确. 大部分数据疲软,因为主题. id=1014">poj 1014 Dividing 题目大意:有6堆石头,权重分别为1 2 ...

  9. AC日记——codevs1688求逆序对

    AC日记--codevs1688求逆序对 锵炬 掭约芴巷 枷锤霍蚣 蟠道初盛 到被他尽情地踩在脚下蹂躏心中就无比的兴奋他是怎么都 ㄥ|囿楣 定要将他剁成肉泥.挫骨扬灰跟随着戴爷这么多年刁梅生 圃鳋 ...

随机推荐

  1. 用JSON.parse和eval出现的问题

    json格式非常受欢迎,而解析json的方式通常用JSON.parse()但是eval()方法也可以解析,这两者之间有什么区别呢? JSON.parse()之可以解析json格式的数据,并且会对要解析 ...

  2. 玩转 HTML5 Placeholder

    Placeholder(占位符) 是 HTML5 新增的一个 HTML 属性,用来对可输入字段的期望值提供提示信息,目前已经得到主流浏览器的广泛支持,使用方式非常简单: <input id=&q ...

  3. SAP查询事务、表属于哪个模块

    这个功能主要是在汇报SAP的产品问题时,让你知道该选择你所汇报的问题所哪个子模块的.它可以依据事务代码.表.程序名.域.数据元素来查找.     方法:SE38,执行报表程序:RSSTATUS .

  4. Navicat for Oracle实现连接Oracle

    不知道为什么,从一开始,我就不喜欢Oracle,名字好听,功能强大,但总感觉"高不可攀";或许是因为我觉得其他的数据库就可以解决数据问题,不太了解Oracle的优势:而且它长得也不 ...

  5. SharePoint 2013 日历视图兼容性问题

    在IE11上访问SharePoint 2013 calendar视图,发现加入兼容性视图以后访问,正常,如下图: 不加入兼容性视图IE11访问,出现兼容性问题,如下图: 因为有些环境有问题,有些环境没 ...

  6. SharePoint 2013 showModalDialog 弹出模式窗口

    1. SharePoint 弹出框 本文讲述SharePoint 2013 中使用 SP.UI.ModalDialog.showModalDialog时 showModalDialog  未定义的问题 ...

  7. swift开发多线程篇 - 多线程基础

    swift开发多线程篇 - 多线程基础 iOS 的三种多线程技术 (1)NSThread  使用NSThread对象建立一个线程非常方便 但是!要使用NSThread管理多个线程非常困难,不推荐使用 ...

  8. 【代码笔记】iOS-带索引的tableView

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  9. [PL/SQL]使用PL/SQL实现ORACLE的Excel格式导入导出

    注:教程所使用的PL/SQL Developer版本为10版本 1.oracle导出excel格式 第一步,在pl/sql窗口查询出你想要导出的数据. 第二步,选择你想导出的数据,然后右键" ...

  10. javascript-单例模式

    单例模式笔记   也称为单体模式,只允许实例化一次的对象类   用法:      1.命名空间:用一个对象来规划一个命名空间,井井有条的管理对象上的属性和方法      2.静态变量管理:让创建的函数 ...