Dividing coins

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背包均分问题)的更多相关文章

  1. HDU1171(01背包均分问题)

    Big Event in HDU Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  2. POJ3211(trie+01背包)

    Washing Clothes Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9384   Accepted: 2997 ...

  3. hihocoder 1038 01背包

    #1038 : 01背包 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说上一周的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励 ...

  4. 1171 Big Event in HDU 01背包

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:把商品分成两半,如不能均分,尽可能的让两个数相接近.输出结果:两个数字a,b且a>=b. ...

  5. UVALive 4870 Roller Coaster --01背包

    题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F ,     D -= K 问在D小于等于一定限度的时 ...

  6. POJ1112 Team Them Up![二分图染色 补图 01背包]

    Team Them Up! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7608   Accepted: 2041   S ...

  7. 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 ...

  8. 51nod1085(01背包)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085 题意: 中文题诶~ 思路: 01背包模板题. 用dp[ ...

  9. *HDU3339 最短路+01背包

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. 使用正則表達式对URL进行解析

    对URL进行解析,一般用到的參数有: 1.协议 如http,https 2.域名或IP 3.port号,如7001,8080 4.Web上下文 5.URI.请求资源地址 6.请求參数 一个URL演示样 ...

  2. erlang 小程序:整数序列,搜索和为正的最长子序列

    近期学习了一下erlang, 编了个小程序 算法例如以下: 把參数分为三个 当前位置的前子序列(Save)(比方 -5, 1,2,-1, _, ... ) 当前位置为_时, 前子序列就是 1,2,-1 ...

  3. hibernate 自动封装

    一.查询全部对象属性的封装则比较简单 Query query = this.getSession().createSQLQuery(queryString).addEntity(pojoClass); ...

  4. hdu 1548

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  5. Java IO 类

    IO包中绝大部分的类都是由以下四个类直接或间接继承来的InputStream OutputStream Reader 还有Writer 其中InputStream和OutputStream代表输入流和 ...

  6. live555 RTSP推送到Darwin出现404错误的解决

    我们将Darwin部署到公网,接收live555 RTSP/RTP推送的时候,经常会出现在SETUP步骤Darwin返回404错误,经过查找原因,主要是Darwin对live555推送的sdp信息中的 ...

  7. bootstrap-table 行内编辑

    1.文件引入 <link rel="stylesheet" href="bootstrap.css"> <link rel="sty ...

  8. ECMAscript 没有对该方法进行标准化,因此反对使用它。 es 日期格式化

    JavaScript substr() 方法 http://www.w3school.com.cn/jsref/jsref_substr.asp 注释:substr() 的参数指定的是子串的开始位置和 ...

  9. Splits a tensor into sub tensors

    https://www.tensorflow.org/api_docs/python/tf/split # 'value' is a tensor with shape [5, 30] # Split ...

  10. 将本地项目上传到git远程库(初始化)

    准备条件: 首先,有一个远程仓库地址,本文中的地址为“http://git.xxxxxxxx.net.cn/jacun/imagegrap.git”; 第一步:在本地创建初始化仓库: git init ...