Wonderful Coloring - 2

这是一个关于染色问题的算法题目,要求在给定的数字序列中使用k种颜色进行染色,使得相同的数字染不同的颜色,且每种颜色出现的次数相同。输入包含n个数字和k种颜色,输出一种满足条件的染色方案。算法思路是记录每个数字的前k次出现位置,然后遍历并依次染色,确保每个颜色的使用次数相等。AC代码使用了unordered_map存储数字及其位置,最终输出满足条件的染色序列。
摘要由CSDN通过智能技术生成

题目来源

题目大意:

输入n个数字和k个颜色
使用这k个颜色给n个数字染色,也可以不被染色,
但是得满足以下条件:
相同的数字必须染不同的颜色。每一种颜色出现的次数必须相同。
必须最大限度的染色。
求任意一种满足该条件的情况,并输出该情况。

思路

1,输入n个数字,并通过每一个map来记录每一个数字的前k次出现的位置、
即被记录的位置是需要染色的。
为了保证最后每个颜色的次数相同,所以只需要染色 time-(time%3)次
遍历我们的map,每次染色一个位置就把那个位置给去掉,并以此染不同的颜色,确保每个颜色的出现次数相同、
ac代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,k,temp,le;
    cin>>t;
    unordered_map<int , vector<int> >s;
    while(t--){
        s.clear();
        cin>>n>>k;
        le =0;
        vector<int>ans(n+1);
        for(int i =0;i<n;i++){
            cin>>temp;
            if(s[temp].size()<k){
                s[temp].push_back(i);
                le++;
            }
        }
        for(int i = 0;i <le/k ; i++){
            int p =1;
            while(p<=k){
                auto it = s.begin();
                if(it==s.end()) continue;
                if(it->second.empty()){
                    s.erase(it);
                    continue;
                }
                else{
                    if(it->second.empty())continue;
                    ans[it->second.back()] = p++;
                    it->second.pop_back();
                }
            }
        }
        for (int i = 0; i < n; ++i) {
            cout<<ans[i]<<' ';
        }cout<<endl;
    }
}

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,…,an. They want to paint it using pieces of chalk of k colors. The coloring of a sequence is called wonderful if the following conditions are met:

each element of the sequence is either painted in one of k colors or isn’t painted;
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);
let’s calculate for each of k colors the number of elements painted in the color — all calculated numbers must be equal;
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] and k=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] and k=3. Note that one of the elements isn’t painted.
Help Paul and Mary to find a wonderful coloring of a given sequence a.

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

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

It is guaranteed that the sum of n over all test cases doesn’t exceed 2⋅105.

Output
Output t 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 n integers c1,c2,…,cn (0≤ci≤k) separated by spaces where

ci=0, if i-th element isn’t painted;
ci>0, if i-th element is painted in the ci-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.

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

夭辰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值