hdu1059&poj1014 Dividing (dp,多重背包的二分优化)
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.
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.
``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.
#include <bits/stdc++.h> using namespace std;
int num[],sum,v,cnt,i,j;
bool dp[];//dp[i]表示容量为i的背包能否装满
int main()
{
//freopen("de.txt","r",stdin);
int casenum=;
while ()
{
memset(dp,,sizeof dp);
memset(num,,sizeof num);
sum=;
for (i=;i<=;++i)
{
scanf("%d",&num[i]);
sum+=i*num[i];
}
if (sum==)
break;
printf("Collection #%d:\n",casenum++);
if (sum%)//如果所有石头总价值和为奇数肯定不能均分
{
printf("Can't be divided.\n\n");
continue;
}
else
{
v=sum/;//用总价值的一半来作为背包的容量
dp[]=;
for (i=;i<=;++i)
{
if (num[i]==)
continue;
for (j=;j<=num[i];j*=)//j来作为2^n的单位
{
cnt=i*j;
for (int k=v;k>=cnt;--k)
{
if (dp[k-cnt])//只有k-cnt的背包被装满,k的背包才能被装满
dp[k]=;
}
num[i]-=j;//分完2^n剩下的个数
}
cnt=num[i]*i;
if (cnt)
{
for (int k=v;k>=cnt;--k)
{
if (dp[k-cnt])
dp[k]=;
}
}
}
if (dp[v])//容量为v的背包能否被装满
printf("Can be divided.\n\n");
else
printf("Can't be divided.\n\n");
}
}
return ;
}
hdu1059&poj1014 Dividing (dp,多重背包的二分优化)的更多相关文章
- HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)
HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...
- 硬币问题 (dp,多重背包的二分优化)
题目描述 给你n种硬币,知道每种的面值Ai和每种的数量Ci.问能凑出多少种不大于m的面值. 输入 有多组数据,每一组第一行有两个整数 n(1≤n≤100)和m(m≤100000),第二行有2n个整数, ...
- poj1014 Dividing (多重背包)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=1014">http://poj.org/problem?id=1014 Descrip ...
- CodeForces922E DP//多重背包的二进制优化
https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法 ...
- POJ1014:Dividing(多重背包)
http://poj.org/problem?id=1014 Description Marsha and Bill own a collection of marbles. They want to ...
- HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)
HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...
- HDOJ(HDU).2191. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 (DP 多重背包+二进制优化)
HDOJ(HDU).2191. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 (DP 多重背包+二进制优化) 题意分析 首先C表示测试数据的组数,然后给出经费的金额和大米的种类.接着是每袋大米的 ...
- HDU 1059(多重背包加二进制优化)
http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others) Me ...
- 动态规划:HDU2844-Coins(多重背包的二进制优化)
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
随机推荐
- 十条服务器端优化Web性能的技巧
服务器 远程桌面连接工具 提高web应用的性能从来没有比现在更重要过.网络经济的比重一直在增长;全球经济超过5%的价值是在因特网上产生的(数据参见下面的资料).这个时刻在线的超连接世界意味着用户对其 ...
- JavaScript中的回调地狱及解决方法
1.回调地狱 在使用JavaScript时,为了实现某些逻辑经常会写出层层嵌套的回调函数,如果嵌套过多,会极大影响代码可读性和逻辑,这种情况也被成为回调地狱.比如说你要把一个函数 A 作为回调函数,但 ...
- Codeforces 803E--Roma and Poker (DP)
原题链接:http://codeforces.com/problemset/problem/803/E 题意:给一个n长度的字符串,其中'?'可以替换成'D'.'W'.'L'中的任意一种,'D'等价于 ...
- window 安装VisualSvn Server
VisualSvn Server 1 .VisualSvn Server 介绍和下载 VisualSvn Server是免费的,而VisualSvn是收费的.VisualSvn是Svn的客户端,和V ...
- [CSP-S模拟测试]:Star Way To Heaven(最小生成树Prim)
题目描述 小$w$伤心的走上了$Star\ way\ to\ heaven$. 到天堂的道路是一个笛卡尔坐标系上一个$n\times m$的长方形通道(顶点在$(0,0)$和$(n,m)$),小$w$ ...
- exception 打印出异常栈踪迹
Java异常抛出使用e.printStackTrace(),打印出抛出的异常栈踪迹, 如果你在catch中继续抛出这个异常,那么e.printStackTrace()也能跟踪到抛出异常的地方, 使用t ...
- 加载的DAL数据访问层的类型
using System; using System.Collections; using System.Reflection; using CSFrameworkV4_5.Core; using C ...
- webpack请求文件路径代理
在开发模式下,访问页面请求会跑到根路径,因为写的都 ./images 而index又在根目录, 所以访问地址会是 http://localhost:8080/images/abc.jpg 而实际 ...
- 洛谷 P1119 灾后重建——dijstra
先上一波题目 https://www.luogu.org/problem/P1119 这道题我们可以将询问按时间排序 然后随着询问将相应已经重建成功的点进行操作 每次更新一个点就以他为起点跑一遍dij ...
- matplotlib系列——折线图
import numpy as np import matplotlib.pyplot as plt import matplotlib #数据准备 #numpy.linspace(start, st ...