HDU 1171 Big Event in HDU

本文探讨了HDU计算机学院设施平均分配的问题,通过将问题转化为01背包问题,并采用二进制优化方法来解决。文章提供了一段C++代码实现,展示了如何有效地分配资源,确保两个学院都能获得尽可能接近的总价值。

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;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值