152人阅读 评论(0)

# Problem S

##### Total Submission(s) : 116   Accepted Submission(s) : 46
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.<br>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&lt;N&lt;1000) kinds of facilities (different value, different kinds).<br>

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.<br>A test case starting with a negative integer terminates input and this test case is not to be processed.<br>

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.<br>

Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1


Sample Output
20 10
40 40


如今,我们都知道,计算机学院二是最大的部门。但是,也许你不知道曾经分成计算机学院计算机学院和软件学院在2002年。< br >二分裂绝对是一个大事件!与此同时,这是一件麻烦的事。所有设备必须平分。首先,所有设施评估,和两个设施被认为是相同的,如果他们有相同的值。假设有N(0 & lt;N<1000)类型的设施。

这个是以前做的，思路有点遗忘，总的来说用一个数储存下来总价值，用一个累加数组记录n的值，再加上状态转移方程“dp[j]=max(dp[j],dp[j-a[i]]+a[i]);”就ok了。。

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int t,n,m,i,j,k,sum;
int dp[100001];
int a[100001];
while(cin>>t&&t>=0)
{
memset(dp,0,sizeof(dp));
sum=0;
k=0;
for(i=0;i<t;i++)
{
cin>>n>>m;
sum+=n*m;
for(j=0;j<m;j++)
{
a[k++]=n;
}
}
for(i=0;i<k;i++)
{
for(j=sum/2;j>=a[i];j--)
{
dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
}
}
cout<<sum-dp[sum/2]<<" "<<dp[sum/2]<<endl;
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：38746次
• 积分：1282
• 等级：
• 排名：千里之外
• 原创：88篇
• 转载：9篇
• 译文：0篇
• 评论：25条
阅读排行
最新评论