-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA.cpp
39 lines (38 loc) · 758 Bytes
/
A.cpp
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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
int a[maxn], d[maxn];
int main() {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
queue <int> Q;
for(int i = 1; i <= n; i++) {
d[i] = -1;
if(i == 1 || i == n) {
d[i] = 0;
} else if (a[i - 1] == a[i] || a[i] == a[i + 1]) {
d[i] = 0;
}
if(d[i] == 0) Q.push(i);
}
int ans = 0;
while(!Q.empty()) {
int x = Q.front();
Q.pop();
for(int i : {x - 1, x + 1}) {
if(d[i] == -1) {
d[i] = d[x] + 1;
a[i] = a[x];
Q.push(i);
}
}
ans = max(ans, d[x]);
}
printf("%d\n", ans);
for(int i = 1; i <= n; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}