动态规划--模板--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 ...
随机推荐
- 设计模式--简单工厂(Factory)模式
温故而知新,看了以前写的博文<Asp.net读取Excel文件 2>http://www.cnblogs.com/insus/archive/2011/05/05/2037808.html ...
- (Python学习4)List对象
1.PyListObject对象 typedef struct { PyObject_VAR_HEAD PyObject **ob_item; Py_ssize_t allocated; } PyLi ...
- Dynamics AX 中重点数据源方法
数据源方法 描述 Active 当用户刚选中一行数据时执行该方法.若选中的是主表的数据,也用该方法来触发加载从表符合条件的数据.主要覆盖该方法来根据条件设置记录及其字段是否可见或是否可被编辑. ...
- Win7配置Go环境
最近想学习下Go语言,先从最基本的Hello Go开始,搭建Go开发环境 一.下载Go包 由于Go官网国内访问经常有问题,可以从国内的镜像下载: http://www.golangtc.com/ 二. ...
- POJ 1836 Alignment 最长递增子序列(LIS)的变形
大致题意:给出一队士兵的身高,一开始不是按身高排序的.要求最少的人出列,使原序列的士兵的身高先递增后递减. 求递增和递减不难想到递增子序列,要求最少的人出列,也就是原队列的人要最多. 1 2 3 4 ...
- 终端&作业控制&会话启动过程
进程组 每个进程除了有个进程id外,还属于一个进程组.进程组是一个或者多个进程的集合.通常他们与同一个作业相关联,可以接受来自同一终端的信号.进程组id等于其进程组长id.进程组的终止与进程组长是否存 ...
- Mac OSX 下用 Homebrew 安装 MongoDB 并配置到 WebStorm 中
1. 安装 Ruby OSX 操作系统内置 Ruby,但如果没有 Ruby,则需先输入以下命令安装能够进行多版本ruby环境安装.管理和切换的命令行工具 RVM. 1.1 安装 RVM 打开终端输入以 ...
- 使django与数据库保持长连接
最近遇到一个很蛋疼的问题,写了一个后台管理系统, 由于是后台管理系统,所以使用频率不是很高,当django程序在闲置一段时间后,再次打开后台系统,就变得很慢,然后又好了.查了很多方面,从模板引擎到请求 ...
- VS2010在空解决方案中添加项目
如题,在空解决方案中添加第一个项目的时候会看不到那个solution解决方案文件,而是你当前添加的项目,当你再添加其他项目的时候就悲催了,找不到这个solution,只能在这个项目文件上新加文件,很郁 ...
- SVG基础图形与参数
SVG是什么 SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG 用来定义WEB上使用的矢量图 SVG 使用 XML 格式定义图形 SVG 图像在缩放时其图形质量不 ...