UVA562(01背包均分问题)
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
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
题意:给出n个硬币,将这些硬币分给两个人,使这两个人的硬币面值之差最小。
思路:尽可能的将硬币的面值平均分。
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
int n;
int w[MAXN];
int dp[MAXN];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(dp,,sizeof(dp));
scanf("%d",&n);
int sum=;
for(int i=;i<n;i++)
{
scanf("%d",&w[i]);
sum+=w[i];
}
for(int i=;i<n;i++)
{
for(int j=sum/;j>=w[i];j--) dp[j]=max(dp[j],dp[j-w[i]]+w[i]);
}
printf("%d\n",abs(sum-*dp[sum/]));
} return ;
}
UVA562(01背包均分问题)的更多相关文章
- HDU1171(01背包均分问题)
Big Event in HDU Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- POJ3211(trie+01背包)
Washing Clothes Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9384 Accepted: 2997 ...
- hihocoder 1038 01背包
#1038 : 01背包 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说上一周的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励 ...
- 1171 Big Event in HDU 01背包
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:把商品分成两半,如不能均分,尽可能的让两个数相接近.输出结果:两个数字a,b且a>=b. ...
- UVALive 4870 Roller Coaster --01背包
题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F , D -= K 问在D小于等于一定限度的时 ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- 51nod1085(01背包)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085 题意: 中文题诶~ 思路: 01背包模板题. 用dp[ ...
- *HDU3339 最短路+01背包
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- python(14)- 简单练习:登录账户失败三次,账户自动锁定
题目需求: 1.输入用户名密码 2.认证成功后显示欢迎信息 3.输错三次后锁定 #读取注册用户的信息,用户名,密码,输错次数,写入字典中 user={} with open("D ...
- xhtml三种元素类型
xhtml三种元素类型:块级元素/内联元素/可变元素 块级元素:独占一行.可自定义自己的宽度和高度.作为其他元素的容器,可容纳其他内联元素和块级元素,喻做一个盒子.内联元素:始终以行内逐个显示.不能设 ...
- ffmpeg 内存池
ffmpeg 部分内存管理采用 了内存池技术.基本的接口在libavutil目录下的buffer.c文件中实现: 1. av_buffer_pool_init 初始化 内存池 2 av_buffer_ ...
- project 2013 激活 key 7YHNW-RVCQY-VBDB2-QX69Q-B96WK viso 66DNF-28W69-W4PPV-W3VYT-TJDBQ
project 2013 激活 key :7YHNW-RVCQY-VBDB2-QX69Q-B96WK viso2013 激活 key:66DNF-28W69-W4PPV-W3VYT-TJDBQ 软件 ...
- 利用CH341A编程器刷新BIOS,恢复BIOS,妈妈再也不用担心BIOS刷坏了
前几天,修电脑主析就捣鼓刷BIOS,结果刷完黑屏开不了机,立刻意识到完了,BIOS刷错了.就从网上查资料,各种方法试了个遍,什么用处都没有.终于功夫不负有心人,找到了编码器,知道了怎么用.下面看看具体 ...
- SpringSecurity学习笔记(一):搭建最简单的SpringSecurity应用
学习过程参考自:http://www.mossle.com/docs/auth/html/pt01-quickstart.html 一.搭建Maven项目: 所需引用的jar包如下: pom.xml文 ...
- 透视WPF 应用程序的利器
当我们看到一些设计新颖的网站时,可以借助浏览器自带的Inspector 工具或插件方便的浏览网站布局结构及逻辑.如果是WPF 应用程序能否看到控件的架构方式呢?本篇将介绍两款工具Snoop 和WPF ...
- HBase GC日志
HBase依靠ZooKeeper来感知集群成员及其存活性.假设一个server暂停了非常长时间,它将无法给ZooKeeper quorum发送心跳信息,其他server会觉得这台server已死亡.这 ...
- FFMPEG SDK流媒体开发2---分离.mp4等输入流音视频而且进行解码输出
对于FFMPEG SDK 提供的Demuxing 为我们实现多路复用 提供了非常多方便,以下的案案例 实现的是 分离一个媒体文件的音频 视频流 而且解码输出 到 不同的文件里. 对于音频被还原回 ...