codeforces1328 D - Carousel(思維)

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.

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 (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 

題意:

旋轉木馬上有 n 個馬(首位相鄰),現在給 n 個馬塗色,每個馬都屬於一個種類,要求相鄰的不同種類的馬具有不同的顏色,求最少需要的顏色種類數,並輸出一種可行解

思路:

1.當所有的馬都屬於同一種時,需要一種顏色

2.(1)當馬的數量爲偶數,顏色可以是1,2,1,2,1,2……

   (2)當馬的數量爲奇數,若其中有相鄰的同一種類的馬,將它倆塗成一樣的顏色,然後未塗色的一端按原來的1,2,1,2的順序反轉;若沒有相鄰的同一種類的馬,除最後一匹馬顏色數是3外,其餘是1,2,1,2,1,2……

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const int N = 2e5 + 10;

int k[N], c[N];

int main()
{
    int t, n;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        memset(c, 0, sizeof(c));
        bool flag = 0;
        int id = -1;
        for(int i = 1; i <= n; ++i)
        {
            scanf("%d", &k[i]);
        }
        k[n + 1] = k[1];
        for(int i = 1; i <= n; ++i)
        {
            if(i & 1)
                c[i] = 1;
            else
                c[i] = 2;
            if(k[i] != k[i + 1])
            {
                flag = 1;
            }
            else
            {
                id = i;
            }
        }
        int cnt = 1;
        if(!flag)
        {
            for(int i = 1; i <= n; ++i)
            {
                c[i] = 1;
            }
        }
        else
        {
            cnt = 2;
            if(n & 1)
            {
                if(id != -1)
                {
                    c[id + 1] = c[id];
                    if(id == n)
                        c[1] = c[n];
                    for(int i = id + 1; i <= n; ++i)
                    {
                        if(i & 1)
                            c[i] = 2;
                        else
                            c[i] = 1;
                    }
                }
                else
                {
                    c[n] = 3;
                    cnt = 3;
                }
            }
        }
        cout<<cnt<<'\n';
        for(int i = 1; i <= n; ++i)
        {
            cout<<c[i];
            if(i < n)
                cout<<' ';
        }
        cout<<'\n';
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章