Wonderful Coloring - 2

题目来源

题目大意:

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

思路

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

#include 
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.

你可能感兴趣的:(笔记)