hdu2850 Load Balancing贪心+优先队列

本文探讨了网站中使用Apache、nginx和lighttpd等Web服务器的流行趋势,特别关注了负载均衡技术。通过介绍负载均衡的基本原理,即在多台服务器间分配任务以优化资源利用、提高吞吐量和减少响应时间,文章深入分析了如何建立简单的负载均衡模型。面对NP-hard级别的调度问题,提出了一种贪心算法来分配任务,以实现服务器负载的平衡。

In the Wide Web World, Which web server was popular in web site? Apache, nginx, lighttpd? Baidu.com use Apache, and many web sites like 163.com use nginx. Why? Its configuration is very simple, and it has very powerful load balancing features. 

How does load balancing work? Load balancing is a technique to spread work between two or more computers in order to get optimal resource utilization, maximize throughput, and minimize response time. Wiskey very curious about this technology and he wants to build a simple load balancing model. 

He define the web site has M servers, and N jobs need be done. Each job has a processing time T; we seek to assign each job to one of the servers so that the loads placed on all servers are as "balanced" as possible. The "balanced" mean the Minimize it (max {TMi} - min {TMj}). TMi is the server i total time cost. 

But, as Wiskey Know, this scheduling problem of finding an assignment of minimum gap is NP-hard. 

Can you write a better load balancing than Wiskey? Challenge it~! 

Input

The first integer C represents the number of cases, And C cases followed. 

Each test case contains a single integer N (1<=N<=100000) and M (1<=M<=100). The next N line contains integers, meaning the time of job T1, T2Tn. (1<=Ti<=1000000) 

Output

For each test case, first output the N, and follows N numbers mean the job i assign to which server. The servers number count from 0.

Sample Input

2
6 3
2 3 4 6 2 2
6 3
6 4 3 2 2 2

Sample Output

6
0 1 2 0 1 2
6
0 1 1 2 2 2

        
  

Hint

Hint
This problem is special judge, if your assign plan (max {TMi} -min {TMj}) <= my answer + 1000, it be consider Accept.

如何将每一个任务分配到服务器上,使得最大的负载减去最小的负载最小

将每个任务从大到小排序然后 分配到最小的服务器上

这样的贪心好像是不太对。。。不过因为是spj 所以也判对了

很迷啊。。。

#include <iostream>
#include <stdio.h>
#include <queue>
#include <algorithm>
#define maxn 100000+5
using namespace std;
struct node {
    int j,f;
    int fs;
};
bool cmp1(node &x,node&y){
    return x.f<y.f;
}
bool cmp2(node &x,node &y){
    return x.j>y.j;
}
struct se{
    int fl,lo;
    bool operator >(const se& d)const{
        return lo>d.lo;
    }
};
int n,m;
node a[maxn];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++){
            a[i].f=i;
            scanf("%d",&a[i].j);
        }
        priority_queue<se,vector<se>,greater<se> >q;
        for(int i=0;i<m;i++){
            se b;b.fl=i;b.lo=0;
            q.push(b);
        }
        sort(a,a+n,cmp2);
        for(int i=0;i<n;i++){
            se b=q.top();q.pop();
            b.lo+=a[i].j;
            a[i].fs=b.fl;
            q.push(b);
        }
        sort(a,a+n,cmp1);
        printf("%d\n",n);
        for(int i=0;i<n;i++){
            printf("%d",a[i].fs);
            if(i!=n-1)printf(" ");
            else printf("\n");
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值