n个任务,m台服务器,给出每个任务的耗时,分配工作到服务器中,使他们各台服务器间总处理时间的最大值与最小值的差最小.
#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
struct Edge{
int cost,id;
Edge(){}
Edge(int a,int b){
cost=a,id=b;
}
friend bool operator <(const Edge &a,const Edge &b){
return a.cost>b.cost;
}
}edge[100004];
int ans[100004];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d%d",&n,&m);
priority_queue<Edge>q;
for(int i=0;i<n;i++){
scanf("%d",&edge[i].cost);
edge[i].id=i;
}
sort(edge,edge+n);
for(int i=0;i<n;i++){
if(i<m){
ans[edge[i].id]=i;
q.push(Edge(edge[i].cost,i));
}else{
Edge temp=q.top();
q.pop();
ans[edge[i].id]=temp.id;
q.push(Edge(edge[i].cost+temp.cost,temp.id));
}
}
printf("%d\n",n);
for(int i=0;i<n;i++){
printf(i==n-1?"%d\n":"%d ",ans[i]);
}
}
return 0;
}