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

 

全部评论

相关推荐

想去毕业旅行的斑马在...:学校不是92的话,没有实习经历投不了大厂,去投中小厂,拿点实习经历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
6539次浏览 63人参与
# 你的实习产出是真实的还是包装的? #
1304次浏览 32人参与
# MiniMax求职进展汇总 #
23225次浏览 302人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7077次浏览 37人参与
# 简历第一个项目做什么 #
31329次浏览 315人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186521次浏览 1115人参与
# 米连集团26产品管培生项目 #
4732次浏览 206人参与
# 研究所笔面经互助 #
118787次浏览 577人参与
# 面试紧张时你会有什么表现? #
30416次浏览 188人参与
# 简历中的项目经历要怎么写? #
309572次浏览 4163人参与
# 职能管理面试记录 #
10722次浏览 59人参与
# AI时代,哪些岗位最容易被淘汰 #
62757次浏览 749人参与
# 网易游戏笔试 #
6374次浏览 83人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6993次浏览 154人参与
# 腾讯音乐求职进展汇总 #
160437次浏览 1107人参与
# 从哪些方向判断这个offer值不值得去? #
56712次浏览 357人参与
# 正在春招的你,也参与了去年秋招吗? #
362741次浏览 2632人参与
# 你怎么看待AI面试 #
179417次浏览 1182人参与
# 小红书求职进展汇总 #
226905次浏览 1357人参与
# 你的房租占工资的比例是多少? #
92146次浏览 896人参与
# 校招笔试 #
467654次浏览 2954人参与
# 经纬恒润求职进展汇总 #
155713次浏览 1085人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务