您的位置:首页 > 编程语言 > C语言/C++

HDU 1171 Big Event in HDU (多重背包)

2016-08-12 16:38 253 查看

Big Event in HDU

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 62   Accepted Submission(s) : 26

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

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.

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

分成两份尽可能一样大的部分。那么就相当于将背包容量变成所有东西总价值的一半,求尽可能大的价值。

AC代码:
#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<string.h>
#include<queue>

using namespace std;

int v,dp[266666];

void ZOP(int cost,int wei)
{
for(int i=v;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+wei);
}
void CP(int cost,int wei)
{
for(int i=cost;i<=v;i++)
dp[i]=max(dp[i],dp[i-cost]+wei);
}
void MP(int cost,int wei,int cnt)
{
if(cost*cnt>=v)
{
CP(cost,wei);
return;
}
else
{
int k=1;
while(k<cnt)
{
ZOP(k*cost,k*wei);
cnt-=k;
k<<=1;
}
ZOP(cnt*cost,cnt*wei);
}
}

int main()
{
int t;
while(cin>>t)
{
if(t<0) break;
memset(dp,0,sizeof(dp));
int sum=0;
int a[1001],b[1001];
for(int i=1;i<=t;i++)
{
cin>>a[i]>>b[i];
sum+=a[i]*b[i];
}
v=sum;
for(int i=1;i<=t;i++)
{
if(b[i]==1) ZOP(a[i],a[i]);
else
MP(a[i],a[i],b[i]);
}
v/=2;
while(dp[v]!=v)
v--;
cout<<sum-dp[v]<<" "<<dp[v]<<endl;
}
return 0;
}




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM C++ 算法 多重背包