zoukankan      html  css  js  c++  java
  • D. Carousel

    原题

    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.

    The 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 (1q1041≤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 (3n21053≤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 (1ti21051≤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 21052⋅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 (1cik1≤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 

    分四种情况判断

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    #define ll              long long
    #define pii             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define forn(i, n)      for(int i = 0; i < int(n); i++)
    using namespace std;
    int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const double eps = 1e-6;
    const int mod = 1e9 + 7;
    const int N = 2e5 + 5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    bool prime(int x) {
        if (x < 2) return false;
        for (int i = 2; i * i <= x; ++i) {
            if (x % i == 0) return false;
        }
        return true;
    }
    ll qpow(ll m, ll k, ll mod)
    {
        ll res = 1, t = m;
        while (k)
        {
            if (k & 1)
                res = res * t % mod;
            t = t * t % mod;
            k >>= 1;
        }
        return res;
    }
    
    int main()
    {
        int T;
        cin >> T;
        while (T--)
        {
            int n, cnt = 0, fg = 0;
            cin >> n;
            vector<int> a(n + 1);
            rep(i, 1, n)
            {
                cin >> a[i];
                if (a[i] != a[i - 1])
                {
                    cnt++;
                }
                else
                    fg = 1;
            }
            if (cnt == 1)
            {
                cout << 1 << endl;
                rep(i, 1, n)
                    cout << 1 << " ";
                cout << endl;
                continue;
            }
            if (a[n] == a[1] || cnt%2==0)
            {
                int t = 0;
                cout << 2 << endl;
                rep(i, 1, n)
                {
                    if (a[i] != a[i - 1])
                        t++;
                    cout << t % 2 + 1<< " ";
                }
                cout << endl;
                continue;
            }
            if (n % 2 == 0)
            {
                int t = 0;
                cout << 2 << endl;
                rep(i, 1, n)
                {
                    t++;
                    cout << t % 2 + 1 << " ";
                }
                cout << endl;
                continue;
            }
            if (fg)
            {
                int t = 0;
                cout << 2 << endl;
                rep(i, 1, n)
                {
                    if (a[i] != a[i - 1])
                        t++;
                    else
                    {
                        if (fg)
                        {
                            fg = 0;
                            t++;
                        }
                    }
                    cout << t % 2 + 1 << " ";
                }
                cout << endl;
                continue;
            }
            int t = 0;
            cout << 3 << endl;
            rep(i, 1, n-1)
            {
                if (a[i] != a[i - 1])
                    t++;
                cout << t % 2 + 1 << " ";
            }
            cout << 3 << endl;
        }
        return 0;
    }
  • 相关阅读:
    获取MAC地址的两种方法
    发现使用wcf传输的文件有20K的丢失
    用 document.readyState == "complete" 判断页面是否加载完成。
    防止SQL注入ASP代码
    什么是极端编程?
    Web.config 节点含义
    去掉网页上的图片工具栏
    程序员—青春饭?
    历史大骗局:广岛长崎原子弹爆炸
    最佳实践
  • 原文地址:https://www.cnblogs.com/dealer/p/13233889.html
Copyright © 2011-2022 走看看