zoukankan      html  css  js  c++  java
  • [Codeforces Round #629 (Div. 3)]

    D. Carousel

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    The round carousel consists of nn figures of animals. Figures are numbered from 11 to nn in order of the carousel moving. Thus, after the nn-th figure the figure with the number 11 follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal of the ii-th figure equals titi.

    imgThe example of the carousel for n=9n=9 and t=[5,5,1,15,1,5,5,1,1]t=[5,5,1,15,1,5,5,1,1].

    You want to color each figure in one of the colors. You think that it's boring if the carousel contains two different figures (with the distinct types of animals) going one right after another and colored in the same color.

    Your task is to color the figures in such a way that the number of distinct colors used is the minimum possible and there are no figures of the different types going one right after another and colored in the same color. If you use exactly kk distinct colors, then the colors of figures should be denoted with integers from 11 to kk.

    Input

    The input contains one or more test cases.

    The first line contains one integer qq (1≤q≤1041≤q≤104) — the number of test cases in the test. Then qq test cases follow. One test case is given on two lines.

    The first line of the test case contains one integer nn (3≤n≤2⋅1053≤n≤2⋅105) — the number of figures in the carousel. Figures are numbered from 11 to nn in order of carousel moving. Assume that after the nn-th figure the figure 11 goes.

    The second line of the test case contains nn integers t1,t2,…,tnt1,t2,…,tn (1≤ti≤2⋅1051≤ti≤2⋅105), where titi is the type of the animal of the ii-th figure.

    The sum of nn over all test cases does not exceed 2⋅1052⋅105.

    Output

    Print qq answers, for each test case print two lines.

    In the first line print one integer kk — the minimum possible number of distinct colors of figures.

    In the second line print nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤k1≤ci≤k), where cici is the color of the ii-th figure. If there are several answers, you can print any.

    Example

    input

    Copy

    4
    5
    1 2 1 2 2
    6
    1 2 2 1 2 2
    5
    1 2 1 2 3
    3
    10 10 10
    

    output

    Copy

    2
    1 2 1 2 2
    2
    2 1 2 1 2 1
    3
    2 3 2 3 1
    1
    1 1 1 
    

    题意:

    (mathit t)组数据,每一组数据中给定一个长度为(mathit n) 的数组,代表第(mathit i)个位置的动物类型,这$mathit n (个数将其)a_1(与)a_n$首位相接构成一个环。

    现在让你用最小的颜色种类(颜色从1开始),去给这个环染色,要求:如果环中相邻的( ext 2)个动物类型不同则需要将这两位染为不同的颜色。

    思路:

    讨论:

    1、所有的(a_i)都相等,那么只需要( ext 1)个颜色,全部染为( ext 1).

    2、至少需要$ ext 2 $个颜色:

    ①:当(mathit n) 为偶数,那么只需要( ext 2)个颜色,染为(1,2,1,2,dots,1,2).

    ②:当(mathit n) 为奇数,去判断环中是否有两个相邻的数字相等,

    ​ 如果有:则将其假象的合并为一个数字(染成相同的颜色),这样就变成了类似偶数,其他的当偶数染$1,2 $交叉循环即可。

    ​ 否则需要( ext 3) 个颜色,染法:前(n-1) 个数染(1,2)循环,最后一个数染为( ext 3)

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #include <sstream>
    #include <bitset>
    #define ALL(x) (x).begin(), (x).end()
    #define sz(a) int(a.size())
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
    #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
    #define du2(a,b) scanf("%d %d",&(a),&(b))
    #define du1(a) scanf("%d",&(a));
    using namespace std;
    typedef long long ll;
    ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
    ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
    ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
    ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
    void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
    ");}}}
    void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
    ");}}}
    inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
    inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
    const int maxn = 1000010;
    const int inf = 0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    #define DEBUG_Switch 0
    int n;
    int a[maxn];
    int b[maxn];
    int cnt;
    int main()
    {
    #if DEBUG_Switch
        freopen("C:\code\input.txt", "r", stdin);
    #endif
        //freopen("C:\code\output.txt","r",stdin);
        int t;
        t = readint();
        while (t--)
        {
            n = readint();
            repd(i, 1, n)
            {
                a[i] = readint();
                b[i] = a[i];
            }
            sort(b + 1, b + 1 + n);
            cnt = unique(b + 1, b + 1 + n) - b - 1;
            if (cnt == 1)
            {
                printf("1
    ");
                repd(i, 1, n)
                {
                    printf("%d%c", 1, i == n ? '
    ' : ' ' );
                }
            } else
            {
                if (n % 2 == 0)
                {
                    printf("2
    ");
                    repd(i, 1, n)
                    {
                        printf("%d%c", (i & 1) ? 1 : 2, i == n ? '
    ' : ' ' );
                    }
                } else
                {
                    int flag = 0;
                    a[n + 1] = a[1];
                    repd(i, 1, n)
                    {
                        if (a[i] == a[i + 1])
                        {
                            flag = 1;
                        }
                    }
                    if (flag)
                    {
                        printf("2
    ");
                        int ans = 1;
                        repd(i, 1, n)
                        {
                            printf("%d%c", ans, i == n ? '
    ' : ' ' );
                            if (flag && a[i] == a[i + 1])
                            {
                                flag = 0;
                            } else
                            {
                                if (ans == 1)
                                {
                                    ans = 2;
                                } else
                                {
                                    ans = 1;
                                }
                            }
                        }
                    } else
                    {
                        printf("3
    ");
                        repd(i, 1, n - 1)
                        {
                            printf("%d ", (i & 1) ? 1 : 2);
                        }
                        printf("3
    ");
                    }
                }
            }
        }
    
        return 0;
    }
    
    
    
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    tensorflow学习笔记13
    Java——内部类
    Java——枚举
    Java——代码块
    Java——static
    Java——接口
    Java——final
    Java——权限修饰符
    Java——多态
    Java——抽象类
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/12582245.html
Copyright © 2011-2022 走看看