Dividing coins 

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 很简单的01背包问题,将n个硬币的总价值累加得到sum,再用sum/2作为背包容量对n个硬币做01背包处理,看能组成的最大容量是多少。 题意:第一个数代表有多少组数据,第二行的数代表有几个硬币,第三行数分别代表每个硬币的价值。将这些硬币分成两堆,尽可能地平衡,求最小价值差为多少。 附上代码:
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int abs(int a)
{
if(a>) return a;
return -a;
}
int main()
{
int n,m,i,j;
int a[],dp[];
scanf("%d",&n);
while(n--)
{
int sum=,s;
scanf("%d",&m);
for(i=; i<m; i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
s=sum/; //求平均值作为背包容量,求能组成的最大背包容量
memset(dp,,sizeof(dp));
for(i=; i<m; i++)
for(j=s; j>=a[i]; j--)
if(dp[j]<dp[j-a[i]]+a[i])
dp[j]=dp[j-a[i]]+a[i];
printf("%d\n",abs(sum-dp[s]-dp[s])); //dp[s]为最接近平均值的容量,减去两个它的绝对值,则为最小价值差
}
return ;
}

uva 562 Dividing coins(01背包)的更多相关文章

  1. UVA 562 Dividing coins --01背包的变形

    01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostre ...

  2. UVA 562 Dividing coins (01背包)

    //平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include ...

  3. UVA 562 Dividing coins(dp + 01背包)

    Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...

  4. UVA 562 Dividing coins (01背包)

    题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum,   A,B其中必有一人获得的钱小于等于sum/2 ...

  5. UVA 562 Dividing coins 分硬币(01背包,简单变形)

    题意:一袋硬币两人分,要么公平分,要么不公平,如果能公平分,输出0,否则输出分成两半的最小差距. 思路:将提供的整袋钱的总价取一半来进行01背包,如果能分出出来,就是最佳分法.否则背包容量为一半总价的 ...

  6. UVa 562 - Dividing coins 均分钱币 【01背包】

    题目链接:https://vjudge.net/contest/103424#problem/E 题目大意: 给你一堆硬币,让你分成两堆,分别给A,B两个人,求两人得到的最小差. 解题思路: 求解两人 ...

  7. UVA 562 Dividing coins【01背包 / 有一堆各种面值的硬币,将所有硬币分成两堆,使得两堆的总值之差尽可能小】

    It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nic ...

  8. uva562 Dividing coins 01背包

    link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVA 562 Dividing coins

    题目描述:给出一些不同面值的硬币,每个硬币只有一个.将这些硬币分成两堆,并且两堆硬币的面值和尽可能接近. 分析:将所有能够取到的面值数标记出来,然后选择最接近sum/2的两个面值 状态表示:d[j]表 ...

随机推荐

  1. LintCode刷题笔记--Longest Increasing Subsequence

    标签: 动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code s ...

  2. Linux C/C++开发

    首先就是要熟练在vim里面写代码,其实就是没有提示和自动补全了,这个问题并不大. 我服务器gcc版本是4.8.5,所以就按照这个来了 https://gcc.gnu.org/onlinedocs/gc ...

  3. Spring Boot → 08:嵌入式Servlet容器自定义

    Spring Boot → 08:嵌入式Servlet容器自定义

  4. Python数据分析与展示[第三周](pandas简介与数据创建)

    第三周的课程pandas 分析数据 http://pandas.pydata.org import pandas as pd 常与numpy matplotlib 一块定义 d=pd.Series(r ...

  5. 【水滴石穿】react-native-book

    先推荐一个学习的地址:https://ke.qq.com/webcourse/index.html#cid=203313&term_id=100240778&taid=12778558 ...

  6. JQuery-- 实例:小米左右切图,淡入淡出,自动,小圆点触发轮播图

    示意图: demo <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  7. jquery的innerWidth 和 innerHeight的使用

    innerWidth This method returns the width of the element, including left and right padding, in pixels ...

  8. R语言数据分析利器data.table包—数据框结构处理精讲

    R语言数据分析利器data.table包-数据框结构处理精讲 R语言data.table包是自带包data.frame的升级版,用于数据框格式数据的处理,最大的特点快.包括两个方面,一方面是写的快,代 ...

  9. MSSQL→ 03:数据库操作

    一.数据库的操作 1.1.新增 使用SSMS图形界面创建数据库 在SQL Server 2008 中,通过SQL Server Management Studio 创建数据库 使用Transact-S ...

  10. database homework1

    mysql> select * from teacher_info; +----+------+-----+--------+---------+ | id | name | age | sal ...