hdu 1059 Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19391 Accepted Submission(s): 5446
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.
line in the input describes one collection of marbles to be divided.
The lines consist of six non-negative integers n1, n2, ..., 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.
each colletcion, 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.
Collection #2:
Can be divided.
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std ;
int dp[] ;
int r[] ;
int sum, num ;
int c = ;
void _zeroone(int c, int w)//0-1背包处理单件物品
{
for(int i = sum; i >= c; --i)
dp[i] = max(dp[i], dp[i - c] + w) ;
}
void _compl(int c, int w)//完全背包处理单件物品
{
for(int i = c; i <= sum; ++i)
dp[i] = max(dp[i], dp[i - c] + w) ;
}
void _mult(int c, int w, int amount)
{
if(c * amount >= sum) { _compl(c, w) ; return ; }//满足条件时,该件物品可以直接当完全背包时的单件物品处理
int k = ;
while(k < amount)//二进制分解
{
_zeroone(c * k,w * k) ;
amount -= k ;
k = k * ;
}
_zeroone(amount * c, amount * w) ;
}
int main()
{
while()
{
memset(dp, , sizeof dp) ;
sum = ;
int flag = ;
for(int i = ; i <= ; ++i){
scanf("%d",&r[i]) ;
if(r[i] != ) flag = ;
sum += i * r[i] ;
}
if(!flag) break ;
printf("Collection #%d:\n",c++) ;
if(sum & ) { printf("Can't be divided.\n\n") ; continue ;}
sum /= ;
for(int i = ; i <= ; ++i)//将多重背包每件分开转换为完全背包或0-1背包中的单件物品处理
_mult(i,i,r[i]) ;
if(dp[sum] == sum ) printf("Can be divided.\n") ;
else printf("Can't be divided.\n") ;
printf("\n") ;
}
return ;
}
hdu 1059 Dividing的更多相关文章
- HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)
HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...
- hdu 1059 Dividing bitset 多重背包
bitset做法 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a ...
- 动态规划--模板--hdu 1059 Dividing
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 1059 Dividing 多重背包
点击打开链接链接 Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1059 Dividing 分配(多重背包,母函数)
题意: 两个人共同收藏了一些石头,现在要分道扬镳,得分资产了,石头具有不同的收藏价值,分别为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 ...
- HDU 1059 Dividing(多重背包)
点我看题目 题意: 将大理石的重量分为六个等级,每个等级所在的数字代表这个等级的大理石的数量,如果是0说明这个重量的大理石没有.将其按重量分成两份,看能否分成. 思路 :一开始以为是简单的01背包,结 ...
- 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 ...
- HDU 1059 Dividing (dp)
题目链接 Problem Description Marsha and Bill own a collection of marbles. They want to split the collect ...
随机推荐
- 【leetcode】Merge Intervals(hard)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- php数据访问基础
建一个连接(造一个连接对象) $db = new MySqli("IP地址或者域名,若果是本地则用localhost","用户名","数据库密码&qu ...
- 如何用Jquery判断在键盘上敲的哪个按键
有时候我们需要判断我们在键盘上敲了哪个键,这个需要查询下键盘上的键对应的值是多少,比如Enter键是13. 下面是Jquery代码,别忘了引用Jquery包哈. <script type=&qu ...
- struts2响应AJAX
1发送ajax请求使用stream进行响应 Result的type属性的stream取值. 1.1定义Action public class UserAction { private String u ...
- August 10th, 2016, Week 33rd, Wednesday
The degree of loving is measured by the degree of giving. 爱的深浅是用给与的多少来衡量的. Some say that if you love ...
- 有关Java的优秀博客集锦
1. 在java编程中,多线程并发总有些疑惑:如为什么会产生并发?并发会有什么影响?java中提供了哪些处理并发的技术(机制) 关于并发产生的原因,我查了一些资料目前发现有两种原因:一,存在共享的资源 ...
- volatile和const
volatile可理解为“编译器警告指示字” volatile用于告诉编译器必须每次去内存中取变量值 volatile主要修饰可能被多个线程访问的变量 volatile也可以修饰可能被未知因数更改的变 ...
- 常用shell命令操作
1.找出系统中所有的*.c 和*.h 文件 (-o 或者) $find / -name "*.cpp" -o -name "*.h" 2.设定 eth0 的 I ...
- Hadoop组件之-HDFS(HA实现细节)
NameNode 高可用整体架构概述 在 Hadoop 1.0 时代,Hadoop 的两大核心组件 HDFS NameNode 和 JobTracker 都存在着单点问题,这其中以 NameNode ...
- win7下python3.4 ImportError: No module named 'MySQLdb'错误解决方法
首先,安装PyMySQL C:\Users\fnngj>python -m pip install PyMySQL 执行以下命令会报错: ImportError: No module named ...