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

题目大意

给一个数组,入伙前后两个元素值不同则必须让他们染不同的颜色,问你如何染色能使得使用的颜色种类最少

做法

用贪心的思路,从前往后扫一遍数组按照题目中的规则,如果与前一个元素相同就染同样的颜色,不同就染不一样的,但是只染1,2两种颜色。

由于是个环,最后只需要判断一下最后一个元素是不是和第一个元素不同的情况下染了相同的颜色,如果是,就看数组中有没有染了相同的颜色还挨在一起,如果有,把后面一个元素的颜色换成与前一个元素不同的,并且后面所有的元素都换成与原来相反的 x%2+1 ;正常输出即可。

代码

#include<bits/stdc++.h>
using namespace std;
int c[200005];
int b[200005];
void solve(){
    int n;
    cin>>n;
    c[0]=2;
    int flag=0,check=0,fl=0;
    for(int i=1;i<=n;i++){
        cin>>b[i];
        if(b[i]==b[i-1]){
            flag=i;
            c[i]=c[i-1];
        }
        else {
            c[i]=c[i-1]%2+1;
            if(i!=1)fl=1;
        }
    }
    if(b[n]!=b[1]&&c[n]==c[1]) check=1;
    if(check&&flag){
        cout<<2<<endl;
        for(int i=1;i<=n;i++){
            if(i>=flag) cout<<c[i]%2+1<<" ";
            else cout<<c[i]<<" ";
        }
    }
    else{
        if(check) cout<<3<<endl;
        else if(fl)cout<<2<<endl;
        else cout<<1<<endl;
        for(int i=1;i<=n;i++){
            if(check&&i==n)cout<<3<<" ";
            else cout<<c[i]<<" ";
        }
    }
    cout<<endl;
}
int main(){
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
} 

原文地址:https://www.cnblogs.com/LH2000/p/12579592.html

时间: 2024-07-30 16:42:25

D. Carousel.(简单做法)Codeforces.Round #629 (Div. 3)的相关文章

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 corre

Codeforces Round #629 (Div. 3)

A Divisibility Problem 题意 给你两个正整数\(a,b\) 你每次可以执行一次\(a++\)操作,问你最小的操作步数,使得\(a\)能被\(b\)整除 思路 数学 分情况讨论 当\(a<b\) 时,显然只有\(a == b\)时才能保证\(a\)被\(b\)整除 当\(a>b\) 时,只要把\(a\)调整到离\(kb\)最近的一个\(b\)的倍数,即\(\lceil {a / b}\rceil * b - a\) #include <bits/stdc++.h>

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 #629 (Div. 3) 解题报告

A [题意]签到题 B [题意]长度为n的字符串,由n-2个a和2个b组成,按照字典序排序,问第k个 [题解]读题细心,最开始以为是ab代表01,搞二进制转化,实际上上面是正确题意,直接算b的位置即可 C [题意]长度为n的由0,1,2组成的序列,分成两个数字,让他们的最小值最大 [题解]所以我们只需要让他尽量平均分即可,0可以分成0和0,2可以分成1和1,所以我们只有当碰到1的时候开始特殊处理,所以我们把1给一个数,然后下来所有的1和2都分配给另外一个数,以此能够保证分配的平均 D [题意]给

Codeforces Round #262 (Div. 2)460A. Vasya and Socks(简单数学题)

题目链接:http://codeforces.com/contest/460/problem/A A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put o

Codeforces Round #259 (Div. 2) (简单模拟实现题)

题目链接:http://codeforces.com/problemset/problem/454/A A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine