一、题目描述
D. Carousel
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The round carousel consists of n
figures of animals. Figures are numbered from 1 to n in order of the carousel moving. Thus, after the n-th figure the figure with the number 1 follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal of the i-th figure equals ti
.
The example of the carousel for n=9
and t=[5,5,1,15,1,5,5,1,1]
.
You want to color each figure in one of the colors. You think that it’s boring if the carousel contains two different figures (with the distinct types of animals) going one right after another and colored in the same color.
Your task is to color the figures in such a way that the number of distinct colors used is the minimum possible and there are no figures of the different types going one right after another and colored in the same color. If you use exactly k
distinct colors, then the colors of figures should be denoted with integers from 1 to k
.
Input
The input contains one or more test cases.
The first line contains one integer q
(1≤q≤104) — the number of test cases in the test. Then q
test cases follow. One test case is given on two lines.
The first line of the test case contains one integer n
(3≤n≤2⋅105) — the number of figures in the carousel. Figures are numbered from 1 to n in order of carousel moving. Assume that after the n-th figure the figure 1
goes.
The second line of the test case contains n
integers t1,t2,…,tn (1≤ti≤2⋅105), where ti is the type of the animal of the i
-th figure.
The sum of n
over all test cases does not exceed 2⋅105
.
Output
Print q
answers, for each test case print two lines.
In the first line print one integer k
— the minimum possible number of distinct colors of figures.
In the second line print n
integers c1,c2,…,cn (1≤ci≤k), where ci is the color of the i
-th figure. If there are several answers, you can print any.
Example
Input
4
5
1 2 1 2 2
6
1 2 2 1 2 2
5
1 2 1 2 3
3
10 10 10
Output
2
1 2 1 2 2
2
2 1 2 1 2 1
3
2 3 2 3 1
1
1 1 1
二、算法分析说明与代码编写指导(官方题解)
答案最多是 3。
首先,如果所有动物都一样,那么只用一种颜色去涂就可以了。
否则,分两种情况:
(1)当 n 是偶数时,无论相邻的两格是否相同,都按照 1,2,1,2,……(或 2,1,2,1,……)的涂色方案涂色即可。
(2)当 n 是奇数时:
①如果发现一对相邻的同种动物,那么将这一对涂上同一种颜色,然后把这一对看成是一个动物,套用 n 为偶数的情形即可。注意:只需要找到一对相邻的同种动物,就立即这样做。
②如果每个相邻的动物都不一样,由于整个图是一个环,不是二分图,因此最少需要 3 种颜色。在 n 是偶数的基础上改动一下,按照 1,2,1,2,……,3 的方法涂色即可。
注意:旋转木马是首尾相接的,判断相邻的两个位置的动物是否相同还要考虑编号 0 和 n - 1 两个位置。
三、AC 代码
#include<cstdio>
#include<algorithm>
#pragma warning(disable:4996)
using namespace std;
const unsigned nmax = 2e5;
unsigned n, q, t[nmax], a[nmax], s;
inline unsigned flip(const unsigned& x) {
switch (x) {
case 1:return 2;
default:return 1;
}
}
int main() {
scanf("%u", &q); ++q;
while (--q) {
scanf("%u", &n); for (unsigned i = 0; i < n; ++i)scanf("%u", &t[i]);
if (count(t, t + n, *t) == n) {
puts("1"); for (unsigned i = 0; i < n; ++i)fputs("1 ", stdout);
putchar('\n');
}
else {
if (n % 2 == 0) {
puts("2"); for (unsigned i = 0; i < n; i += 2)fputs("1 2 ", stdout);
putchar('\n');
}
else {
a[0] = 1; s = n;
for (unsigned i = 1; i < n; ++i) {
if (t[i] == t[i - 1]) { s = i; a[i] = a[i - 1]; break; }
else { a[i] = flip(a[i - 1]); }
}
for (unsigned i = s + 1; i < n; ++i)a[i] = flip(a[i - 1]);
if (s == n && t[0] == t[n - 1]) { s = 0; a[0] = a[n - 1]; }
if (s == n) { puts("3"); a[n - 1] = 3; }
else { puts("2"); }
for (unsigned i = 0; i < n; ++i)printf("%u ", a[i]);
putchar('\n');
}
}
}
return 0;
}