其他分享
首页 > 其他分享> > CF1328D Carousel

CF1328D Carousel

作者:互联网

原题链接

#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