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 ...
随机推荐
- python中sys和os的区别
<os和sys的官方解释> ➤os os: This module provides a portable way of using operating system dependent ...
- ORACLE CPU过高的sql查询
1. 根据占用CPU高的进程号来查询这个进程执行的SQL语句: CPU过高的进程号: #首先找到CPU过高的进程号 # top -bn1 是静态找到占用最高的进程 [root@localhost ...
- Codeforces 1260 ABC
DEF 题对于 wyh 来说过于毒瘤,十分不可做. A. Heating Description: 给定\(a,b\),将\(b\)分成至少\(a\)个正整数,使这些正整数的平方和最小. Soluti ...
- git 常用命令记录 -- 快捷&备忘
1.安装 略2.git拉取远程分支 git config user.name git config user.email git config --global user.name xxxx git ...
- Spring Schedule 实现定时任务
很多时候我们都需要为系统建立一个定时任务来帮我们做一些事情,SpringBoot 已经帮我们实现好了一个,我们只需要直接使用即可,当然你也可以不用 SpringBoot 自带的定时任务,整合 Quar ...
- system调用
调用系统命令,利用fork+exec+wait来执行系统命令,依赖系统环境
- 《React后台管理系统实战 :四》产品分类管理页:添加产品分类、修改(更新)产品分类
一.静态页面 目录结构 F:\Test\react-demo\admin-client\src\pages\admin\category add-cate-form.jsx index.jsx ind ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:显示关闭按钮
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 对于strlen()函数的一点小疑问
看csapp时候,看一下char*[8],以为char*也是一字节,但是指针是地址,64位编译器下是8字节,所以sizeof(B)是64字节 后来又看strlen(). #include"s ...
- C# 篇基础知识5——委托和事件
事件处理程序是基于“委托”机制运行的. 1.委托 (1)委托的定义和使用 有时需要将一个函数作为另一个函数的参数,这时就要用到委托(Delegate)机制.例如设计一个马戏表演函数: //定义委托 d ...