2014
02-17

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~!

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)

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)

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

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.



#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;

struct Edge
{
int cost,id;
Edge () {}
Edge (int a,int b)
{
cost = a,id = b;
}
void Get (int _i)
{
id=_i;
scanf("%d",&cost);
}
bool operator < (const Edge &y) const
{
return cost>y.cost;
}
}data[100005];

int ans[100005],n,m;

int main ()
{
int T,i;
scanf("%d",&T);
while (T--)
{
priority_queue<Edge> que;
scanf("%d%d",&n,&m);
for (i=0;i<n;i++)
data[i].Get (i);
sort (data,data+n);
Edge temp;    //temp的id值表示它是由哪个机器处理的
for (i=0;i<n;i++)
if (i<m)     //都还没有任务的时候
{
que.push(Edge(data[i].cost,i));   //将时间和处理标号一起入队
ans[data[i].id]=i;
}
else
{
temp=que.top ();
ans[data[i].id]=temp.id;
que.pop();
que.push(Edge(temp.cost+data[i].cost,temp.id));//将目前用掉的总时间和处理标号入队
}
printf("%d\n",n);
for (i=0;i<n;i++)
printf(i==n-1?"%d\n":"%d ",ans[i]);
}
return 0;
}