/*
01背包 + 是否完全装满
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=456
题意:把一堆数分成两堆,尽可能的使两堆数各自的和相差最小,
求最小值
*/
# include <iostream>
# include <string.h>
# include <math.h>
# include <cmath>
# include <algorithm>
# define INFI 0x3f3f3f3f
using namespace std;
int main(void)
{
// freopen("in.txt","r",stdin);
int n,M,ans=-1;
int ncase;
cin >> ncase;
while(ncase--)
{
cin >> n;
int num[n+1],sum=0,tmp;
for(int i=0; i<n; ++i)
{
cin >> num[i];
sum += num[i];
}
tmp = sum;
sum /= 2;
int dp[sum+1];
for(int i=0; i<=sum;++i)
dp[i] = -INFI;
dp[0] = 0;
for(int i=0 ; i< n; ++i)
for(int j=sum; j>=num[i]; --j)//从大到小
{
dp[j] = max(dp[j],dp[j-num[i]]+num[i]);
}
for(int i=sum; i >=0; --i)
{
if(dp[i] > 0)
{
cout << abs(tmp-2*i)<<endl;
break;
}
}
}
return 0;
}
01背包 + 判断是否装满 Nyoj 邮票分你一半
最新推荐文章于 2024-10-21 11:22:02 发布