codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1551B2.md (3020B)


      1 Source: https://codeforces.com/problemset/problem/1551/B2
      2 
      3 B2. Wonderful Coloring - 2
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely.
     22 
     23 Recently, Paul and Mary have found a new favorite sequence of integers $a_1, a_2, \dots, a_n$. 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:
     24 
     25 1. each element of the sequence is either painted in one of $k$ colors or isn't painted;
     26 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);
     27 3. let's calculate for each of $k$ colors the number of elements painted in the color — all calculated numbers must be equal;
     28 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.
     29 
     30 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.
     31 
     32  ![](https://espresso.codeforces.com/7e77500cc77bb1e3650277f1f9d6f1d6bf9850d7.png) 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. 
     33 
     34 Help Paul and Mary to find a wonderful coloring of a given sequence $a$.
     35 
     36 Input
     37 
     38 The first line contains one integer $t$ ($1 \le t \le 10000$) — the number of test cases. Then $t$ test cases follow.
     39 
     40 Each test case consists of two lines. The first one contains two integers $n$ and $k$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le n$) — the length of a given sequence and the number of colors, respectively. The second one contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$).
     41 
     42 It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
     43 
     44 Output
     45 
     46 Output $t$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case.
     47 
     48 Each wonderful coloring must be printed as a sequence of $n$ integers $c_1, c_2, \dots, c_n$ ($0 \le c_i \le k$) separated by spaces where
     49 
     50 * $c_i=0$, if $i$-th element isn't painted;
     51 * $c_i \gt 0$, if $i$-th element is painted in the $c_i$-th color.
     52 
     53 Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one.
     54 
     55 Example
     56 
     57 Input
     58 
     59 ```
     60 6
     61 10 3
     62 3 1 1 1 1 10 3 10 10 2
     63 4 4
     64 1 1 1 1
     65 1 1
     66 1
     67 13 1
     68 3 1 4 1 5 9 2 6 5 3 5 8 9
     69 13 2
     70 3 1 4 1 5 9 2 6 5 3 5 8 9
     71 13 3
     72 3 1 4 1 5 9 2 6 5 3 5 8 9
     73 ```
     74 
     75 Output
     76 
     77 ```
     78 1 1 0 2 3 2 2 1 3 3
     79 4 2 1 3
     80 1
     81 0 0 1 1 0 1 1 1 0 1 1 1 0
     82 2 1 2 2 1 1 1 1 2 1 0 2 2
     83 1 1 3 2 1 3 3 1 2 2 3 2 0
     84 ```
     85 
     86 Note
     87 
     88 In the first test case, the answer is shown in the figure in the statement. The red color has number $1$, the blue color — $2$, the green — $3$.