+ Analog thinking Codeforces Round # 629 (Div. 3) D title Carousel

Carousel

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.
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.


Title effect that: n patterns you, you let coloring, with the proviso that if adjacent patterns are equal then it can not be painted the same color, find the minimum number of different colors;

This problem thinking comparison can be found if n is even, as long as the two colors on the line, the adjacent coating 1,2 respectively;

If n is odd, to find a way to become even-odd, the point can be reduced (the same color adjacent combined), where condensation point do not shrink more than two as long as the line, to become an even number of odd, respectively, then 1,2-coated;

If you can not shrink, it is three, the last 3 painted on the line;

Code:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=200010;
const int M=2000100;
const LL mod=2e9;
int t[N]; 
int main(){
	int q;
	cin>>q;
	while(q--){
		int n,st=0;
		cin>>n;
		set<int>se;
		for(int i=1;i<=n;i++){
			scanf("%d",&t[i]);
			if(t[i]==t[i-1]&&!st) st=i-1;
			se.insert(t[i]);
		}
		if(!st&&t[n]==t[1]) st=n;
		if(se.size()==1){
			 printf("1\n");
			 for(int i=1;i<n;i++) printf("1 ");
			 printf("1\n");
		}
		else{
			if(n&1){
				if(st) printf("2\n");
				else printf("3\n");
				if(st==n){
					for(int j=1;j<n;j++){
						if(j&1) printf("1 ");
						else printf("2 ");
					}
					printf("1\n");
				}
				else if(st){
					for(int j=1;j<st;j++){
						if(j&1) printf("1 ");
						else printf("2 ");
					}
					if(st&1) printf("1 1 ");
					else printf("2 2 ");
					for(int j=st+2;j<=n;j++){
						if(j&1) printf("2 ");
						else printf("1 ");
					}
					printf("\n");
				}
				else{
					for(int j=1;j<n;j++){
						if(j&1) printf("1 ");
						else printf("2 ");
					}
					printf("3\n");
				} 
			}
			else{
				printf("2\n");
				for(int j=1;j<n;j++){
					if(j&1) printf("1 ");
					else printf("2 ");
				}
				printf("2\n");
			}	
		}	
	}
	return 0;
}
Published 264 original articles · won praise 46 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_44291254/article/details/105137696