POJ 1014 Dividing(多重背包)
Description
Input
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
Output
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. 这道题目多重背包入手真心很简单,题目要求,将弹珠根据价值的大小均分给两个人。
多重背包的模板题目里,问题的相关条件有:背包的体积、 物品的种类、 每种物品的数量、 每种物品所占的体积。这是通常情况,而这道题目里,只有
物品的种类、 每种物品的数量、 每种物品所占的体积这3个条件,但是题目也要求简单,就是看这堆弹珠是否能够均分,所以,背包的体积你可以当作是题目极限条件那么大。
然后运用二进制的思想写出多重背包就好.其实我也就是昨天才学会了多重背包。
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream> using namespace std;
const int max_size = * + ;
int main()
{
int val[];//val数组里存放每种弹珠的数量
int dp[max_size];//dp数组开题目极限那么大 int cas = ; while(cas)
{
int tag = ;
for(int i = ; i < ; i++)
{
scanf("%d", val+i);
if(val[i] == )
{
tag++;
}
}
memset(dp, , sizeof(dp));
if(tag == )
break;
else
{
int tot = ;
for(int i = ; i < ; i++)
{
tot += val[i] * (i+);
}
int half = tot / ;
int half1 = tot - half;
if(half == half1)
{
bool flag = false;
for(int i = ; i < ; i++)
{
int k = ;
while(k < val[i])
{
for(int j = max_size; j - (i+)*k>= ; j--)
{
dp[j] = max(dp[j], dp[j-(i+)*k]+(i+)*k);
if(dp[j] == half)//在dp过程中,找寻是否有一种状态,满足将弹珠平分这一条件
{
flag = true;
break;
}
}
val[i] -= k;
k *= ;
if(flag == true)
break;
}
if(flag != true)
{
for(int j = max_size; j - val[i]*(i+) >= ; j--)
{
dp[j] = max(dp[j], dp[j-(i+)*val[i]]+(i+)*val[i]);
if(dp[j] == half)
{
flag = true;
break;
}
}
}
else
{
printf("Collection #%d:\n", cas);
printf("Can be divided.\n");
break;
}
}
if(flag != true)
{
printf("Collection #%d:\n", cas);
printf("Can't be divided.\n");
}
}
else
{
printf("Collection #%d:\n", cas);
printf("Can't be divided.\n");
}
}
cas++;
printf("\n");//GG,我去,因为没看要多输出一行空行,PE一次
} return ;
}
POJ 1014 Dividing(多重背包)的更多相关文章
- Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...
- POJ 1014 Dividing 多重背包
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63980 Accepted: 16591 Descri ...
- POJ 1014 Dividing(多重背包, 倍增优化)
Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...
- POJ 1014 Dividing (多重可行性背包)
题意 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是( ...
- POJ 1014 Dividing(多重背包+二进制优化)
http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include&l ...
- DFS(DP)---POJ 1014(Dividing)
原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两 ...
- hdu 1059 Dividing(多重背包优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- POJ 1742 Coins(多重背包, 单调队列)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- POJ 2392【多重背包】
题意: k个块,给出每个块的高度hi,数量ci,不能超过的高度: 求这些块可以组成的最大高度一个. 思路: 大致可看这个题是一个背包,背包的承重是高度. 对于每个物品,有他的价值是高度,还有限定的数量 ...
随机推荐
- flume学习
下载 自定义sink(mysql) 1.ide打开下载后的源码 2.代码如下: /** * Licensed to the Apache Software Foundation (ASF) under ...
- 数对的个数(cogs610)
Description出题是一件痛苦的事情!题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的A+B Problem,改用A-B了哈哈! 好吧,题目是这样的:给出一串数以及一个数字C,要求计算出所有A- ...
- Mysql之INFORMATION_SCHEMA解析1
INFORMATION_SCHEMA库是Mysql提供的一个系统库,保存了数据库的原数据,方便用户监控与管理Msyql. 现在单说与INNODB相关的库:INNODB_SYS_TABLES,INNOD ...
- C 替换字符方法
// 444.cpp : Defines the entry point for the console application. // #include "stdafx.h" # ...
- FastPolice项目总结
This is the final homework for spatial information Mobile Service Lesson.It generally inclusived the ...
- stat file 查看文件的 最新的被访问时间 最近的修改时间 最近的状态改变时间
[root@NB ~]# stat /media/6FE5-D831/git-data/IT-DOC/web收藏.txt File: `/media/6FE5-D831/git-data/IT-DOC ...
- Linq to json
Json.Net系列教程 4.Linq To JSON 一.Linq to JSON是用来干什么的? Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JS ...
- Android Studio 配置
Android配置:[转]原地址:http://www.cnblogs.com/smyhvae/p/4022844.html [开发环境] 物理机版本:Win7旗舰版(64位) Android Stu ...
- 攻城狮在路上(壹) Hibernate(十一)--- 映射实体关联关系
本文以Customer和Address类的关系为例说明一对一关联映射:以Category和Item类的关系说明多对多关联关系.一.映射一对一关联: 分两种情况:按照外键映射和按照主键映射.这两种方式的 ...
- LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)
LeetCode 39 class Solution { public: void dfs(int dep, int maxDep, vector<int>& cand, int ...