CF1328D Carousel
作者:互联网
- 题意:在一个环中,给每个数涂色,要求不同的相邻的数字颜色不同。
- 题解:很显然的是,偶数只要是 \(121212\) 就可以保证都不相同,如果是奇数环,那么要小心头尾会相遇,那么如果还是 \(1212\) 那么如果 \(a_{n-1}\) 和 \(a_{1}\) 都 \(\neq a_{n}\) 那么确实会多一个,但是如果有存在两个相同的,那么把他俩染色了,就会 \(12112\) 那么最后一个无论如何都是合法的。所以首先,先判能不能一个颜色都染好,然后在判是否是奇数,然后在判奇数中存不存在有两个数相邻且相等。
- 代码:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const ll N = 1e6 + 9, mod = 1e9 + 7;
ll a[N];
ll ans[N];
ll b[N];
void solve() {
int n;scanf("%d", &n);
for (int i = 1; i <= n; i ++) scanf("%lld", &a[i]), a[i + n] = a[i];
for (int i = 2; i <= n; i ++) {
if (a[i] != a[i-1])break;
if (i ==n) {
cout << 1 << endl;
for (int i = 1; i <= n; i ++)cout << 1 << " ";
cout << endl;return;
}
}
ans[1] = 0;
ll d = 0;
for (int i = 1; i <= n; i ++) {
ans[i] = ans[i-1]^1;
}
int f = 0;
for (int i = 2; i <= n; i ++) {
if (a[i] == a[i-1]) {f = i;break;}
}
if(f && n%2) {
ans[f-1] = ans[f] = 0;
for (int i = f - 2; i >= 1; i--) {
ans[i] = ans[i + 1] ^ 1;
}
for (int i = f + 1; i <= n; i++) {
ans[i] = ans[i - 1] ^ 1;
}
} else if (n % 2){
if (a[1] != a[n] && a[n-1] != a[n]) {
ans[n] = 2;
}
}
cout << max(ans[n] + 1, (ll)2) << endl;
for (int i = 1; i <= n; i ++) {
cout << ans[i] + 1 << " ";
}
cout << endl;
}
signed main() {
int t;scanf("%d", &t);
while (t--) {
solve();
}
}
标签:那么,CF1328D,奇数,int,ll,Carousel,ans,include 来源: https://www.cnblogs.com/Xiao-yan/p/14669631.html