Codeforces Round #629 (Div. 3) 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 (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 给动物涂色,唯一的限制是相邻的不同种动物不能为同一种颜色(注意这是一个环),求最少需要的颜色以及涂色方案。首先可以看出来只用一种颜色的话肯定必须要所有动物都是同一种。其次考虑只用两种颜色,一开始我想的是直接1 2 1 2 ...这样涂,但是如果总数为奇数且1和n非同种动物的话处理起来其实也比较麻烦....后来参考别人的博客,发现可以在同类相邻动物涂成一种颜色的前提下1 2 1 2这样涂,这样的话如果1和n是同种动物或1和n不同类且涂的颜色也不同时肯定是合法的;对于不合法的情况,首先找到任意一处连续的动物,从这段中间开始把后面全换成相反的颜色(这样能保证颜色不增加且合法,很巧妙),如果没有连续段的话就只能把最后一个数涂成3了。
#include <bits/stdc++.h>
using namespace std;
int num[200005],color[200005];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int i;
        int k=1;
        bool same=1;//判断是否全为一个数
        int conti=0;//判断是否有连续区域,有的话设置为下标
        scanf("%d",&num[1]);
        for(i=2;i<=n;i++)
        {
            scanf("%d",&num[i]);
            if(num[i]!=num[i-1])same=0;
            if(num[i]==num[i-1])conti=i;
        }
        if(same)
        {
            cout<<1<<endl;
            for(i=1;i<=n;i++)printf("1 ");
            cout<<endl;
        }
        else
        {
            color[1]=1;
            for(i=2;i<=n;i++)
            {
                if(num[i]==num[i-1])color[i]=color[i-1];
                else color[i]=3-color[i-1];
            }
            if(num[1]==num[n]||num[1]!=num[n]&&color[1]!=color[n])
            {
                cout<<2<<endl;
                for(i=1;i<=n;i++)printf("%d ",color[i]);
                cout<<endl;
            }
            else
            {
                if(conti)
                {
                    cout<<2<<endl;
                    for(i=1;i<=n;i++)
                    {
                        if(i<conti) printf("%d ",color[i]);
                        else printf("%d ",3-color[i]);
                    }
                    cout<<endl;
                }
                else
                {
                    color[n]=3;
                    cout<<3<<endl;
                    for(i=1;i<=n;i++)printf("%d ",color[i]);
                    cout<<endl;
                }
            }

        }

    }
}

CF上最少xx这种有的时候根据样例直接考虑一下0 1 2 3这些比较小的数,极有可能是思维题。

原文地址:https://www.cnblogs.com/lipoicyclic/p/12585237.html

时间: 2024-08-29 06:08:41

Codeforces Round #629 (Div. 3) D. Carousel(思维/贪心?)的相关文章

Codeforces Round #424 (Div. 2) D 思维 E set应用,树状数组

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) D. Office Keys 题意:一条直线上,有个办公室坐标 p,有 n个人在a[i],有 k把钥匙在b[i],每个人必须拿到一把钥匙,然后到办公室.问怎么安排花的时间最短. tags:还是不懂套路啊..其实多画两下图就能够感觉出来,2333 关键是要看出来,n个人拿的 n把钥匙应该是连续的. 然后,就是瞎暴力.. #include<bits/stdc++.h> usi

D. Carousel.(简单做法)Codeforces.Round #629 (Div. 3)

题目大意 给一个数组,入伙前后两个元素值不同则必须让他们染不同的颜色,问你如何染色能使得使用的颜色种类最少 做法 用贪心的思路,从前往后扫一遍数组按照题目中的规则,如果与前一个元素相同就染同样的颜色,不同就染不一样的,但是只染1,2两种颜色. 由于是个环,最后只需要判断一下最后一个元素是不是和第一个元素不同的情况下染了相同的颜色,如果是,就看数组中有没有染了相同的颜色还挨在一起,如果有,把后面一个元素的颜色换成与前一个元素不同的,并且后面所有的元素都换成与原来相反的 x%2+1 :正常输出即可.

Codeforces Round #541 (Div. 2) E 字符串 + 思维 + 猜性质

https://codeforces.com/contest/1131/problem/D 题意 给你n个字符串,字符串长度总和加起来不会超过1e5,定义字符串相乘为\(s*s1=s1+s[0]+s1+s[1]+s1+...+s1+s[size-1]+s1+s[size]+s1\),求n个字符串依次相乘后最长连续字符相同的子序列长度 题解 鬼畜的题意 or 难以优化的复杂度,都需要观察性质才能做,第二串要插入第一个串每个字符之间,可以看出字符数增长的速度很快,所以并不能把整个字符存下来 只看一种

Make k Equal from Codeforces Round #629 (Div. 3)

description you are given an array and you are asked to make \(k\) elements of it equal after some operations. you can make one of the following operations: take one of the minimum and increase its value by \(1\) take one of the maximum and subtract

Codeforces Round #629 (Div. 3) E. Tree Queries(lca题)

https://codeforces.com/contest/1328/problem/E E. Tree Queries You are given a rooted tree consisting of nn vertices numbered from 11 to nn. The root of the tree is a vertex number 11. A tree is a connected undirected graph with n−1n−1 edges. You are

Codeforces Round #629 (Div. 3) F - Make k Equal (离散化 树状数组维护前缀和)

https://codeforces.com/contest/1328/problem/F 首先把a数组处理成pair对(num,cnt),表示数字num有cnt个,然后按num升序排序离散化一下. 对于一个数x,若想使得小于x的数字都变成x,必须先把所有小于x的数变成x-1,然后再+1变成x. 同理,要使得大于x的数变成x,必须把所有大于x的数字变成x+1,然后再-1变成x. 以上是题意所要求的必须操作. 思路: 1. 用f[i]数组记录离散化后前i大的数字的总数,那么对于任意第i大数字,可以

Codeforces Round #629 (Div. 3) E. Tree Queries(LCA)

https://codeforces.com/contest/1328/problem/E 题目所描述的是一棵树,题中已明示1为root结点. 题目可以转化为,是否存在一条路径,满足集合中的k个点到路径的距离小于等于1? 思路: 1.首先倍增离线预处理出结点深度,便于后续在线询问LCA 2.对于每次的询问,依次扫描k个点.对于集合中的u和v两点,每次我们求出u和v的LCA,计算u和v到LCA的距离,如果u和v到LCA的距离同时大于1,那么说明无法找到一条路径,使得u和v到该路径链的距离小于等于1

Codeforces Round #581 (Div. 2)D(思维,构造,最长非递减01串)

#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[100007];int main(){ cin>>s+1; int n=strlen(s+1); int cnt=0; for(int i=n;i>=1;--i){//从后向前,保证后面的解都是合法的情况下 if(s[i]=='1'){//如果当前位置的数字是1 if(cnt)//i后面1的个数小于0的个数,此时如果把i位

Codeforces Round #589 (Div. 2)D(思维,构造)

#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;vector<int>adj[100007];map<vector<int>,int>mp;int main(){ int n,m; scanf("%d%d",&n,&m); int u,v; for(int i=1;i<=m;++i){ scanf("%d%d&qu