(DP,背包,只有价值)Big Event in HDU -- HDOJ

来源:互联网 发布:qq远程控制软件 编辑:程序博客网 时间:2024/06/07 12:47

Big Event in HDU

Problem Description
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don’t know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0

#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>#include<queue>#include<math.h>using namespace std;int num[100005];int dp[100005];long long sum=0;int cnt = 0;int main(void){  //  freopen("in.txt","r",stdin);    int n;    while(scanf("%d",&n) && n>0)    {        cnt = 0;        sum = 0;        memset(dp,0,sizeof(dp));        for(int i=0; i<n; ++i)        {            int v,nm;            scanf("%d %d",&v,&nm);            sum += v*nm;            for(int j=0; j<nm; ++j)                num[cnt++] = v;        }        for(int i=0; i<cnt; ++i)        {            for(int j=sum/2; j>=num[i]; --j)            {                dp[j] = max(dp[j-num[i]] + num[i],dp[j]);            }        }        printf("%d ",sum-dp[sum/2]);        cout <<dp[sum/2]<<endl;    }    return 0;}
原创粉丝点击