Big Event in HDU-dynamic planning 01 backpack

Now, we all know that the School of Computer is the largest department of HDU. However, maybe you do n’t know that the School of Computer Science was divided into the School of Computer and the School of Software in 2002.
Splitting is definitely a major event for HDU! At the same time, this is also a hassle. All facilities must be halved. First, evaluate all facilities. If two facilities have the same value, they are considered to be the same. Suppose there are N (0 <N <1000) facilities (different values, different types).
Input: The
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—the value of the facility) and an integer M (0 <M <= 100—the corresponding number of facilities). You can assume that all Vs are different. Test cases beginning with a negative integer terminate the input and do not process this test case.
Output:
For each case, print a line containing two integers A and B, respectively representing the values ​​of the School of Computer and the School of Software. A and B should be as equal as possible. At the same time, you should ensure that A is not less than B.
Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1
Sample Output
20 10
40 40

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int V,N,weight[10002],v[10002];
long int f[100002];
int OnePack()  //0-1背包的主要思想
{
    memset(f,0,sizeof(f));
    for(int i=0;i<N;i++)
    {
        for(int j=V;j>=weight[i];j--)
            f[j]=max(f[j],f[j-weight[i]]+v[i]);
    }
    return f[V];
}

int main()
{
    int t,a,b,x,y,z;
    while(scanf("%d",&t)&&t>0)
    {
        N=V=0;  //每次背包的N和V都要更新
        int i=0;
        while(t--)
        {
            scanf("%d%d",&a,&b);
            N+=b;
            V+=a*b;
            while(b--)
            {
                v[i]=a;  //记录每件物品的价值
                weight[i]=a;  //记录每件物品的体积,这里的价值和所占体积是同一个值
                i++;
            }
        }
        z=V;
        V/=2;
        x=max(OnePack(),z-OnePack());  //最大的设施价值放前面
        y=min(OnePack(),z-OnePack());
        cout<<x<<" "<<y<<endl;
    }
    return 0;
}

Published 97 original articles · praised 5 · visits 3946

Guess you like

Origin blog.csdn.net/weixin_44641254/article/details/105452964