优先队列,把任务耗时大的先放。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
#define maxn 110000
struct node
{
int num,val,belong;
}save[maxn];
int n,m;
bool cmp(node aa,node bb)
{
return aa.val>bb.val;
}
bool cmp2(node aa,node bb)
{
return aa.num<bb.num;
}
struct cpu
{
int num;
long long sum;
friend bool operator <(cpu aa,cpu bb)
{
return aa.sum>bb.sum;
}
}b[200];
int main()
{
int cases;
scanf("%d",&cases);
while(cases--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&save[i].val);
save[i].num=i;
}
sort(save+1,save+1+n,cmp);
priority_queue<cpu>q;
for(int i=1;i<=m;i++)
{
b[i].sum=0;
b[i].num=i;
q.push(b[i]);
}
cpu cur;
for(int i=1;i<=n;i++)
{
cur=q.top();
q.pop();
save[i].belong=cur.num;
cur.sum+=save[i].val;
q.push(cur);
}
sort(save+1,save+1+n,cmp2);
printf("%d\n",n);
printf("%d",save[1].belong-1);
for(int i=2;i<=n;i++)
{
printf(" %d",save[i].belong-1);
}
printf("\n");
}
return 0;
}