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 ...
随机推荐
- 2.vue插件总结——总有你能用上的插件
UI组件 框架 element - 饿了么出品的Vue2的web UI工具套件 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开源 UI 组件库 Keen-UI - ...
- csp-s模拟65Simple,Walk, Travel,棋盘题解
题面:https://www.cnblogs.com/Juve/articles/11639923.html simple: 考试时只想到的暴力exgcd判断 考虑n,m互质的情况: 我们枚举y,对于 ...
- 淼一淼A+B problem
鲁迅:这可是道难题呢! 鲁迅:我没说过这话,不过确实在理. 某改题毕,但见LOJ之上有数「A+B」之AC记录.余亦尝闻A+B之趣味无穷,遂兴起而码之. 少顷,AC之,吾心所畅. #include< ...
- dijkstra (模板)
突然意识到以前写的都是假的dij,感谢GhostCai神犇. #include<iostream> #include<cstdio> #include<cstring&g ...
- centos7 搭建 php7 + nginx (1)
前言 曾今,写过几篇类似的文章,但是发现几个月后,自己回头再看的时候,有种支离破碎的感觉.自己写的并不全,所以今天打算写一篇比较详细的文档.争取下次环境的减的时候,只需要拷贝复制粘贴即可完成环境搭建. ...
- 04.如何升级扩展以支持Visual Studio 2019
更新.vsixmanifest 我们需要对.vsixmanifest文件进行一些更新.首先,我们必须更新支持的VS版本范围 <InstallationTarget> 这是一个版本,支持Vi ...
- Cesium官方教程9--粒子系统
原文地址:https://cesiumjs.org/tutorials/Particle-Systems-Tutorial/ 粒子系统介绍 这篇教程带你学习Cesium的粒子相关API,比如如何在你的 ...
- Windows下运行Tomcat闪退问题
直接双击startup.bat后闪退,可能的原因是tomcat的配置没有完全正确 完整的tomcat的配置应该配置: 1.JAVA_HOME 2.CATALINA_HOME 3.Path:%CATAL ...
- 用python打造简单的cms识别
代码 #!/usr/bin/env python3 # coding:utf-8 #lanxing #判断代码,判断是否安装requests库 try: import requests except: ...
- mybatis和java一些知识记录
<where> <if test="userName != null and userName != ''"> and user_name like con ...