Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28628 Accepted Submission(s): 10072
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
#pragma comment(linker, "/STACK:1024000000,1024000000") //黑科技
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
typedef long long LL;
const double eqs=1e-9;
//freopen("data.in", "r", stdin);
#define maxd 130000
int dp[maxd];//dp[i]表示能否分到价值为i的物品
int val[55], amount[55];
int wei[330];//二进制优化,转化为01背包
int main()
{
#ifndef ONLINE_JUDGE
//freopen("data.in", "r", stdin);
#endif
int n;
while(scanf("%d", &n) != EOF && n >= 0)
{
int max_value = 0;
for(int i = 0; i < n; i++)
{
scanf("%d %d", &val[i], &amount[i]);
max_value += val[i] * amount[i];
}
int len = 0;
for(int i = 0; i < n; i++)
{
int k = 1;
while(amount[i] > k)
{
wei[len++] = k * val[i];
amount[i] -= k;
k *= 2;
}
wei[len++] = amount[i] * val[i];
}//二进制优化
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for(int i = 0; i < len; i++)
for(int j = max_value / 2; j >= wei[i]; j--) dp[j] = max(dp[j], dp[j - wei[i]]);//
int res_1, res_2;
for(int i = max_value/2; i >= 0; i--)
if(dp[i])
{
res_2 = i;
break;
}
res_1 = max_value - res_2;
printf("%d %d\n", res_1, res_2);
}
return 0;
}
本文探讨了HDU计算机学院设施平均分配的问题,通过将问题转化为01背包问题,并采用二进制优化方法来解决。文章提供了一段C++代码实现,展示了如何有效地分配资源,确保两个学院都能获得尽可能接近的总价值。
2万+

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



