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 ...
随机推荐
- Spring之IOC、AOP和事务
IOC 什么是IOC?如果你回答:Spring是个IOC容器,实现原理是反射,没了.这样证明你没有真正理解IOC. IOC的意思是控制反转,还有个外号叫依赖注入(DI).为什么起这么晦涩难懂的名字呢? ...
- Oracle10g下载地址
Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise/Standard Edition for Microsoft Windows (32-bit ...
- mysql判断数据库或表是否存在
(1) 判断数据库存在, 则删除: drop database if exists db_name;(2) 判断数据表存在, 则删除: drop table if exists table ...
- JAVA 数据库操作工具类----sqllite
package com.asc.db; import android.content.ContentValues; import android.content.Context; import and ...
- 题解 LG P2264
这是题解P2264 先讲一下Trie,其实Trie也名前缀树,就是说:如果Trie中某串是某串的前缀,那么我们可以共用这个串也就是这样: 插入h.hk.jc,jcfa 那么,h节点会给h和hk共用,j ...
- unity3d Asset Store下载的资源在哪?
win7 C:\Users\(用户名)\AppData\Roaming\Unity\Asset Store\ 用户名为中文的时候,是不能直接在unity3d中打开的.
- UVALive 3231 网络流
题目要求给m个任务分配给n个机器,但最后任务量最多的那个机器的任务量尽量少,利用最大流,在最后的汇点那里设置关卡,二分结果,把机器到最终汇点的容量设置为该值,这样就达到题目条件,这样跑最大流 还能把m ...
- 使用windows函数SetWindowsHookEx实现键盘钩子
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- linux下mysql允许远程连接
1. MySql安装教程 https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html 默认情况下mysq的 roo ...
- Lua 完美打印数据 (例子)
例子1 : ableprint = function(data,cstring,deepIndex) --第二个参数可以为空,第三个参数不要手动添加,它是用来进行打印深度控制的. if data == ...