1. 程式人生 > >Big Event in HDU HDU - 1171

Big Event in HDU HDU - 1171

.cn ++ pid for scan nbsp esp pac 01背包

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1171

題意:給出每個物體的價值和物體的數量,如何分使得A,B所得價值最接近並且A的價值不能小於B

思路:將總和平分後,就是一道01背包題了

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 1e6;
int val[maxn];
int dp[maxn];
int main()
{
    int n,i,j,a,b,l,sum;
    
while(~scanf("%d",&n)) { if(n<=0)break; memset(val,0,sizeof(val)); memset(dp,0,sizeof(dp)); l = 0; sum = 0; for(i = 0;i<n;i++) { scanf("%d%d",&a,&b); while(b--) { val[l
++] = a; sum+=a; } } for(i = 0;i<l;i++) { for(j = sum/2;j>=val[i];j--)//01背包 { dp[j] = max(dp[j],dp[j-val[i]]+val[i]); } } printf("%d %d\n",sum-dp[sum/2],dp[sum/2]); }
return 0; }

Big Event in HDU HDU - 1171