Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 47763 Accepted Submission(s): 16403
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
210 120 1310 1 20 230 1-1
Sample Output
20 1040 40
Author
第一种 01背包做法
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int dp[500000];//大数组一定要开main函数外面,否则可能会Runtime Error (ACCESS_VIOLATION)
int v[5005];
int main()
{
//freopen("in.txt","r",stdin);
int n,sum,cnt,price,num;
while(scanf("%d",&n)!=EOF&&n>=0)
{
memset(dp,0,sizeof(dp));
memset(v,0,sizeof(v));
sum=0;cnt=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&price,&num);
while(num--)
{
v[cnt++]=price;//转化成01背包
sum+=price;
}
}
int halfsum=sum/2;
for(int i=0;i<cnt;i++)
for(int j=halfsum;j>=v[i];j--)
dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
printf("%d %d\n",sum-dp[halfsum],dp[halfsum]);
}
return 0;
}
第二种 多重背包做法
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int dp[500000];
int main()
{
//freopen("in.txt","r",stdin);
int n,sum;
int v[5005],m[105];
while(scanf("%d",&n)!=EOF&&n>=0)
{
sum=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&v[i],&m[i]);
sum+=v[i]*m[i];
}
int num=sum/2;
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
for(int k=0;k<m[i];k++)
for(int j=num;j>=v[i];j--)
dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
printf("%d %d\n",sum-dp[num],dp[num]);
}
return 0;
}
本文介绍了一道关于计算机学院和软件学院设施分配的算法题目。该问题可通过两种背包问题的解决方法来求解:一种是01背包问题的实现方式,另一种是多重背包问题的实现方式。每种方法都详细提供了C++代码示例。

1135

被折叠的 条评论
为什么被折叠?



