B2. Wonderful Coloring - 2- Codeforces Round #734 (Div. 3)

原题链接Problem - B2 - Codeforces

B2. Wonderful Coloring - 2

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely.

Recently, Paul and Mary have found a new favorite sequence of integers a1,a2,…,ana1,a2,…,an. They want to paint it using pieces of chalk of kk colors. The coloring of a sequence is called wonderful if the following conditions are met:

  1. each element of the sequence is either painted in one of kk colors or isn't painted;
  2. each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color);
  3. let's calculate for each of kk colors the number of elements painted in the color — all calculated numbers must be equal;
  4. the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions.

E. g. consider a sequence a=[3,1,1,1,1,10,3,10,10,2]a=[3,1,1,1,1,10,3,10,10,2] and k=3k=3. One of the wonderful colorings of the sequence is shown in the figure.

The example of a wonderful coloring of the sequence a=[3,1,1,1,1,10,3,10,10,2]a=[3,1,1,1,1,10,3,10,10,2] and k=3k=3. Note that one of the elements isn't painted.

Help Paul and Mary to find a wonderful coloring of a given sequence aa.

Input

The first line contains one integer tt (1≤t≤100001≤t≤10000) — the number of test cases. Then tt test cases follow.

Each test case consists of two lines. The first one contains two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤n1≤k≤n) — the length of a given sequence and the number of colors, respectively. The second one contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n).

It is guaranteed that the sum of nn over all test cases doesn't exceed 2⋅1052⋅105.

Output

Output tt lines, each of them must contain a description of a wonderful coloring for the corresponding test case.

Each wonderful coloring must be printed as a sequence of nn integers c1,c2,…,cnc1,c2,…,cn (0≤ci≤k0≤ci≤k) separated by spaces where

  • ci=0ci=0, if ii-th element isn't painted;
  • ci>0ci>0, if ii-th element is painted in the cici-th color.

Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one.

Example

input

Copy

6
10 3
3 1 1 1 1 10 3 10 10 2
4 4
1 1 1 1
1 1
1
13 1
3 1 4 1 5 9 2 6 5 3 5 8 9
13 2
3 1 4 1 5 9 2 6 5 3 5 8 9
13 3
3 1 4 1 5 9 2 6 5 3 5 8 9

output

Copy

1 1 0 2 3 2 2 1 3 3
4 2 1 3
1
0 0 1 1 0 1 1 1 0 1 1 1 0
2 1 2 2 1 1 1 1 2 1 0 2 2
1 1 3 2 1 3 3 1 2 2 3 2 0

Note

In the first test case, the answer is shown in the figure in the statement. The red color has number 11, the blue color — 22, the green — 33.

--------------------------------------------------------------------------------------------------------------------------------

本来我敲了一个模拟,比较麻烦,也就是排个序,染上之前比对该颜色之前的数字值,不相等再染色,到达k个之后再退回去染色,细节较多。

#include<iostream>
#include<set>
# include<algorithm>
# include<cstring>

using namespace std;

typedef struct
{
   int id;
   int se;
   int a;
   int cnt;

}dian;
dian s[200000+10];

bool cmp(dian a,dian b)
{
    return a.a<b.a;
}

bool cmp1(dian a,dian b)
{
    return a.id<b.id;
}
int pre[200000+10];

int main()
{


int t;
cin>>t;
while(t--)
{
    int n;
    cin>>n;
    int k;
    cin>>k;

    for(int i=1;i<=n;i++)
    {
        cin>>s[i].a;
        s[i].se=0;
        s[i].cnt=0;
        s[i].id=i;
    }

    sort(s+1,s+1+n,cmp);

    memset(pre,0,sizeof(pre));

    int cnt=1;
    int temp=0;
    int now=1;
    for(int i=1;i<=n;i++)
    {
        if(s[i].a==pre[now])
            continue;

        pre[now]=s[i].a;


        s[i].cnt=cnt;

        now++;
        if(now==k+1)

        {
            now--;
           for(int j=i;j>=1;j--)
           {
               if(s[j].cnt==cnt)
               {
                   s[j].se=now;
                    now--;
               }
              
               if(!now)
                break;
           }

           now=1;
           cnt++;


        }


    }
    sort(s+1,s+1+n,cmp1);

    for(int i=1;i<=n;i++)
    {
        cout<<s[i].se<<" ";
    }

    cout<<'\n';


}

    return 0;
}

后来想到,我们只需要把出现k次以内的数字塞进数组,然后按照一组k个进行染色就行了

转载一位网友的代码

int main() {
	int m,n,k;
	cin>>m;
	while(m--){
		int cnt[200005],ans[200005],inp;//cnt统计26个字母的个数,ans存储染色结果
		vector<pair <int,int> > v;
		cin>>n>>k;
		for(int i=0;i<=n;i++){
			ans[i]=0;cnt[i]=0;
		}
		for(int i=0;i<n;i++){
			cin>>inp;		
			//cout<<"cnt[inp]: "<<cnt[inp]<<" ";
			if(cnt[inp]<k){
				v.push_back({inp,i});
			}
            		cnt[inp]++;		
		}
		sort(v.begin(),v.end());//排序,目的是为了避免同个数字被染同样色
		int groups=v.size()/k;//把能染色的个数分成k组,设一次染色过程为把k种颜色各自用一遍,groups就是能有几次染色过程
		for(int i=0;i<groups*k;i++){//gruops*k即为保证用各种颜色次数相等时的最大染色数量
			ans[v[i].second]=i%k+1;//染色
		}
		for(int i=0;i<n;i++) cout<<ans[i]<<" ";
		cout<<endl;
	}
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值