UVA 562 Dividing coins (01背包)
题意:给你n个硬币,和n个硬币的面值。要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值。
思路:将n个硬币的总价值累加得到sum,
A,B其中必有一人获得的钱小于等于sum/2,另一人获得的钱大于等于sum/2。
因此用sum/2作为背包容量对n个硬币做01背包处理,
所能得到的最大容量即为其中一人获得的钱数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int maxv=*+;
const int maxn=;
int dp[maxv];
int w[maxn];
int n,m,v;
int sum;
int main()
{
scanf("%d",&n);
while(n--){
scanf("%d",&m);
sum=;
for(int i=;i<=m;i++){
scanf("%d",&w[i]);
sum+=w[i];
}
v=sum/;
memset(dp,,sizeof(dp));
for(int i=;i<=m;i++){
for(int j=v;j>=w[i];j--){
dp[j]=max(dp[j],dp[j-w[i]]+w[i]);
}
}
int ans=abs(sum-dp[v]-dp[v]);
printf("%d\n",ans);
}
return ;
}
UVA 562 Dividing coins (01背包)的更多相关文章
- UVA 562 Dividing coins --01背包的变形
01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostre ...
- UVA 562 Dividing coins (01背包)
//平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include ...
- UVA 562 Dividing coins(dp + 01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...
- uva 562 Dividing coins(01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were f ...
- UVA 562 Dividing coins 分硬币(01背包,简单变形)
题意:一袋硬币两人分,要么公平分,要么不公平,如果能公平分,输出0,否则输出分成两半的最小差距. 思路:将提供的整袋钱的总价取一半来进行01背包,如果能分出出来,就是最佳分法.否则背包容量为一半总价的 ...
- UVa 562 - Dividing coins 均分钱币 【01背包】
题目链接:https://vjudge.net/contest/103424#problem/E 题目大意: 给你一堆硬币,让你分成两堆,分别给A,B两个人,求两人得到的最小差. 解题思路: 求解两人 ...
- UVA 562 Dividing coins【01背包 / 有一堆各种面值的硬币,将所有硬币分成两堆,使得两堆的总值之差尽可能小】
It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nic ...
- uva562 Dividing coins 01背包
link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 562 Dividing coins
题目描述:给出一些不同面值的硬币,每个硬币只有一个.将这些硬币分成两堆,并且两堆硬币的面值和尽可能接近. 分析:将所有能够取到的面值数标记出来,然后选择最接近sum/2的两个面值 状态表示:d[j]表 ...
随机推荐
- JavaWeb之 Servlet执行过程 与 生命周期
Servlet的概念 什么是Servlet呢? Java中有一个叫Servlet的接口,如果一个普通的类实现了这个接口,这个类就是一个Servlet.Servlet下有一个实现类叫HttpServle ...
- public、private、protected 与 默认 等访问修饰符
1)public(公共的):被public修饰的属性和方法可以被所有类访问. 2)private(私有的):被private修饰的属性和方法只能在改类的内部使用. 3)protected(受保护的): ...
- 1092. To Buy or Not to Buy (20)
Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy ...
- struts2框架基本操作总结
struts技术说明 一:第一配置开发环境 1.struts.xml文件 <?xml version="1.0" encoding="UTF-8" ?&g ...
- orcherd 汉化
点击这里下载汉化包,此汉化包是在是在前人的基础上精心整理修改的,后续汉化版本随时升级更新. Orchard汉化包 下载后解压缩后打开后看到如下文件夹(App_Data.Core.Modules.The ...
- R 语言中文乱码问题
R 语言似乎在WINDOWS平台上对中文的支持不是特别好,似乎是3.1.2的一个BUG. 目前我研究出了一个临时解决方案,你可以将代码编写成一个函数,从而在调用的过程中不必如下繁琐: 1. 先将本地语 ...
- vhdl基础---分频
偶数分频 ibrary IEEE; use IEEE.STD_LOGIC_1164.ALL; use ieee.std_logic_arith; use ieee.std_logic_unsigned ...
- 给Eclipse提速的7个技巧(转)
本文由 ImportNew - 孙 波翔 翻译自 nicolasbize.欢迎加入翻译小组.转载请参见文章末尾的要求. 大约一个月前,我发表了一篇博客,其中介绍了对Eclipse的爱与恨. 有些人问我 ...
- Introduction to Haskell
"I know why you're here. ...why you hardly sleep, why night after night, you sit by your comput ...
- OC学习笔记之属性详解和易错点
属性的概念在OC1.0中就存在,格式是定义实例变量,然后定义setter和getter方法,用点操作符操作属性 举例,类的接口部分 @interface Father : NSObject { NSI ...