题目链接

problem

有一个长度为\(n\)个点连成的环。每个点为黑色或白色。当一个点和与他相邻的两个点颜色不同时。该点的颜色就会改变。

问改变\(K\)次后每个点的颜色。

solution

发现两个性质:

1.发现如果一个点在第一次时就不需要改变。那么他以后都不需要改变。

2.如果有个点在某次不需要改变,那么下一次他相邻的两个点也一定不需要改变。

所有思路就很明显了。从不需要改变的点开始\(bfs\)。得到每个点最早不需要改变的时间。然后与\(K\)取\(min\)后计算出最终颜色就行了。

code

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const int N = 200010;
ll read() {
ll x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0';c = getchar();
}
return x * f;
}
char s[N];
int vis[N];
queue<int>q;
int main() {
int n = read(),K = read();
scanf("%s",s);
memset(vis,-1,sizeof(vis));
for(int i = 0;i < n;++i) {
if(s[i] != s[(i - 1 + n) % n] && s[i] != s[(i + 1) % n]);
else {
q.push(i),vis[i] = 0;
}
} while(!q.empty()) {
int u = q.front();q.pop(); if(vis[(u - 1 + n) % n] == -1) {
vis[(u - 1 + n) % n] = vis[u] + 1;q.push((u - 1 + n) % n);
}
if(vis[(u + 1) % n] == -1) {
vis[(u + 1) % n] = vis[u] + 1;q.push((u + 1) % n);
}
} for(int i = 0;i < n;++i) {
if(vis[i] == -1 || vis[i] > K) {
if(K & 1) putchar(s[i] == 'B' ? 'W' : 'B');
else putchar(s[i]);
}
else {
if(vis[i] & 1) putchar(s[i] == 'B' ? 'W' : 'B');
else putchar(s[i]);
}
} return 0;
}

CF1244F Chips的更多相关文章

  1. some problem

    CF1257F Make Them Similar $solution:$ 折半搜索后考虑如何维护两个数组的和,可以将 $A$ 中每个数减 $A_1$ ,$B$ 中每个数被减 $B_1$ ,$map$ ...

  2. 【UVALive - 5131】Chips Challenge(上下界循环费用流)

    Description A prominent microprocessor company has enlisted your help to lay out some interchangeabl ...

  3. Codeforces Round #194 (Div. 2) D. Chips

    D. Chips time limit per test:1 second memory limit per test:256 megabytes input:standard input outpu ...

  4. Codeforces Round #194 (Div. 1) B. Chips 水题

    B. Chips Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/333/problem/B D ...

  5. [2011WorldFinal]Chips Challenge[流量平衡]

    Chips Challenge Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. A - Chips

    Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells ...

  7. Chips CodeForces - 333B

    Chips CodeForces - 333B 题意:有一个n*n的棋盘,其中有m个格子被禁止.在游戏开始前要将一些芯片(?)放到四条边上(但不能是角上).游戏开始后,每次操作将每一个芯片移动到它四周 ...

  8. [译]Cookies Without Chocolate Chips

    Cookies Without Chocolate Chips In the HTTP sense, a cookie is a name with an associated value. A se ...

  9. Angular Material 学习笔记 Chips

    依据 material guidelines, chips 可以用来做 filter https://material.io/design/components/chips.html#filter-c ...

随机推荐

  1. 超宽banner图在版心居中

    步骤如下: 1.版心盒子设置相对定位relative 2.banner图设置绝对定位,设置block,清除默认的间距 3.banner图的left设置:left:50%:  margin-left:- ...

  2. Saltstack_使用指南09_远程执行-编写执行模块

    1. 主机规划 salt 版本 [root@salt100 ~]# salt --version salt (Oxygen) [root@salt100 ~]# salt-minion --versi ...

  3. C语言编程的一些小总结

    1. static:可用于定义静态局部变量 在局部变量前,加上关键字static,该变量就被定义成为一个静态局部变量. 举一个静态局部变量的例子: void fn() { static int n=1 ...

  4. Chrome 开发者工具实用操作

    Chrome 开发者工具实用操作 https://umaar.com/dev-tips/

  5. 寝室远程连接室友mysql数据库

    注意,本方法是适用于同一局域网下的远程连接 注意,本方法是适用于同一局域网下的远程连接 注意,本方法是适用于同一局域网下的远程连接 首先需要修改mysql数据库的相关配置,将user表中的host改为 ...

  6. CentOS自动化安装LAMP脚本

    #!/bin/bash #-- #blog:lizhenliang.blog.51cto.com ########## function ########## depend_pkg () { yum ...

  7. 【bzoj4671】异或图(容斥+斯特林反演+线性基)

    传送门 题意: 给出\(s,s\leq 60\)张图,每张图都有\(n,n\leq 10\)个点. 现在问有多少个图的子集,满足这些图的边"异或"起来后,这张图为连通图. 思路: ...

  8. devops--django+ldap

    首先,什么是Ldap,官方解释 轻型目录访问协议(英文:Lightweight Directory Access Protocol,缩写:LDAP,/ˈɛldæp/)是一个开放的,中立的,工业标准的应 ...

  9. ElementPath

    ElementTree库附带了一个简单的类似XPath的路径语言ElementPath主要区别在于,可以在ElementPath表达式中使用{namespace}标记符号但是,诸如值比较和函数之类的高 ...

  10. PalletOne调色板跨链的ETH提币实现

    实现区块链的跨链,最主要的诉求就是Token的转移,而Token的跨链转移又分为充币和提币2种操作.以PalletOne调色板来说,如果要把ETH跨链到PalletOne上来流转,就是ETH的充币操作 ...