Codeforces Round #531 (Div. 3)
B. Array K-Coloring
Description
You are given an array aa consisting of nn integer numbers.
You have to color this array in kk colors in such a way that:
- Each element of the array should be colored in some color;
- For each ii from 11 to kk there should be at least one element colored in the ii-th color in the array;
- For each ii from 11 to kk all elements colored in the ii-th color should be distinct.
Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c1,c2,…cnc1,c2,…cn, where 1≤ci≤k1≤ci≤k and cici is the color of the ii-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any.
Input
The first line of the input contains two integers nn and kk (1≤k≤n≤50001≤k≤n≤5000) — the length of the array aa and the number of colors, respectively.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤50001≤ai≤5000) — elements of the array aa.
output
If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers c1,c2,…cnc1,c2,…cn, where 1≤ci≤k1≤ci≤kand cici is the color of the ii-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any.
Examples
Input
4 2
1 2 2 3
Output
YES
1 1 2 2
Input
5 2
3 2 1 2 3
Output
YES
2 1 1 2 1
正确解法:
用k的颜色填充n个数字,k个颜色要用完,每种数字的颜色不能一样。
原本知道肯定是排序,但是我只能保证有这种情况,肯定小于等于k,不能保证k一定用完。
看了题解。排序,然后第k个数是第k个颜色。第k+1的数是第1种颜色。
保证有k个颜色。
不会vector
感觉我这种数据一大就会tle。死亡。
1 #include2 #include 3 #include<string> 4 #include 5 #include
D. Balanced Ternary String
Description
You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.
Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').
Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.
Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.
It is guaranteed that the answer exists.
Input
The first line of the input contains one integer nn (3≤n≤3⋅1053≤n≤3⋅105, nn is divisible by 33) — the number of characters in ss.
The second line contains the string ss consisting of exactly nn characters '0', '1' and '2'.
output
Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements.
Because nn is divisible by 33 it is obvious that the answer exists. And it is obvious that there is only one possible answer.
Examples
Input
3
121
Output
021
Input
6
000000
Output
001122
正确解法:
暴力求解。六种情况。
1 #include2 #include 3 #include<string> 4 #include 5 #include