Dividing coins

Time Limit: 3000ms
Memory Limit: 131072KB

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

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的更多相关文章

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

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

  2. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

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

  3. uva 562 Dividing coins(01背包)

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

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

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

  5. Dividing coins (01背包)

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

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

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

  7. uva562 Dividing coins 01背包

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

  8. UVA 562 Dividing coins (01背包)

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

  9. UVA 562 Dividing coins

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

随机推荐

  1. HDU-2303 The Embarrassed Cryptographer 高精度算法(大数取模)

    题目链接:https://cn.vjudge.net/problem/HDU-2303 题意 给一个大数K,和一个整数L,其中K是两个素数的乘积 问K的是否存在小于L的素数因子 思路 枚举素数,大数取 ...

  2. Hexo构建Blog系列

    Hexo是一个开源构建blog框架,基于nodejs研发.可以自由切换主题,插件等功能,实现自已酷炫博客需求. 下面是基于hexo实践所产出的一些心得,供大家参考. 基础 Hexo 搭建 Hexo 与 ...

  3. scrapy xpath选择器多级选择错误

    在学习scrapy中用xpath提取网页内容时,有时要先提取出一整个行标签内容,再从行标签里寻找目标内容.出现一个错误. 错误代码: def parse(self, response): sel = ...

  4. 题解 UVA10328 【Coin Toss】

    这道题目其实就是说有N张纸牌,问至少连续K张正面朝上的可能性是多少. 可以用递推做.首先我们将题目所求从 至少K张 转化为 总数 - 至多K张 (为什么要这样自己想) 设F[i][j]为前i个纸牌至多 ...

  5. FastDFS学习总结(1)--FastDFS安装和部署

    FastDFS是一个开源的,高性能的的分布式文件系统,他主要的功能包括:文件存储,同步和访问,设计基于高可用和负载均衡,FastDFS非常适用于基于文件服务的站点,例如图片分享和视频分享网站 Fast ...

  6. CSU 1249 竞争性酶抑制剂和同工酶

    1249: 竞争性酶抑制剂和同工酶 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 109  Solved: 49 Description 人体内很多化学 ...

  7. Android编译环境配置

    Android编译环境配置 网上关于Android编译环境配置的整理资料有不少,经整理亲测后,希望能给需要的亲们提供帮助. 主要分为四步: 1.安装JDK(Java Standard Edition ...

  8. C#读写共享目录

    C#读写共享目录 该试验分下面步骤: 1.在server设置一个共享目录.在这里我的serverip地址是10.80.88.180,共享目录名字是test,test里面有两个文件:good.txt和b ...

  9. legend---七、jquery如何选中select的selected的选择上的自定义属性

    legend---七.jquery如何选中select的selected的选择上的自定义属性 一.总结 一句话总结:用冒号属性选择器 var type=$(this).children('option ...

  10. Python正则表达式的简单应用和示例演示

    前一阵子小编给大家连续分享了十篇关于Python正则表达式基础的文章,感兴趣的小伙伴可以点击链接进去查看.今天小编给大家分享的是Python正则表达式的简单应用和示例演示,将前面学习的Python正则 ...