您的位置:首页 > 其它

HDU 1171 —— Big Event in HDU 01背包入门

2015-12-14 17:09 183 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1171

题意:有n种数,下面n行给出每种数的大小以及个数,将所有的数字分成两堆,且两堆的差值尽可能的小;

注意点:1、容量是sum/2;2、n<0 break,而不是-1;3、数组大小不要开错;

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 250000+5;

int dp[maxn];
int a[maxn];
int n;

int main()
{
while(~scanf("%d", &n))
{
if(n < 0)	break;
memset(dp, 0, sizeof dp);
int cnt = 0;
int sum = 0;
for(int i = 1;i<=n;i++)
{
int x, y;
scanf("%d%d", &x, &y);
sum += (x*y);
while(y--)
{
a[++cnt] = x;
}
}
for(int i = 1;i<=cnt;i++)
{
for(int j = sum/2;j>=a[i];j--)
{
dp[j] = max(dp[j], dp[j-a[i]]+a[i]);
}
}
printf("%d %d\n", max(sum-dp[sum/2], dp[sum/2]), min(sum-dp[sum/2], dp[sum/2]));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: