hdu 2850 Load Balancing

本文详细介绍了HDOJ2850问题的解决方案,涉及按照占用时间逆序排序任务,然后采用贪心算法进行服务器分配,以最小化最大与最小使用时间之差。
//hdu 2850 Load Balancing
//http://acm.hdu.edu.cn/showproblem.php?pid=2850
/*
 * 题意:
 * 有n个job和m个服务器,每个job有占用时间,给每个job分配服务器,得到各个服务器的使用时间TMi,Minimize (max {TMi} - min {TMj})
 * 思路:
 * 先按照占用时间逆序排序n个job,然后按照贪心分配服务器,每个job分配给当前使用时间最小的服务器
 * author:licatweijie
 */
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

struct nt{
    int server;
    int cost;
    bool operator<(const nt&rhs)const{
        return cost>rhs.cost;
    }
};

priority_queue<nt> pq;
int maxi,n,m;
nt tmp;
nt nodes[100010];
int ctmp;
int ct[100010];
int t;
int ans[100010];


int main()
{
//    freopen("/home/licatweijie/in","r",stdin);
    scanf("%d",&t);
    for (int cc=0;cc<t;cc++){
        while(!pq.empty())
            pq.pop();
        scanf("%d %d",&n,&m);
        for (int i=0;i<m;i++){
            tmp.server = i;
            tmp.cost = 0;
            pq.push(tmp);
        }

        printf("%d\n",n);
        for (int i=0;i<n;i++){
            scanf("%d",&nodes[i].cost);
            nodes[i].server = i;
        }
        sort(nodes,nodes+n);
        for (int i=0;i<n;i++){
            tmp = pq.top();
            pq.pop();
            ans[nodes[i].server] = tmp.server;
            tmp.cost+=nodes[i].cost;
            pq.push(tmp);
        }

        bool first = true;
        for (int i=0;i<n;i++){
            if (first) first = false;
            else printf(" ");
            printf("%d",ans[i]);
        }

        printf("\n");
    }
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值