Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 47159 Accepted Submission(s): 16187
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).
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.
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
2 10 1 20 1 3 10 1 20 2 30 1 -1
Sample Output
20 10 40 40
Author
lcy
Recommend
题意:n种物品,每种物品有m件,每种物品有一个价值v,问能凑出的尽可能相同的两个价值是多少。
感觉像是多重背包的题目,为了巩固母函数知识,尝试用母函数解决了这个问题(刷空HDU母函数),变量sum求出所有物品的总价值,题目即为求解在sum/2的范围内我能凑出的最大价值,母函数筛选一下方案数,则在sum/2以下最大的数即为答案的一个。
#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define ll long long
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
using namespace std;
int c1[250000],c2[250000],v[1005],m[1005];
int main()
{
int n;
while(cin>>n,n>0)
{
int sum=0;
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
memset(v,0,sizeof(v));
memset(m,0,sizeof(m));
for(int i=1;i<=n;i++)
{
cin>>v[i]>>m[i];
sum+=v[i]*m[i];
}
c1[0]=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=sum;j++)
for(int k=0;k<=v[i]*m[i] && k+j<=sum;k+=v[i])
c2[k+j]+=c1[j];
memcpy(c1,c2,sizeof(c2));
memset(c2,0,sizeof(c2));
}
for(int i=sum/2;i>=0;i--)
if(c1[i])
{
cout<<max(i,sum-i)<<" "<<min(i,sum-i)<<endl;
break;
}
}
return 0;
}