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.
The input contains one or more test cases.
The first line contains one integer q () — 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 () — 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 (), where ti is the type of the animal of the i-th figure.
The sum of n over all test cases does not exceed .
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.
4
5
1 2 1 2 2
6
1 2 2 1 2 2
5
1 2 1 2 3
3
10 10 10
2
1 2 1 2 2
2
2 1 2 1 2 1
3
2 3 2 3 1
1
1 1 1
n个木马围成圈,为每个属性为t[i]的木马涂上一种颜色c[i],要求任何相邻的属性不同的木马颜色不同,问最少需要多少种颜色,并输出涂色方案(tip:属性相同的木马颜色可以不同也可以相同)
1. 如果所有木马属性都相同,那么最少要涂一种颜色,需要特判
2.属性相同的木马颜色可以不同也可以相同,那么我们可以交替涂1,2,1,2,...如果n为偶数,那么一定满足条件且只需要两种颜色
3. 如果n为奇数,那么我们希望最后一个数c[n]两边的颜色c[1]和c[n - 1]尽可能相同,这样我们就不用使用第三种颜色,使答案最佳
4.我们只要找到第n个数之前的t[i] = t[i - 1] (2 <= i <= n - 1),然后从i开始的每个数与1异或即可(写代码时需要从零开始,才能0,1异或,最后答案加1即可)
5.经过步骤4后,如果c[1] == c[n - 1],c[n] = c[1] ^ 1即可
6.如果c[1] != c[n - 1](也就是前n - 1个属性都不同,无法通过步骤4使得c[1] == c[n - 1]),且c[n]与相邻两个数c[1]和c[n - 1]都不相同,那么c[n] = 2(最后答案会加1),否则t[n] == t[n - 1] ->c[n] = c[n -1]或者 t[n] == t[1] -> c[n] = c[1]
#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 2e5 + 10;
int t[N], c[N];
int main()
{
int q;
cin >> q;
while (q --){
int n;
scanf("%d", &n);
int num = 0;
for (int i = 1; i <= n; ++ i){
scanf("%d", &t[i]);
if (t[i] == t[1]) num ++;
}
if (num == n){//特判答案为1
cout << 1 << endl;
for (int i = 1; i < n; ++ i) printf("1 ");
cout << 1 << endl;
continue;
}
c[1] = 0;
for (int i = 2; i <= n - 1; ++ i) c[i] = c[i - 1] ^ 1;//0,1交替
if (n & 1){//n为奇数
for (int i = 2; i <= n - 1; ++ i){
if (t[i] == t[i - 1]){//找到,使c[n - 1] == c[1]
for (int j = i; j <= n - 1; ++ j){
c[j] ^= 1;
}
break;
}
}
}
int ans = 2;
if (c[n - 1] == c[1]){
c[n] = c[1] ^ 1;//反正答案为2,即使c[n] == c[n - 1] == c[1],也为c[n]涂上不同颜色
}else {
if (t[n] != t[n - 1] && t[n] != t[1]){
ans = 3;//说明所有木马属性都不同
c[n] = 2;
}else {
if (t[n] == t[1]) c[n] = c[1];//和谁一样就涂谁的颜色
else c[n] = c[n - 1];
}
}
cout << ans << endl;
for (int i = 1; i < n; ++ i) printf("%d ", c[i] + 1);//答案要加1
cout << c[n] + 1 << endl;
}
return 0;
}