动态规划--模板--hdu 1059 Dividing
Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14861 Accepted Submission(s): 4140
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.
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 a blank line after each test case.
1 0 0 0 1 1
0 0 0 0 0 0
Can't be divided.
Collection #2:
Can be divided.
题意:
#include "stdio.h"
#include "string.h"
#define N 120005
int a[],map[N];
int sum; int MAX(int x,int y)
{
if(x>y) return x;
return y;
} void CompletePack(int cost,int weight) //完全背包
{
for(int i=cost;i<=sum;i++)
map[i] = MAX(map[i],map[i-cost]+weight);
} void ZeroOnePack(int cost,int weight) // 01背包
{
for(int i=sum;i>=cost;i--)
map[i] = MAX(map[i],map[i-cost] + weight);
} void dp(int cost,int weight,int k)
{
if(cost*k>=sum)
CompletePack(cost,weight);
else
{
for(int j=;j<k; ) //二进制优化
{
ZeroOnePack(j*cost,j*weight);
k-=j;
j*=;
}
ZeroOnePack(k*cost,k*weight);
}
} int main()
{
int i,p=;
while()
{
sum=;
for(i=;i<=;i++)
{
scanf("%d",&a[i]);
sum+=a[i]*i;
}
if(sum==) return ;
printf("Collection #%d:\n",p++);
if(sum%==)
{
printf("Can't be divided.\n\n");
continue;
}
sum=sum/;
memset(map,,sizeof(map));
for(i=;i<=;i++)
dp(i,i,a[i]);
if(map[sum]==sum)
printf("Can be divided.\n\n");
else
printf("Can't be divided.\n\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 & 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 多重背包
点击打开链接链接 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
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1059 Dividing(多重背包)
点我看题目 题意: 将大理石的重量分为六个等级,每个等级所在的数字代表这个等级的大理石的数量,如果是0说明这个重量的大理石没有.将其按重量分成两份,看能否分成. 思路 :一开始以为是简单的01背包,结 ...
- HDU 1059 Dividing (dp)
题目链接 Problem Description Marsha and Bill own a collection of marbles. They want to split the collect ...
随机推荐
- ASP.NET MVC5利用EF,反向自动生成数据库
1.在Model类里面,写好相应的属性. using System; using System.Collections.Generic; using System.Linq; using System ...
- JS代码的位置与事件响应代码块的封装问题
JS代码的位置 我们可以将JavaScript代码放在html文件中任何位置,但是我们一般放在网页的head或者body部分. 放在<head>部分最常用的方式是在页面中h ...
- DataList分页访问FooterTemplate模板里的控件
今天做DataList分页的时候,突然想把分页控件写在FooterTemplate模板里面,弄了很久都访问不到控件,终于发现问题所在,以下是访问FooterTemplate里控件的方法: <Fo ...
- csharp: Flash Player play *.flv file in winform
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- HTML页面导航栏页脚不动,变换中间部分
代码段一: <script> $(document).ready(function() { $("#teachingObjectives").click(functio ...
- $(document).ready()即$()方法和window.onload方法的比较
以浏览器装载文档为例,我们都知道在页面完毕后,浏览器会通过JavaScript为DOM元素添加事件.在常规的JavaScript代码中,通常使用window.onload方法,而在jQuery中,使用 ...
- Windows程序控件升级==>>构建布局良好的Windows程序
01.菜单栏(MenuStrip) 01.看看这就是menuStrip的魅力: 02.除了一些常用的属性(name.text..)外还有: 03.有人会问:上图的快捷键: 方法: 方式一:1.设置菜单 ...
- IT外包行业与职业发展
在IT行业,总是有一些IT外包公司的存在,凡是存在的都是合理的.当你做为IT从业人员应该尽量避免去外包公司工作 .特别是你从事软件开发工作. 先来说说缘由,一些外包公司本来是从事软 ...
- mysql performance_schema/information_schema授权问题
mysql> grant all on performance_schema.* to 'testuser'@'%';ERROR 1044 (42000): Access denied for ...
- SharpGL学习笔记(十八) 解析3ds模型并显示
笔者设想的3D仿真中的元件,是不可能都是“画”出来的.这样就玩复杂了,应该把任务分包出去,让善于制作模型的软件来制作三维模型,我们只需要解析并且显示它即可. 3dsmax制作三维模型的方便,快捷,专业 ...