Carousel (Codeforces 1328D)
Tags: Greedy, graphs

The round carousel consists of $$$n$$$ figures of animals. Figures are numbered from $$$1$$$ to $$$n$$$ in order of the carousel moving. Thus, after the $$$n$$$-th figure the figure with the number $$$1$$$ 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 $$$i$$$-th figure equals $$$t_i$$$.  The example of the carousel for $$$n=9$$$ and $$$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 $$$k$$$ distinct colors, then the colors of figures should be denoted with integers from $$$1$$$ to $$$k$$$.

Input: The input contains one or more test cases.The first line contains one integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of test cases in the test. Then $$$q$$$ test cases follow. One test case is given on two lines.The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) — the number of figures in the carousel. Figures are numbered from $$$1$$$ to $$$n$$$ in order of carousel moving. Assume that after the $$$n$$$-th figure the figure $$$1$$$ goes.The second line of the test case contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le 2 \cdot 10^5$$$), where $$$t_i$$$ is the type of the animal of the $$$i$$$-th figure.The sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$.

Output: Print $$$q$$$ answers, for each test case print two lines.In the first line print one integer $$$k$$$ — the minimum possible number of distinct colors of figures.In the second line print $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le k$$$), where $$$c_i$$$ is the color of the $$$i$$$-th figure. If there are several answers, you can print any.


Sample Inputs:
4
5
1 2 1 2 2
6
1 2 2 1 2 2
5
1 2 1 2 3
3
10 10 10

Sample Outputs:
2
1 2 1 2 2
2
2 1 2 1 2 1
3
2 3 2 3 1
1
1 1 1

Solution (Python):
def kar():
    w = int(input())
    for i in range(w):
        c = int(input())
        l = list(map(int, input().split()))
        if [l[0]] * c == l:
            print(1)
            print(('1 ' * c)[:-1])
        elif c % 2 == 0:
            print(2)
            print(('1 2 ' * (c // 2))[:-1])
        else:
            z = '1 '
            u = False
            k = 0
            if l[0] == l[-1]:
                print(2)
                print('1 2 ' * (c // 2) + '1')
            else:
                for i in range(1, c):
                    if l[i] == l[i - 1] and (not u):
                        z += z[-2] + ' '
                        k = 1
                        u = True
                    else:
                        z += str((i + k) % 2 + 1) + ' '
                if u:
                    print(2)
                    print(z[:-1])
                else:
                    print(3)
                    z = ''
                    for i in range(c):
                        z += str(i % 3 + 1) + ' '
                    z = z[:-1]
                    if z[-1] == '1':
                        z = z[:-1]
                        z += '2'
                    print(z)
kar()

Solution (Python):
import sys
q = int(input())
for _ in range(q):
    n = int(input())
    animals = list(map(int, input().split()))
    animals += [animals[0]]
    sum1 = sum([1 if animals[i] != animals[i + 1] else 0 for i in range(n)])
    if sum1 == 0:
        print(1)
        print(' '.join(['1'] * n))
        continue
    if sum1 == n and n % 2 == 1:
        print(3)
        print(' '.join(['1' if i % 2 == 0 else '2' for i in range(n - 1)] + ['3']))
        continue
    res = [0] * n
    color = 1
    change = n % 2 == 1
    for i in range(0, n):
        res[i] = str(color)
        color = -color + 3
        if change and animals[i] == animals[i + 1]:
            change = False
            color = -color + 3
    print(2)
    print(' '.join(res))

Solution (Python):
from collections import Counter
q = int(input())
for i in range(q):
    n = int(input())
    arr = input().split()
    count = Counter(arr)
    if len(count) == 1:
        k = 1
        ans = ['1'] * n
    elif n % 2 == 0:
        k = 2
        ans = ['1', '2'] * (n // 2)
    else:
        idx = -1
        for j in range(n - 1):
            if arr[j] == arr[j + 1]:
                idx = j
                break
        if arr[0] == arr[-1]:
            k = 2
            ans = ['1', '2'] * (n // 2)
            ans.append('1')
        elif idx == -1:
            k = 3
            ans = ['1', '2'] * (n // 2)
            ans.append('3')
        else:
            k = 2
            idx += 1
            tmp1 = ['1', '2'] * (idx // 2)
            if idx % 2:
                tmp1.append('1')
            tmp = n - idx
            tmp2 = ['1', '2'] * (tmp // 2)
            if tmp % 2:
                tmp2.append('1')
            ans = tmp1[::-1]
            ans.extend(tmp2)
    print(k)
    print(' '.join(ans))