hdoj1171 Big Event in HDU(01背包 || 多重背包)

时间:2023-02-04 18:43:37

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1171

题意

老师有一个属性:价值(value)。在学院里的老师共有n种价值,每一种价值value对应着m个老师,说明这m个老师的价值都为value。现在要将这些老师从人数上平分成两个院系,并且希望平分后两个院系老师的总价值A和B应尽可能地相等,求A和B的值(A>=B)。

思路

由于每种老师的个数是有限的,所以使用多重背包解决。由于测试数据不是很严格,所以使用01背包也可以通过。

代码

01背包:

 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int N = ;
const int M = * ;
int v[N];
int dp[M]; int main()
{
//freopen("hdoj1171.txt", "r", stdin);
int n;
while (cin >> n && n >= )
{
int cur = ; //记录教师总数
int sum = ; //记录教师总价值
for (int i = ;i < n; i++)
{
int val, m;
cin >> val >> m;
for (int j = ; j < m; j++)
{
v[cur++] = val;
sum += val;
}
} memset(dp, , sizeof(dp));
for (int i = ; i < cur; i++)
{
for (int j = sum / ; j >= v[i]; j--)
dp[j] = max(dp[j], dp[j - v[i]] + v[i]); //weight和value相同
}
//由于dp[sum/2]<sum/2,所以dp[sum/2]一定是较小者,sum - dp[sum / 2]是较大者
cout << sum - dp[sum / ] << " " << dp[sum / ] << endl;
}
return ;

多重背包:

 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int N = ;
const int M = * ;
int v[N], num[N];
int dp[M];
int sum; void zero_one_pack(int weight, int value, int capacity)
{
for (int i = capacity; i >= weight; i--) //逆序
dp[i] = max(dp[i], dp[i - weight] + value);
} void complete_pack(int weight, int value, int capacity)
{
for (int i = weight; i <= capacity; i++) //正序
dp[i] = max(dp[i], dp[i - weight] + value);
} void mutiple_pack(int weight, int value, int amount, int capacity)
{
if (weight*amount >= capacity)
complete_pack(weight, value, capacity);
else
{
int k = ;
while (k <= amount)
{
zero_one_pack(weight*k, value*k, capacity);
amount -= k;
k *= ;
}
zero_one_pack(weight*amount, value*amount, capacity);
} } int main()
{
//freopen("hdoj1171.txt", "r", stdin);
int n;
while (cin >> n && n >= )
{
sum = ;
for (int i = ; i <= n; i++)
{
cin >> v[i] >> num[i];
sum += v[i] * num[i];
} memset(dp, , sizeof(dp));
for (int i = ; i <= n; i++)
mutiple_pack(v[i], v[i], num[i], sum / ); cout << sum - dp[sum / ] << " " << dp[sum / ] << endl;
}
}