Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 47907 Accepted Submission(s): 16464
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
We have carefully selected several similar problems for you: 2159 2844 1087 1176 1114
01背包解决
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int v[5005];
int dp[255555];
int main()
{
int n,i,j,l,a,b,sum;
while(~scanf("%d",&n)&&n>0)
{
memset(dp,0,sizeof(dp));
memset(v,0,sizeof(v));
l=0;
sum=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
while(b--){
v[l++]=a;
sum+=a;
}
}
for(i=0;i<l;i++)
for(j=sum/2;j>=v[i];j--)
dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
}
return 0;
}
多重背包
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int v[55];
int dp[250015];
int num[55];
int main()
{
int n,i,j,k,l,a,b,sum;
while(~scanf("%d",&n)&&n>0)
{
memset(dp,0,sizeof(dp));
sum=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&v[i],&num[i]);
sum+=num[i]*v[i];
}
for(i=0;i<n;i++)
for(k=0;k<num[i];k++)
for(j=sum/2;j>=v[i];j--)
dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
}
return 0;
}
母函数模板正在研究附上
#include<cstdio>
typedef long long LL;
const int N = 100 + 5;//假如题目只问到100为止
const int MAX = 3;//题目只有1,2,3这3种邮票
LL c1[N], c2[N];//c2是临时合并的多项式,c1是最终合并的多项式
int n;
void init(){
c1[0] = 1;//一开始0的情况算一种
for(int i = 1; i <= MAX; i ++){//把1分到MAXN的邮票合并,变成一个多项式
for(int j = 0; j < N; j += i){//i分的邮票,步长是i
for(int k = 0; j + k < N; k ++){//从x^0到x^N遍历一遍
c2[j + k] += c1[k];//因为j的所有项系数为1,所以c1[k]可以看成c1[k]*1;
}
}
for(int j = 0; j < N; j ++){//把c2的数据抄到c1,清空c2
c1[j] = c2[j];
c2[j] = 0;
}
}
}
int main(){
init();
while(scanf("%d", &n) != EOF){
printf("%I64d\n", c1[n]);
}
}
大佬代码
#include<stdio.h>
int c1[250001],c2[250001]; //这个地方开始把题目看错了,导致数组开小了
int a[55][2];
int main()
{
int i,j,k,s,n;
while(scanf("%d",&n)&&n>0)
{
s=0; //开始忘了初始化,输入数据后,一直不能输出
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i][0],&a[i][1]);
s+=a[i][0]*a[i][1];
}
for(i=0;i<=s;i++)
{
c1[i]=0;c2[i]=0;
}
for(i=0;i<=a[0][1];i++)
c1[i*a[0][0]]=1;
for(i=1;i<n;i++)
{
for(j=0;j<=s;j++)
for(k=0;k<=a[i][0]*a[i][1];k+=a[i][0])
c2[j+k]+=c1[j];
for(j=0;j<=s;j++)
{
c1[j]=c2[j];
c2[j]=0;
}
}
if(s%2==0)i=s/2;
else i=s/2+1;
for(j=i;;j++)
if(c1[j]!=0)break;
printf("%d %d\n",j,s-j);
}
return 0;
}