hdu 1171 Big Event

来源:互联网 发布:淘宝网10至20包邮 编辑:程序博客网 时间:2024/06/08 17:26
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<N<1000) kinds of facilities (different value, different kinds).
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
Output
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
Sample Input
210 120 1310 1 20 230 1-1
 
Sample Output
20 1040 40
----------------------------------------------------------------
#include<stdio.h>#include<string.h>struct node{ int v,m,sum;}a[250008];int c1[250008],c2[250008];int main(){ int n,i,j,k,tal,div; while(scanf("%d",&n)&&n>=0) {  memset(c1,0,sizeof(c1));  memset(c2,0,sizeof(c2));  tal=0;  for(i=1;i<=n;i++)  {   scanf("%d%d",&a[i].v,&a[i].m);   a[i].sum = a[i].m*a[i].v;   tal += a[i].sum;  }  c1[0]=1;  for(i=a[1].v;i<=a[1].sum;i+=a[1].v)   c1[i]=1;  for(i=2;i<=n;i++)  {   for(j=0;j<=tal;j++)   {    for(k=0;k+j<=tal&&k<=a[i].sum;k+=a[i].v)     c2[k+j] += c1[j];   }   for(j=0;j<=tal;j++)   {    c1[j] = c2[j];    c2[j] = 0;   }  }  div = tal / 2;  for(i=div;i>=0;i--)  {   if(c1[i]!=0)    break;  }  printf("%d %d\n",tal-i,i); } return 0;}
--------------------------------------------------------------
参考1:
#include<stdio.h>#include<string.h>#define max 250001          
int c1[max],c2[max];int main(){    int n,v[55],m[55];    int j,k,i,sum,div;    while(scanf("%d",&n)!=EOF && n>=0)    {        sum=0;        for(i=1;i<=n;i++)        {            scanf("%d%d",&v[i],&m[i]);            sum+=m[i]*v[i];        }        div=sum/2;        memset(c1,0,sizeof(c1)+1);        memset(c2,0,sizeof(c2)+1);        for(i=0;i<=m[1];i++)            c1[v[1]*i]=1;        for(i=2;i<=n;i++)//母函数循环控制两种办法,一种循环外层,一种循环内层        {            for(j=0;j<=sum;j++)                         {                for(k=0;k+j<=sum && k<=v[i]*m[i];k+=v[i])//k 是当前括号中的一项的指数 v[i]*m[i] 就是指数的最大值;                    c2[j+k]+=c1[j];            }            for(j=0;j<=sum;j++)            {                c1[j]=c2[j];                c2[j]=0;            }        }        if(c1[div])
            printf("%d %d\n",sum-div,div);        else        {            for(i=0;i<=sum;i++)                if(i>div && c1[i])                {                    printf("%d %d\n",i,sum-i);                    break;                }        }    }//注意边界数的控制,1,0之类的    return 0;}
-------------------------------------------------------------
参考2:
这种方法简单,但是我不知道为什么可以这样做
#include <iostream>#include <string.h>#include <algorithm>using namespace std;const int MAX = 110000;int a[MAX],len,n;int main(){    while(cin >> n && n >= 0){        memset(a,0,sizeof(a));        int num,value,sum = 0;        len = 0;        for(int i=0;i<n;i++){            cin >> value >> num;            sum += value * num;            while(num--)                a[len++] = value;        }        sort(a,a+len);        int half = sum / 2;        num = 0;        for(int i=len-1;i>=0;i--){            if(num + a[i] > half)                continue;            num += a[i];        }        cout << sum - num << " " << num << endl;    }    return 0;}
0 0
原创粉丝点击