Carousel CodeForces - 1328D(贪心+思维)

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
思路:我们考虑最少需要几种颜色就可以。对于前n-1个,我们可以1212这样的去排列,对于第n个,我们直接弄个3就可以了。所以最多只需要3种就能完成。那么我们考虑的就是如何去缩短这个数字。
对于第n个数字
情况一:如果等于第n-1个或者第一个中的一个数字的话,那么我们就将第n个染色为与不相等的那一个数字的颜色相反的颜色(1相反就是2,反之为1)。如果都相等的话,就随便染一个就可以了。
情况二:第一种情况不符合,即第n个和第n-1个与第1个都不一样。如果第n-1个和第1个颜色相同的话,将第n个染成相反颜色就可以了。如果不符合的话,那么我们就需要将前面中的某一个染成相反颜色(允许的话),那么这样2种颜色也可以。如果这种情况还是不符合,那么只能3种颜色了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=2e5+100;
int a[maxx];
int ans[maxx];
int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		ans[1]=1;
		for(int i=2;i<n;i++)
		{
			if(a[i]==a[i-1]) ans[i]=ans[i-1];
			else ans[i]=3-ans[i-1];
		}
		if(a[n]==a[n-1]&&a[n]!=a[1]) ans[n]=3-ans[1];
		else if(a[n]==a[1]&&a[n]!=a[n-1]) ans[n]=3-ans[n-1];
		else if(a[n]==a[1]&&a[n]==a[n-1]) ans[n]=ans[1];
		else 
		{
			if(ans[n-1]==ans[1]) ans[n]=3-ans[1];
			else
			{
				int mk=0,i;
				for(i=n-1;i>=2;i--)
				{
					if(a[i]==a[i-1])
					{
						mk=1;
						break;
					}
				}
				if(mk==1)
				{
					for(int j=i;j<=n-1;j++) ans[j]=3-ans[j];
					ans[n]=3-ans[1];
				}
				else ans[n]=3;
			}
		}
		int flag=1;
		for(int i=1;i<=n;i++) flag=max(flag,ans[i]);
		cout<<flag<<endl;
		for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
		cout<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

发布了596 篇原创文章 · 获赞 47 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/105157122