UVA 562 Dividing coins(dp + 01背包)

时间:2021-05-06 09:03:49
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 (UVA 562 Dividing coins(dp + 01背包)) 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

题意:一堆硬币分给两个人,要求两个人得到钱差值最少,输出这个差值。

思路:01背包。硬币总价值为sum,一个人分到i,另一个人肯定分到sum - i,如果硬币尽量平分是最好的,先用01背包求出所有可能组成的值,然后从sum / 2开始找,找到一个可以组成的值作为i,他们的差值为sum - 2 * i。

代码:

#include <stdio.h>
#include <string.h> int t, n, coin[105], sum, i, j, dp[50005];
int main() {
scanf("%d", &t);
while (t --) {
scanf("%d", &n);
sum = 0;
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (i = 0; i < n; i ++) {
scanf("%d", &coin[i]);
sum += coin[i];
}
for (i = 0; i < n; i ++)
for (j = sum; j >= coin[i]; j --) {
if (dp[j - coin[i]])
dp[j] = 1;
}
for (i = sum / 2; i >= 0; i --)
if (dp[i]) {
printf("%d\n", sum - i * 2);
break;
}
}
return 0;
}

UVA 562 Dividing coins(dp + 01背包)的更多相关文章

  1. uva 562 Dividing coins(01背包)

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

  2. UVA 562 Dividing coins (01背包)

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

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

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

  4. HDOJ&lpar;HDU&rpar;&period;3466 Dividing coins &lpar; DP 01背包 无后效性的理解&rpar;

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

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

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

  6. UVA 562 Dividing coins &lpar;01背包&rpar;

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

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

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

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

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

  9. Dividing coins (01背包)

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

随机推荐

  1. 64位系统上使用PLSQL Dervloper解决方案

    win7+64位+Oracle+11g+64位下使用PLSQL+Developer+的解决办法  2012-04-15 01:28:37|  分类: 默认分类 |  标签: |字号大中小 订阅 . w ...

  2. 生产者消费者模式--阻塞队列--LOCK&comma;Condition--线程池

    1.阻塞队列:http://www.cnblogs.com/dolphin0520/p/3932906.html 2.Condition 生产者消费者实现 :http://www.cnblogs.co ...

  3. &lpar;转&rpar;Android-Mac电脑如何进行APK反编译-使用apktool、jd-gui

    最近群里有人问如何在mac下进行apk反编译,我也没试过,以前都是在windows下进行反编译的,windows下很简单,有许多比较好的集成工具,如apkide(改之理),不过我猜mac下应该和win ...

  4. RealSense 3D实感体验:前景广阔目前应用少

    腾讯数码讯(周硕)在去年的IDF大会上,英特尔着重展示了其全新的RealSense 3D实感技术,而厚度仅6mm堪称史上最薄平板的戴尔Venue 8 7000也成为首个搭载RealSense技术的产品 ...

  5. SSD 和 SAS 意外造 raid 1

    一台机器的磁盘更换后, 一个 SSD 和 一个 SAS 做了 raid 1 , 诡异情况,询问 IDC 同事中. 有可能是打开了 热备.

  6. bug记录&lowbar;document&period;defaultview&period;getcomputedstyle&lpar;&rpar;

    页面中使用document.defaultview.getcomputedstyle()在火狐下取不到值. 原本方法现在$(document).ready()中,换到window.onload里就可以 ...

  7. 1782&colon; &lbrack;Usaco2010 Feb&rsqb;slowdown 慢慢游

    1782: [Usaco2010 Feb]slowdown 慢慢游 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 570  Solved: 346[Sub ...

  8. &num;&num; Python中的Package和Jupyter中import包问题

    前言 关于python包的一些知识 Java中的package概念 我们知道在java中的import package概念, java中的包就是一个目录,里面包含着子目录,子目录套着子目录,当需要引入 ...

  9. error&colon; The requested URL returned error&colon; 401 Unauthorized while accessing

    我遇到的其中一个问题. 问题描述: 在git push -u origin master是,提示“error: The requested URL returned error: 401 Unauth ...

  10. SEO中TDK写法的意思以及注意事项

    在SEO中,所谓的TDK其实就是title.description.keywords这三个标签,这三个标签在网站的优化过程中,至关重要所以今天童童来和大家分享下,如何去写好TDK标签! 1.title ...