Carousel

Carousel

Created by LXC on Tue May 7 20:39:19 2024

https://codeforces.com/problemset/problem/1328/D

ranting: 1800

tag: constructive algorithms, dp, graphs, greedy, math

problem

有$q$组询问,每组询问如下:

已知一个有$n(3\le n\le 210^5)$个点的*,点$i$的类型为$a_i$,现在需要给每个点进行染色,要求相邻不同类型的点的颜色不同且所用颜色数最小.输出颜色数及一种染色方案即可.(颜色从1开始)

注意:($\sum n)\le 2*10^5$.

solution

如果存在相邻节点是相同类型,那么只需要两种颜色。相邻节点是相同类型这两个节点可以是同种颜色也可以是不同颜色。

否则如果总数是奇数,需要三种颜色,偶数需要两种。

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

#include <bits/stdc++.h>
// #define SINGLE_INPUT
#define ll long long
#define ull unsigned long long
#define N 500005
#define MOD 998244353
using namespace std;

random_device seed;
ranlux48 engine(seed());
int random(int l, int r) {
uniform_int_distribution<> distrib(l, r);
return distrib(engine);
}
template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p) {
return os<<'['<<p.first<<", "<<p.second<<']';
}
template<class t> ostream& operator<<(ostream& os,const vector<t>& v) {
os<<'['; int s = 1;
for(auto e:v) { if (s) s = 0; else os << ", "; os << e; }
return os<<']';
}
template<class t,class u> ostream& operator<<(ostream& os,const map<t,u>& mp){
os<<'{'; int s = 1;
for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; }
return os<<'}';
}

void sol() {
int n;
cin >> n;
vector<int> a(n);
for (auto& i:a) cin >> i;
int p = -1;
for (int i=1; i<2*n; i++) {
if (a[i%n] == a[(i-1)%n]) {
p = i%n;
break;
}
}
if (p == -1) {
if (n%2) {
cout << "3\n3";
for (int i=1; i<n; i++) {
cout << " " << (i%2+1);
}
cout << "\n";
} else {
cout << "2\n";
for (int i=0; i<n; i++) {
cout << (i%2+1) << " ";
}
cout << "\n";
}
} else {
vector<int> ans(n);
int c = 1;
for (int i=0; i<n; i++) {
if (a[(i+p-1+n)%n] != a[(i+p)%n]) {
c = 3-c;
}
ans[(i+p)%n] = c;
}
cout << (count(ans.begin(), ans.end(), 1) == n ? 1 : 2) << "\n";
for (int i:ans) {
cout << i << " ";
}
cout << "\n";
}
}

int main() {
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef SINGLE_INPUT
int t;
cin >> t;
while (t--) {
sol();
}
#else
sol();
#endif
return 0;
}