UVALive 5583 Dividing coins
Dividing coins
This problem will be judged on UVALive. Original ID: 5583
64-bit integer IO format: %lld Java class name: Main
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 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
Sample Input
2
3
2 3 5
4
1 2 4 6
Sample Output
0
1
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int dp[],val[],T,n,sum;
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
memset(dp,,sizeof dp);
for(int i = sum = ; i < n; ++i) {
scanf("%d",val+i);
sum += val[i];
}
int m = sum>>;
for(int i = ; i < n; ++i)
for(int j = m; j >= val[i]; --j)
dp[j] = max(dp[j],dp[j-val[i]] + val[i]);
printf("%d\n",sum - (dp[m]<<));
}
return ;
}
UVALive 5583 Dividing coins的更多相关文章
- UVA 562 Dividing coins(dp + 01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- 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背包 / 有一堆各种面值的硬币,将所有硬币分成两堆,使得两堆的总值之差尽可能小】
It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nic ...
- 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背包的变形
01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostre ...
- uva562 Dividing coins 01背包
link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 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]表 ...
随机推荐
- TP5 上传文件
直接贴上一个完整的代码 /** * 图片上传方法 * @return [type] [description] */ /** * 1 获取到文件 * 2 验证文件的形状是不是符合上传的规则 * 3 i ...
- 前端之CSS属性相关
宽和高 width属性可以为元素设置宽度. height属性可以为元素设置高度. 块级标签才能设置宽度,内联标签的宽度由内容来决定. 字体属性 文字字体 font-family可以把多个字体名称作为一 ...
- notepad++调用python3中文乱码
使用notepad++,配置好快捷键调用python3,一切就绪,仿佛就差代码了,结果一使用, 中文乱码,一直没有好的解决办法. 最后只能在代码中增加一行重写向输出解决,示例如下: #!/usr/bi ...
- SweetAlert的入门
在做后台管理系统,在用户交互这块(弹窗.提示相关),用了一款还不错的插件SweetAlert(一款原生js提示框,允许自定义,支持设置提示框标题.提示类型.确认取消按钮文本.点击后回调函数等等), 效 ...
- Java Web乱码分析及解决方式(二)——POST请求乱码
引言 GET请求的本质表现是将请求參数放在URL地址栏中.form表单的Method为GET的情况.參数会被浏览器默认编码,所以乱码处理方案是一样的. 对于POST请求乱码.解决起来要比GET简单.我 ...
- Spring MVC数据转换
样例:把一个字符串封装而一个对象. 如:username:password格式的数据ZhangSan:1234.我们把这个数据封装成一个User对象.以下分别使用属性编辑器与转换器来实现. 1.自己定 ...
- windows開始菜单和任务栏图标显示空白而且点击时候显示项目已被移动或删除
这几天实验室老常常自己主动断电.这是非常蛋疼的一件事,这不上次断电就出事了.来电后开机,点击任务栏上的程序全都显示为无法打开此项目,该项目已被移动.删除.原因是图标缓存丢失,可能是突然断电引起的,也有 ...
- django 笔记4 数据库操作
django操作数据库 orm操作 对应关系 models.tb.objects.filter(id__gt=) models.tb.objects.filter(id=) models.tb.obj ...
- SGU 461 Wiki Lists dfs
不难的题,不过蛮有意思的dfs #include <iostream> #include <cstdio> #include <fstream> #include ...
- 基于 Web 的 Go 语言 IDE - Wide 1.2.0 发布!
Wide 是什么 Wide 是一个基于 Web 的 Go 语言团队 IDE. 在线开发:打开浏览器就可以进行开发.全快捷键 智能提示:代码自动完成.查看表达式.编译反馈.Lint 实时运行:极速编译. ...