Codeforces 1244F Chips(环修改,思维题)

链接:https://codeforces.com/contest/1244/problem/F

题意:给出一个每个节点黑色或者白色的环,k次更新,每次更新将所有满足条件的节点颜色反转(同时进行),满足条件:该节点的相邻的2个节点颜色和它不同。求k次后各个节点的颜色。

题解:环的话,序列复制向左右分别扩展一倍数,最后看中间部分,可以发现,颜色相同的连续的节点不会发生变化,颜色不同的序列,直接暴力更新即可,复杂度O(n)

#include <bits/stdc++.h>
#define IO_read ios::sync_with_stdio(false);cin.tie(0)
#define fre freopen("in.txt", "r", stdin)
#define _for(i,a,b) for(int i=a; i< b; i++)
#define _rep(i,a,b) for(int i=a; i<=b; i++)
#define inf 0x3f3f3f3f
#define lowbit(a) ((a)&-(a))
using namespace std;
typedef long long ll;
template <class T>
void read(T &x)
{
    char c; bool op=0;
    while(c=getchar(), c<‘0‘||c>‘9‘) if(c==‘-‘) op=1;
    x=c-‘0‘;
    while(c=getchar(), c>=‘0‘&&c<=‘9‘) x=x*10+c-‘0‘;
    if(op) x=-x;
}
template <class T>
void write(T x)
{
    if(x<0) putchar(‘-‘), x=-x;
    if(x>=10) write(x/10);
    putchar(‘0‘+x%10);
}

const int maxn=2e5+5;
char s[maxn];
int color[maxn*3];
struct Segment{
    int l, r, color;
}segment[maxn*3];
int n, k, tot;

int main()
{
    IO_read;
    //fre;
    cin>>n>>k;
    cin>>(s+1);
    for(int i=1; i<=n; i++) color[i]= s[i]==‘B‘?1:0;
    for(int i=n+1; i<=3*n; i++) color[i]=color[(i-1)%n+1];
    for(int i=1,j=1; i<=3*n; i=j+1)
    {
        while(j+1<=3*n&&color[i]==color[j+1]) ++j;
        if(j-i>=1)
            segment[++tot]={i, j, color[i]};
    }
    for(int i=1; i<tot; i++)
    {
        int l=segment[i].r+1, r=segment[i+1].l-1;
        int lc=color[l-1], rc=color[r+1];
        if(l>r) continue;
        int len=r-l+1;
        if(2*k>=len){
            if(lc==rc){
               for(int j=l; j<=r; j++) color[j]=lc;
            }
            else{
                for(int j=l; j<=l+len/2-1; j++) color[j]=lc;
                for(int j=r; j>=r-len/2+1; j--) color[j]=rc;
            }
        }
        else{
            for(int j=l; j<=l+k-1; j++) color[j]=lc;
            for(int j=r; j>=r-k+1; j--) color[j]=rc;
            for(int j=l+k; j<=r-k; j++) color[j]=!color[j-1];
        }
    }
    if(tot==0&&k%2) for(int i=n+1; i<=2*n; i++) color[i]=!color[i-1];  //要判断tot为0且k为奇数的时候
    for(int i=n+1; i<=2*n; i++)
        printf("%c", color[i]?‘B‘:‘W‘);
    return 0;
}

原文地址:https://www.cnblogs.com/Yokel062/p/11760831.html

时间: 2024-07-29 02:42:55

Codeforces 1244F Chips(环修改,思维题)的相关文章

codeforces 848B Rooter&#39;s Song 思维题

http://codeforces.com/problemset/problem/848/B 给定一个二维坐标系,点从横轴或纵轴垂直于发射的坐标轴射入(0,0)-(w,h)的矩形空间.给出点发射的坐标轴,位置,延迟时间,发生碰撞则交换方向.求最后每个点的射出位置. 首先我们观察能得出两个结论,1. 类似蚂蚁爬树枝的问题,相遇只会交换方向,所以最后的射出点集只会因为碰撞而改变动点与射出点的对应关系,而不会增加减少射出点集.2.我们根据其射入位置和延迟时间可以计算出一个值v=pos-time,只有这

Codeforces 901C. Bipartite Segments(思维题)

擦..没看见简单环..已经想的七七八八了,就差一步 显然我们只要知道一个点最远可以向后扩展到第几个点是二分图,我们就可以很容易地回答每一个询问了,但是怎么求出这个呢. 没有偶数简单环,相当于只有奇数简单环,没有环套环.因为如果有环套环,必定是两个奇数环合并1个或几个点,也就是同时保持奇数或者同时变为偶数,而我们知道奇数+奇数=偶数,偶数+偶数=偶数,所以就证明了只有奇数简单环,不存在环套环. 我们现在有一些点,再加入一个点,最多会形成一个环,并且一定是奇环,这时候,编号为1~环上的最小编号的点,

Codeforces 1244F. Chips

传送门 显然可以注意到连续的两个相同颜色的位置颜色是不会改变的,并且它还会把自己的颜色每秒往外扩展一个位置 同时对于 $BWBWBW...$ 这样的序列,它每个位置的颜色每一秒变化一次 然后可以发现,对于一个位置 $x$,在 $x$ 左边和右边 连续两个相同颜色 扩展到 $x$ 之前, $x$ 的颜色一定是每秒变化一次 考虑每个位置 $x$ 第 $k$ 秒时的颜色,如果 $x$ 初始往 左或者往右的连续两个相同颜色 扩展到 $x$ 的时间都大于 $k$ ,那么我们可以通过 $k$ 的奇偶性和 $

贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

题目传送门 1 /* 2 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 3 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0),先从1开始找到已经套好的娃娃层数, 4 其他是2次操作,还要减去k-1个娃娃是只要套上就可以 5 详细解释:http://blog.csdn.net/firstlucker/article/details/46671251 6 */ 7 #include <cstdio> 8 #i

CodeForces 1131B(思维题)

You still have partial information about the score during the historic football match. You are given a set of pairs (ai,bi)(ai,bi), indicating that at some point during the match the score was "aiai: bibi". It is known that if the current score

Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) A. Contest for Robots(思维题)

Polycarp is preparing the first programming contest for robots. There are nn problems in it, and a lot of robots are going to participate in it. Each robot solving the problem ii gets pipi points, and the score of each robot in the competition is cal

Codeforces Round #634 (Div. 3) 补题

A. Candies and Two Sisters 签到题,直接输出即可 代码 #include<bits/stdc++.h> #define INF 0x3f3f3f3f typedef long long ll; using namespace std; inline void read(int &p) { p=0;int flag=1;char c=getchar(); while(!isdigit(c)) {if(c=='-') flag=-1;c=getchar();} w

Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to

Unique Encryption Keys (思维题 预处理)

题目 题意:给m个数字, q次询问, 询问b到e之间如果有重复数字就输出, 没有就输出OK 思路:用f[i]数组 记录从i开始向后最近的有重复数字的 位置, 如 1 3 2 2, 则f[1] = 4; 如果离a最近的重复数字的位置 都大于b, 就说明没有重复数字. f[]数组需要预处理,从后向前. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector>