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;
}

 

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务