Dividing coins (01背包)
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 (m≤100m≤100) 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
题目大意:
给若干个硬币,将其分成两份,让这两份的差值最小,求最小差值。
即求dp[总值/2]的最大值。
#include <iostream>
#include <cstring>
using namespace std;
int a[],dp[];
int main()
{
int T;
cin>>T;
while(T--)
{
memset(dp,,sizeof dp);
int m,sum=;
cin>>m;
for(int i=;i<=m;i++)
cin>>a[i],sum+=a[i];
for(int i=;i<=m;i++)
for(int j=sum/;j>=a[i];j--)
dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
cout<<(sum-*dp[sum/])<<'\n';
}
return ;
}
Dividing coins (01背包)的更多相关文章
- UVA 562 Dividing coins --01背包的变形
01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostre ...
- UVA 562 Dividing coins (01背包)
//平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include ...
- uva562 Dividing coins 01背包
link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 562 Dividing coins(dp + 01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- uva 562 Dividing coins(01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were f ...
- UVA 562 Dividing coins (01背包)
题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum, A,B其中必有一人获得的钱小于等于sum/2 ...
- UVA 562 Dividing coins 分硬币(01背包,简单变形)
题意:一袋硬币两人分,要么公平分,要么不公平,如果能公平分,输出0,否则输出分成两半的最小差距. 思路:将提供的整袋钱的总价取一半来进行01背包,如果能分出出来,就是最佳分法.否则背包容量为一半总价的 ...
- 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 ...
- Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)
A - Coins Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descript ...
随机推荐
- 题解报告:hdu 6441 Find Integer(费马大定理+智慧数)
Problem Description people in USSS love math very much, and there is a famous math problem .give you ...
- Android逆向分析工具表
逆向分析工具表 工具 描述 网址 androidterm Android Terminal Emulator http://code.google.com/p/androidterm/ droidbo ...
- 在Android 源码中添加系统服务
Android系统本身提供了很多系统服务,如WindowManagerService,PowerManagerService等.下面描述一下添加一个系统服务的具体步骤. 1.定义自定义系统服务接口 撰 ...
- AJPFX分享eclipse自动生成java注释方法
设置方法介绍:eclipse中:Windows->Preferences->Java->Code Style->Code Template->Comments,然后对应的 ...
- 洛谷2017 5月月赛R1
我只想说面对这种难度的题目就是冲着20%的数据暴力... 分数:40+20+36.1+38+0+19 T1 签到题 III 题目背景 pj组选手zzq近日学会了求最大公约数的辗转相除法. 题目描述 类 ...
- 【学习笔记】深入理解js原型和闭包系列学习笔记——精华
深入理解js原型和闭包笔记: 1.“一切皆是对象”,对象是属性的集合. 丨 函数也是对象,但是使用typeof时为什么函数返回function而 丨 不是object呢,js为何要对函数做这样的区分 ...
- 十个 JDBC 的最佳实践
JDBC是Java为多种关系型数据库提供的统一的访问接口,以下是我长期使用JDBC总结的十个最佳实践. 1. 使用PrearedStatement 任何一个使用过JDBC的Java程序员几乎都知道这个 ...
- 机器学习在SAP Cloud for Customer中的应用
关于机器学习这个话题,我相信我这个公众号1500多位关注者里,一定有很多朋友的水平比Jerry高得多.如果您看过我以前两篇文章,您就会发现,我对机器学习仅仅停留在会使用API的层面上. 使用Java程 ...
- 禁用DRM
10G: alter system set "_gc_policy_time"=0 scope=spfile sid='*'; alter system set "_gc ...
- $.noconflict() 有什么用处
jQuery默认使用"$"操作符,prototype等其他框架也是是使用"$",于是,如果jQuery在其他库之后引入,那么jQuery将获得"$&q ...