[CF]Codeforces Round #531 (Div. 3)
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 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<map> 6 #include<set> 7 #include<algorithm> 8 #include<cmath> 9 #include<cstdlib> 10 using namespace std; 11 int n, k,b[5100]; 12 struct student 13 { 14 int shu, zhi,id; 15 }a[5100]; 16 int cmp1(student x, student y) 17 { 18 return (x.shu < y.shu); 19 } 20 int cmp2(student x, student y) 21 { 22 return (x.id < y.id); 23 } 24 int main() 25 { 26 scanf("%d %d",&n,&k); 27 for (int i = 1; i <= n; i++) 28 { 29 scanf("%d",&a[i].shu); 30 b[a[i].shu]++; 31 a[i].id = i; 32 } 33 sort(a + 1, a + n + 1, cmp1); 34 int maxx = 0; 35 for (int i = 1; i <= 5000; i++) 36 maxx = max(b[i],maxx); 37 if (maxx > k) 38 { 39 printf("NO\n"); 40 return 0; 41 } 42 int kk = 0; 43 for (int i = 1; i <= n; i++) 44 { 45 a[i].zhi = kk % k + 1; 46 kk++; 47 } 48 sort(a + 1, a + n + 1, cmp2); 49 printf("YES\n"); 50 for (int i = 1; i <= n; i++) 51 { 52 if (i == 1) printf("%d",a[i].zhi); 53 else printf(" %d",a[i].zhi); 54 } 55 printf("\n"); 56 return 0; 57 }
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 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<map> 6 #include<set> 7 #include<algorithm> 8 #include<cmath> 9 #include<cstdlib> 10 using namespace std; 11 int n,a[300100],a1=0,a2=0,a0=0; 12 char s; 13 int main() 14 { 15 scanf("%d",&n); 16 getchar(); 17 for (int i = 0; i <n; i++) 18 { 19 scanf("%c",&s); 20 a[i] = s-'0'; 21 if (a[i] == 1) 22 a1++; 23 else if (a[i] == 0) 24 a0++; 25 else a2++; 26 } 27 if (a0 <= n / 3) 28 { 29 if (a1 <= n / 3)//a0<=s,a1<=s,a2>s; 30 { 31 int k = n / 3 - a0; 32 for (int i = 0; i < n; i++) 33 { 34 if (k == 0) break; 35 if (a[i] == 2) 36 { 37 a[i] = 0; 38 k--; 39 } 40 } 41 k = n / 3 - a1; 42 for (int i = 0; i < n; i++) 43 { 44 if (k == 0) break; 45 if (a[i] == 2) 46 { 47 a[i] = 1; 48 k--; 49 } 50 } 51 } 52 else if (a2 <= n / 3)//a0<=s,a1>s,a2<=s; 53 { 54 int k = n / 3 - a0; 55 for (int i = 0; i < n; i++) 56 { 57 if (k == 0) break; 58 if (a[i] == 1) 59 { 60 a[i] = 0; 61 k--; 62 } 63 } 64 k = n / 3 - a2; 65 for (int i = n - 1; i >= 0; i--) 66 { 67 if (k == 0) break; 68 if (a[i] == 1) 69 { 70 a[i] = 2; 71 k--; 72 } 73 } 74 } 75 else//a0<=s,a1>s,a2>s; 76 { 77 int k = a1 - n / 3; 78 for (int i = 0; i < n; i++) 79 { 80 if (k == 0) break; 81 if (a[i] == 1) 82 { 83 a[i] = 0; 84 k--; 85 } 86 } 87 k = a2 - n / 3; 88 for (int i = 0; i < n; i++) 89 { 90 if (k == 0) break; 91 if (a[i] == 2) 92 { 93 a[i] = 0; 94 k--; 95 } 96 } 97 } 98 } 99 else 100 { 101 if (a1 > n / 3)//a0>s,a1>s,a2<=s; 102 { 103 int k = a0 - n / 3; 104 for (int i = n - 1; i >= 0; i--) 105 { 106 if (k == 0) break; 107 if (a[i] == 0) 108 { 109 a[i] = 2; 110 k--; 111 } 112 } 113 k= a1 - n / 3; 114 for (int i = n - 1; i >= 0; i--) 115 { 116 if (k == 0) break; 117 if (a[i] == 1) 118 { 119 a[i] = 2; 120 k--; 121 } 122 } 123 } 124 else if (a2 > n / 3)//a0>s,a1<=s,a2>s; 125 { 126 int k = a0 - n / 3; 127 for (int i = n - 1; i >= 0; i--) 128 { 129 if (k == 0) break; 130 if (a[i] == 0) 131 { 132 a[i] = 1; 133 k--; 134 } 135 } 136 k = a2 - n / 3; 137 for (int i = 0; i < n; i++) 138 { 139 if (k == 0) break; 140 if (a[i] == 2) 141 { 142 a[i] = 1; 143 k--; 144 } 145 } 146 } 147 else//a0>s,a1<=s,a2<=s; 148 { 149 int k = n / 3 - a2; 150 for (int i = n - 1; i >= 0; i--) 151 { 152 if (k == 0) break; 153 if (a[i] == 0) 154 { 155 a[i] = 2; 156 k--; 157 } 158 } 159 k = n / 3 - a1; 160 for (int i = n - 1; i >= 0; i--) 161 { 162 if (k == 0) break; 163 if (a[i] == 0) 164 { 165 a[i] = 1; 166 k--; 167 } 168 } 169 } 170 } 171 //cout << a0 << " " << a1 << " " << a2 << endl; 172 for (int i = 0; i < n; i++) 173 printf("%d",a[i]); 174 printf("\n"); 175 return 0; 176 }