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.

扫描二维码关注公众号,回复: 10206356 查看本文章

使用了一波玄学做法,把重复的认为是一个数,那么就分成了偶数个和奇数个

偶数个就直接1212,重复部分颜色相同即可

奇数个又分为有重复和无重复

有重复,某一个重复部分变色一次就和偶数一样了

无重复,也就是1 2 3这样的,直接按照偶数输出,末尾变成3就可以了

代码感觉可优化部分非常多,因为是边想边写的,可能会有点乱

#define _CRT_SECURE_NO_WARNINGS
 
 
 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
#define INF 99999999 
#define MAXN 200000
 
int m, n, t, k;
 
 
int main() {
    cin >> t;
    while (t--) {
        cin >> n;
        int a[MAXN] = { 0 };
        int num = 1;
        int ex = 1;
        int t = 0;
        for (int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
 
            
            if (i >= 1 && a[i] == a[i - 1]) {
                ex = 0;
                t = i;
            }
            if (i>=1&&a[i] != a[i - 1]) {
                if (i == n - 1 && a[i] == a[0]) {
                    ex = 0;
                    t = i;
                }
                else {
                    num++;
                }
            }
        }
 
 
 
        if (num == 1) {
            printf("1\n");
            for (int i = 0; i < n; i++) {
                printf("1 ");
            }printf("\n");
        }
        else if (num % 2 == 0) {
            int mark = 1;
            printf("2\n");
            for (int i = 0; i < n-1; i++) {
                printf("%d ", mark);
                if (i!=n-1&&a[i] != a[i + 1]) {
                    if (mark == 2) {
                        mark = 1;
                    }
                    else {
                        mark = 2;
                    }
                }
            }printf("%d\n", mark);
        }
        else {
            if (ex == 0) {
                int mark = 1;
                printf("2\n");
                for (int i = 0; i < n; i++) {
                    printf("%d ", mark);
                    if ((i != n - 1 && a[i] != a[i + 1]) || t - 1 == i) {
                        if (mark == 2) {
                            mark = 1;
                        }
                        else {
                            mark = 2;
                        }
                    }
                }printf("\n");
            }else{
                int mark = 1;
                printf("3\n");
                for (int i = 0; i < n-1; i++) {
                    printf("%d ", mark);
                    if ((i != n - 1 && a[i] != a[i + 1])) {
                        if (mark == 2) {
                            mark = 1;
                        }
                        else {
                            mark = 2;
                        }
                    }
                }printf("3\n");
            }
        }
 
 
 
    }
 
 
 
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Vetsama/p/12578796.html
今日推荐