D.Carousel
- 题意:n个数环形排列,让你去染色,唯一的限制是相邻不相同的数要用不同颜色染色。问染色方案。(颜色使用要少)
- 题解: 如果不是环形只需要1,2交错排列就可以满足要求,但是环形会出现 a[n]!=a[1]&&res[n]==res[1]这种情况,我们可以令res[n]=3-res[n],再反向处理一遍,如果还是这种情况,就只能让res[n] = 3了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int N = 2e6 + 5;
const int mod = 1e9 + 7;
const double Pi = 3.1415926;
const ll INF = 0x3f3f3f3f;
int T;
int n, k;
int num = 0;
int a[N], res[N], b[N];
int main()
{
scanf("%d",&T);
while(T --){
num = 1;
scanf("%d",&n);
for(int i = 1; i <= n; ++ i){
scanf("%d",&a[i]);
}
res[1] = 1;
for(int i = 2; i <= n; ++ i){
if(a[i] != a[i - 1]) num = 2, res[i] = 3 - res[i - 1];
else res[i] = res[i - 1];
}
if(a[n] != a[1] && res[n] == res[1]){
res[n] = 3 - res[n];
for(int i = n - 1; i >= 1; -- i){
if(a[i] == a[i + 1]) break;
else res[i] = 3 - res[i + 1];
}
if(res[n] == res[1]){
num = 3;
res[n] = 3;
}
}
printf("%d\n",num);
for(int i = 1; i <= n; ++ i){
if(i == n) printf("%d\n",res[i]);
else printf("%d ",res[i]);
}
}
return 0;
}