题目链接:
https://vjudge.net/problem/UVA-562
题目大意:
给定n个硬币,要求将这些硬币平分以使两个人获得的钱尽量多,求两个人分到的钱最小差值
思路:
它所给出的n个钱币加起来sum,将sum/2当作体积,求出在sum/2下的最大值,sum-2*dp[sum/2]
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
const int maxm = 1e5+;
int a[maxn];
int dp[maxm];
int T, n, m;
int main()
{
cin >> T;
while(T--)
{
cin >> n;
int sum = ;
memset(dp, , sizeof(dp));
for(int i = ; i < n; i++)cin >> a[i], sum += a[i];
for(int i = ; i < n; i++)
{
for(int j = sum / ; j >= a[i]; j--)
dp[j] = max(dp[j], dp[j - a[i]] + a[i]);
}
cout<<(sum - * dp[sum / ])<<endl;
}
return ;
}