Dividing coins - UVa 562 dp背包
来源:互联网 发布:哈鲁留学怎么样 知乎 编辑:程序博客网 时间:2024/06/07 11:04
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
232 3 541 2 4 6
Sample Output
01
题意:把一堆硬币分成两堆,问两堆的最小价值差是多少。
思路:背包,硬币的值既是价值,又是体积,dp[sum/2]就是少的那堆的值。
AC代码如下:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int num[110],dp[25010];int main(){ int t,n,sum,sum2,i,j,k; scanf("%d",&t); while(t--) { scanf("%d",&n); sum=0; for(i=1;i<=n;i++) { scanf("%d",&num[i]); sum+=num[i]; } sum2=sum/2; memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) for(j=sum2;j>=num[i];j--) dp[j]=max(dp[j],dp[j-num[i]]+num[i]); printf("%d\n",sum-2*dp[sum2]); }}
- UVA 562 Dividing coins(dp + 01背包)
- UVA 562 Dividing coins(DP:01背包)
- Dividing coins - UVa 562 dp背包
- UVA.562 Dividing coins (DP 01背包)
- UVa 562 - Dividing coins 背包
- uva--562Dividing coins +dp
- uva 562 Dividing coins (0/1背包)
- uva 562 - Dividing coins(01背包)
- UVA 562 Dividing coins(01背包)
- UVA 562 Dividing coins 01背包 Commencel
- UVA 562 - Dividing coins(01背包)
- UVA - 562 Dividing coins 经典01背包
- UVA - 562 Dividing coins (01背包)
- UVA 562 Dividing coins 01背包
- uva 562 Dividing coins (01背包)
- UVA - 562 - Dividing coins (01背包)
- UVA 562Dividing coins 01背包
- UVA 562Dividing coins(01 背包问题)
- 【安卓笔记】扁平化的ProgressBar-----ProgressWheel
- 二叉搜索树的性质与操作
- POJ3264
- Visible Lattice Points poj+欧拉函数的应用+水水的过了
- 中国剩余定理
- Dividing coins - UVa 562 dp背包
- poj2528Mayor's posters(线段树)
- 对libevent+多线程服务器模型的C++封装类
- uva 11167 网络流 【好题】
- 从某道C面试题开始……
- PorterDuff.Mode
- 判断输入的任意整数m是否为素数。
- Graph Automata Player
- java 内省机制