思维+模拟 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;
}
发布了264 篇原创文章 · 获赞 46 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44291254/article/details/105137696