hdu 1059 Dividing(多重背包优化)
Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20635 Accepted Submission(s): 5813Problem DescriptionMarsha
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.InputEach
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.
OutputFor
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.
Sample Input1 0 1 2 0 01 0 0 0 1 10 0 0 0 0 0Sample OutputCollection #1:Can't be divided.Collection #2:Can be divided.Source
哎~dp好难..........
题意:每行六个数,若都为零则结束,否则,第几个数代表价值为几的东西有几个,东西数量不超过20万,问所有东西能不能分成两堆价值相等的。
能的话输出 Can..,否则Can't...,每组输出后面有个空行~
多重背包加优化..暂时还是不能深入的理解dp的奥义..烦恼ing...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
#define ll long long using namespace std;
int dp[],w[];
int main(void)
{
int ans,sum,cnt = ;
while(scanf("%d",&w[]) != -)
{
sum = ;
ans = ;
if(w[] != ) ans= ;
sum += w[];
for(int i = ; i <= ; i++)
{
scanf("%d",&w[i]);
sum += w[i]*i;
if(w[i])
ans = ;
}
if(!ans)
break;
if(sum % == )
{
printf("Collection #%d:\nCan't be divided.\n\n",cnt++);
}
else
{
memset(dp,,sizeof(dp));
for(int i = ; i <= w[i] && i <= sum/; i++) dp[i] = ;
for(int i = ; i <= ; i++)
for(int j = sum/; j >= ; j--)
{
if(dp[j] == )
continue;
for(int k = ; k <= w[i] && k *i+j <= sum/; k++)
{
if(dp[k*i+j]) break;
dp[k*i+j] = ;
}
}
if(dp[sum/] == )
printf("Collection #%d:\nCan be divided.\n\n",cnt++);
else
printf("Collection #%d:\nCan't be divided.\n\n",cnt++);
} }
return ;
}
hdu 1059 Dividing(多重背包优化)的更多相关文章
- hdu 1059 Dividing 多重背包
点击打开链接链接 Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
- HDU 1059(多重背包加二进制优化)
http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others) Me ...
- 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 分配(多重背包,母函数)
题意: 两个人共同收藏了一些石头,现在要分道扬镳,得分资产了,石头具有不同的收藏价值,分别为1.2.3.4.5.6共6个价钱.问:是否能公平分配? 输入: 每行为一个测试例子,每行包括6个数字,分别对 ...
- 题解报告:hdu 1059 Dividing(多重背包、多重部分和问题)
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection 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 ...
- ACM学习历程—HDU 1059 Dividing(dp && 多重背包)
Description Marsha and Bill own a collection of marbles. They want to split the collection among the ...
随机推荐
- 【默默努力】h5-game-heroVSmonster
先放下作者大大的项目地址:https://github.com/yangyunhe369/h5-game-heroVSmonster 然后游戏的效果为 截动图的按键与游戏按键应该冲突,我就截几张图片了 ...
- CF755F PolandBalls and Gifts
题意:给你一个礼物的置换.有k个人忘带了礼物.一个人无法获得礼物为他自己没有带礼物或给他带礼物的那个人没有带礼物.求选择k个人,没有获得礼物的人数的最小值和最大值. n,k<=1e6. 标程: ...
- day09 samba、nginx服务配置
samba 1.环境准备 [root@localhost ~]# iptables -F #清除防火墙配置 [root@localhost ~]# systemctl stop firewalld # ...
- 莫烦PyTorch学习笔记(五)——模型的存取
import torch from torch.autograd import Variable import matplotlib.pyplot as plt torch.manual_seed() ...
- 当EntityFramework爱上AutoMapper
有时候相识即是一种缘分,相爱也不需要太多的理由,一个眼神足矣,当EntityFramework遇上AutoMapper,就是如此,恋爱虽易,相处不易. 在DDD(领域驱动设计)中,使用AutoMapp ...
- LOJ#3119 随机立方体
解:极大值至少为1.我们尝试把最大那个数的影响去掉. 最大那个数所在的一层(指一个三维十字架)都是不可能成为最大值的. 考虑容斥.我们试图求除了最大值以外至少有k个极大值的概率. 我们钦定某k个位置是 ...
- Android Studio && NDK开发
Android Studio下载安装网址:http://www.android-studio.org/index.php/download/hisversion 在下载界面可以查看安装包内是否包含SD ...
- BZOJ 1933 [Shoi2007]Bookcase 书柜的尺寸
神奇的dp优化. 考虑6维状态的dp,分别表示三行高和宽,显然MLE&&TLE. 把高排个序,从大到小往架上放,那么若不是重开一行便对高度没有影响. 然后求出宽度的sum,dp[i][ ...
- 分享非常漂亮的WPF界面框架源码及插件化实现原理
在上文<分享一个非常漂亮的WPF界面框架>中我简单的介绍了一个界面框架,有朋友已经指出了,这个界面框架是基于ModernUI来实现的,在该文我将分享所有的源码,并详细描述如何基于Mod ...
- spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
Zuul的核心 Filter是Zuul的核心,用来实现对外服务的控制.Filter的生命周期有4个,分别是“PRE”.“ROUTING”.“POST”.“ERROR”,整个生命周期可以用下图来表示. ...