链接:https://codeforces.ml/problemset/problem/1551
题意:自己看,概括不动;
题解:
统计每个数字出现的次数,大于等于K的,涂上1~k,剩下填零;小于K的所有数依次填上1~k;保证填的最后一个数是K;具体做法就是先按值排序后处理,然后根据下标输出;
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
class point
{
public:
int a, b, c;
};
point num[200005];
int ct[200005];
int ct1[200005] = {0};
bool bmp(point x, point y)
{
return x.a > y.a;
}
bool bmp1(point x, point y)
{
return x.b < y.b;
}
int main()
{
int t;
cin >> t;
while (t--)
{
memset(ct, 0, sizeof(ct));
memset(ct1, 0, sizeof(ct1));
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++)
{
cin >> num[i].a;
ct[num[i].a]++;
num[i].b = i;
}
int sum = 0;
for (int i = 0; i < n; i++)
{
if(ct[num[i].a]<k)sum++;
}
sort(num, num + n, bmp);
int z = sum / k * k;
int cnt = 1;
int cnt1 = 1;
for (int i = 0; i < n; i++)
{
if (ct[num[i].a] >= k)
{
num[i].c = ++ct1[num[i].a];
if (num[i].c > k)num[i].c = 0;
}
else
{
if (cnt <= z)
{
cnt++;
num[i].c = cnt1;
cnt1++;
if (cnt1 > k)cnt1 = 1;
}
}
}
sort(num, num + n, bmp1);
for (int i = 0; i < n; i++)
{
cout << num[i].c << " ";
}
cout << endl;
for (int i = 0; i < n; i++)
{
num[i].a = 0;
num[i].b = 0;
num[i].c = 0;
}
}
}