uva 562
Description

It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nickel, which was made of copper. They were both so eager to get it and the fighting was so fierce, they stretched the coin to great length and thus created copper-wire.
Not commonly known is that the fighting started, after the two Dutch tried to divide a bag with coins between the two of them. The contents of the bag appeared not to be equally divisible. The Dutch of the past couldn't stand the fact that a division should favour one of them and they always wanted a fair share to the very last cent. Nowadays fighting over a single cent will not be seen anymore, but being capable of making an equal division as fair as possible is something that will remain important forever...
That's what this whole problem is about. Not everyone is capable of seeing instantly what's the most fair division of a bag of coins between two persons. Your help is asked to solve this problem.
Given a bag with a maximum of 100 coins, determine the most fair division between two persons. This means that the difference between the amount each person obtains should be minimised. The value of a coin varies from 1 cent to 500 cents. It's not allowed to split a single coin.
Input
A line with the number of problems n, followed by n times:
- a line with a non negative integer m (
) indicating the number of coins in the bag
- a line with m numbers separated by one space, each number indicates the value of a coin.
Output
The output consists of n lines. Each line contains the minimal positive difference between the amount the two persons obtain when they divide the coins from the corresponding bag.
Sample Input
2
3
2 3 5
4
1 2 4 6
Sample Output
0
1
dp题,dp[i]的只有 0和1,0表示不能组成钱数i,1表示可以。转移方程:if(dp[i]) dp[j+coin[i]]=1; 这样把所有的可能组成结果找出来,
然后从sum/2向下找(向上也行),第一个找到的就是所求的结果,表示某一个人能过拿到的钱数,然后进行进一步计算。
#include<bits/stdc++.h>
using namespace std;
int dp[*+];
int main()
{
int n;
cin>>n;
int coin[];
while (n--)
{
memset(dp,,sizeof(dp));
long long int sum=;
int m;
cin>>m;
for(int i=;i<m;i++)
{
cin>>coin[i];
sum+=coin[i];
}
dp[]=;
for(int i=;i<m;i++)
{
for(int j=sum;j>=;j--)
if(dp[j])
dp[j+coin[i]]=;
}
int j=sum/;
while(!dp[j]) j--;
printf("%d\n",abs(sum-*j)); }
return ;
}
uva 562的更多相关文章
- UVA 562(01背包)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=s ...
- UVA 562 Dividing coins --01背包的变形
01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostre ...
- UVA 562 Dividing coins (01背包)
题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum, A,B其中必有一人获得的钱小于等于sum/2 ...
- UVA 562 Dividing coins
题目描述:给出一些不同面值的硬币,每个硬币只有一个.将这些硬币分成两堆,并且两堆硬币的面值和尽可能接近. 分析:将所有能够取到的面值数标记出来,然后选择最接近sum/2的两个面值 状态表示:d[j]表 ...
- 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背包)
//平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include ...
- 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 ...
- UVA 562 Dividing coins 分硬币(01背包,简单变形)
题意:一袋硬币两人分,要么公平分,要么不公平,如果能公平分,输出0,否则输出分成两半的最小差距. 思路:将提供的整袋钱的总价取一半来进行01背包,如果能分出出来,就是最佳分法.否则背包容量为一半总价的 ...
随机推荐
- Android开源项目第二篇——工具库篇
本文为那些不错的Android开源项目第二篇——开发工具库篇,**主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容 ...
- HDU 1018 Big Number (数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 解题报告:输入一个n,求n!有多少位. 首先任意一个数 x 的位数 = (int)log10(x ...
- Xen虚拟机克隆实战
导读 在我们使用Xen虚拟化的时候,会经常创建虚拟机(VM),每次安装创建步骤比较繁琐,本文介绍通过virt-clone命令克隆xen虚拟机实战. 查看virt-clone命令是否存在 rpm -qa ...
- OpenCV入门(二)
这次主要学习了下滤波,就当复习了图像处理的知识了:http://blog.csdn.net/poem_qianmo/article/details/22745559 代码实现比较简单,但关于通过滚动条 ...
- centos安装redis及php-redis扩展
centos安装redis及php-redis扩展 Linux, WEB 七162012 今天公司同事要求在测试机上安装redis,并且要求让php安装上redis的扩展,redis是一个key-v ...
- 18个网站SEO建议
第一位专家是Autotrader公司的搜索市场经理Dewi Nawasari,她认为SEO就是优化网站,以吸引你的目标客户的过程.她的建议如下: 1.创建良好的引导链接 要把用户的使用过程尽量的简化, ...
- POJ 3083
---恢复内容开始--- http://poj.org/problem?id=3083 题目大意就是给你要你从S走到E,且只有.代表的地方才是可以走的,有三种方式的走法. 一.是向左优先转,从S到E的 ...
- iOS 关于Layer的疑问
很久很久以前,就对ios的Layer十分的不解,学习了android后,打算通过android中的相关实现,分析一下ios中layer的作用,结果没有找到android中的对应的内容!十分让人郁闷.于 ...
- (转)SQL Server 的事务和锁(二)-Range S-S锁
在这篇随笔中,我们的主要关注点在 Key-Range Lock.Key-Range Lock有 S-S.S-U.I-N.X-X几种情况.我们一个一个来说,力求明白.遗憾的是,这里可能会比较冗长,那么死 ...
- 转MYSQL学习(五) 索引
索引是在存储引擎中实现的,因此每种存储引擎的索引都不一定完全相同,并且每种存储引擎也不一定支持所有索引类型. 根据存储引擎定义每个表的最大索引数和最大索引长度.所有存储引擎支持每个表至少16个索引,总 ...