POJ1014:Dividing
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 63013 | Accepted: 16315 |
Description
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
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
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.
给六种宝石,价值分别是1 2 3 4 5 6,题中input给的是各个宝石的数量,要求的是能不能将这些数量的宝石,分成等价值的两堆。
要是别人不告诉我这是动态规划题,我真想不出这种思路。
头一次见过这种思路,求整体能不能分开,是看其一半值total在数组DP[total]中是不是为true,说是动态规划,其动态规划就体现在在取第n轮宝石时,只跟n-1轮宝石的值有关,这和POJ2392那道题的思路很类似很类似。只要理解思路,写出代码就不难了。
代码:
<span style="font-size:14px;">#include <iostream>
#include <vector>
using namespace std; int dp[8][120002];//dp[i][j]代表选第i中宝石时,j是否可能取到
//dp[i][j]=dp[i-1][j-i*x]
int marible[8];
int total=0; void init()
{
int count;
total=0;
for(count=1;count<=6;count++)
{
cin>>marible[count];
total += marible[count]*count;
}
} void cal()
{
int i,j,x; memset(dp,0,sizeof(dp));
dp[1][0]=1;
for(i=1;i<=marible[1];i++)
dp[1][i]=1; for(i=2;i<=6;i++)
{
for(j=0;j<=total;j++)
{
if(dp[i-1][j])
{
for(x=1;x<=marible[i];x++)
{
dp[i][j+x*i]=dp[i-1][j];
}
}
if(dp[i][j])
continue;
dp[i][j]=dp[i-1][j]; }
} } int main()
{
bool end = true;
int t=0;
while(end)
{
t++; init();
if(total==0)
{
return 0;
}
if(total%2)
{
cout<<"Collection #"<<t<<":"<<endl;
cout<<"Can't be divided."<<endl<<endl;
}
else
{
total /=2;
cal();
if(dp[6][total])
{
cout<<"Collection #"<<t<<":"<<endl;
cout<<"Can be divided."<<endl<<endl;
}
else
{
cout<<"Collection #"<<t<<":"<<endl;
cout<<"Can't be divided."<<endl<<endl;
}
}
}
return 0;
}</span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ1014:Dividing的更多相关文章
- 【poj1014】 Dividing
http://poj.org/problem?id=1014 (题目链接) 题意 给出有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份, ...
- poj1014:母函数+优化
题目大意: 有1~6六种宝石,价格分别为1~6 ..给定每种宝石的个数,问能否平分给两个人 分析: 一看显然是个多重背包问题,也可以用母函数做 不过母函数的复杂度是n*v*k,第一次tle了.. 后来 ...
- poj1014 hdu1059 Dividing 多重背包
有价值为1~6的宝物各num[i]个,求能否分成价值相等的两部分. #include <iostream> #include <cstring> #include <st ...
- 题解 【POJ1014】 Dividing
题目意思 有六种不同的石子,权值为\(1\)~\(6\),给出六种石子的数量,求能否将石子分成权值相等的两份. 解析 这题可以直接用多重背包写, 因为仔细想想, 能够平均分成两份, 也就是能将一部分石 ...
- Command and Query Responsibility Segregation (CQRS) Pattern 命令和查询职责分离(CQRS)模式
Segregate operations that read data from operations that update data by using separate interfaces. T ...
- 多重背包 (poj 1014)
题目:Dividing 题意:6种重量的的石头,每个给定数量,用总重的一半去装,问能否装满. #include <iostream> #include <algorithm> ...
- hdu 4963(中途相遇法)
题目链接:Dividing a String 题意:给定一个2*n(n<=20)的字符串及每个位置的字符包含的权重,求将该字符串分成两个子序列S1.T1,要求S1=T1且abs(weight1- ...
- java web 开发三剑客 -------电子书
Internet,人们通常称为因特网,是当今世界上覆盖面最大和应用最广泛的网络.根据英语构词法,Internet是Inter + net,Inter-作为前缀在英语中表示“在一起,交互”,由此可知In ...
- 所有selenium相关的库
通过爬虫 获取 官方文档库 如果想获取 相应的库 修改对应配置即可 代码如下 from urllib.parse import urljoin import requests from lxml im ...
随机推荐
- 「CF1303C Perfect Keyboard」
前置芝士 图的遍历:通过DFS或者BFS遍历全图. 前向星:用来存边,但是在本题用也可以用一个二维数组解决. 具体做法 先从判断YES和NO开始,可以发现如果一个字母与三个及以上不同的字母相邻时肯定是 ...
- RESTful风格化
RESTful Web Service介绍 Roy Thomas Fielding博士2000年提出的 REST是英文Representational State Transfer的缩写 表象化状态转 ...
- 设计模式课程 设计模式精讲 10-2 外观模式coding
1 代码演练 1.1 代码演练1(不使用外观模式) 1.2 代码演练2(使用外观模式) 2 应用核心 2.1 核心 2.2 零碎知识点 1 代码演练 1.1 代码演练1(不使用外观模式) 需求: 木木 ...
- LibreOJ #6001. 「网络流 24 题」太空飞行计划
\(\quad\) 与网络流有关的最值有三个:最大流,最小费用,最小割.这道题是最小割.想了好久,终于想明白最小割应该怎么用. \(\quad\) 先找出矛盾的事物.在这道题中,两件事是矛盾的:做实验 ...
- pytorch张量数据索引切片与维度变换操作大全(非常全)
(1-1)pytorch张量数据的索引与切片操作1.对于张量数据的索引操作主要有以下几种方式:a=torch.rand(4,3,28,28):DIM=4的张量数据a(1)a[:2]:取第一个维度的前2 ...
- python中对闭包的理解
运行环境声明:本人的代码在sublime text 3中写的,可以Ctrl+b运行.python版本是python3.6.如果您直接运行的,请自觉加入if __name__ == '__main__' ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 安装
所有平台的 MySQL 下载地址为: MySQL 下载:https://dev.mysql.com/downloads/mysql/ 注意:安装过程我们需要通过开启管理员权限来安装,否则会由于权限不足 ...
- RedHat OpenShift QuickStart 1.1 OpenShift基础
openshift 提供了命令行工具和web可视化页面,这些工具通过REST API去和openshift交互 一.开始为开发人员使用OpenShift 1. 探索命令行 2. 探索web conso ...
- #写一个随机产生138开头手机号的程序 1.输入一个数量,产生xx条手机号 2.产生的这些手机号不能重复
import randomcount=int(input('请输入你所想要手机号数量:'))prefix='138'for i in range(count): num=random.sample(r ...
- 导弹拦截p1020(LIS问题)
题目描述(题目链接:https://www.luogu.org/problem/P1020) 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够 ...