思維+模擬 Codeforces Round #629 (Div. 3) D題 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.


題目大意爲:給你n個圖案,讓你塗色,條件是相鄰的圖案如果相等那麼就不能塗相同的顏色,求最小不同色數;

這題比較思維,可以發現如果n是偶數,只要2種顏色就行,相鄰的分別塗1、2;

如果n是奇數,要想辦法把奇數變爲偶數,所以就可以進行縮點(相同顏色相鄰合併),這裏縮點不要縮多,只要2個就行,把奇數個變爲偶數個,然後分別塗1、2;

如果不能縮,那麼就是3個,最後一個塗3就行;

代碼:

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章