题目链接

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. 邬江兴院士:工业互联网安全&拟态防御

    尊敬的郑院士.曹书记.张秘书长,各位学术界的同仁们,很高兴在第一届工业互联网学术专题论坛上发言.我今天想谈的问题是工业互联网,这个概念很热,前景也很美好,很诱人.但是我认为工业互联网的安全挑战更严峻, ...

  2. Redis配置文件详解(redis.conf)

    # vi redis.conf   daemonize yes #是否以后台进程运行 pidfile /var/run/redis/redis-server.pid    #pid文件位置 port ...

  3. Linux系统学习 二十、SAMBA服务—介绍、安装、端口

    1.简介 网络数据文件共享服务器 可以和Windows中的网上邻居通用 数据共享的方法: Windows中最常用的是“网上邻居”.网上邻居使用的文件系统是CIFS(通用互联网文件系统)协议进行数据共享 ...

  4. Opencv中图像height width X 轴 Y轴 rows cols之间的对应关系

    这里做一个备忘录:

  5. CG-CTF SQL注入

    SQL注入1 题目 访问题目网址 先查看一下源码 仔细分析一下核心源码 <?php if($_POST[user] && $_POST[pass]) { //判断user和pas ...

  6. win10+Ubuntu16.04双系统下深度学习环境的搭建

    环境零零碎碎地搭了三四天,虽然碰到各种问题,但还是搭建好了,自己整理记录下,同时也算给有需要的人一些指导吧 一.双系统的安装 Win10硬盘管理助手 压缩或者直接利用未使用的空间,空间大小自定,将腾出 ...

  7. js中获取当前url路径

    可以使用 window.location 获取当前页面url.以下是一些简单应用. <script> $(function(){ // 返回 web 主机的域名,如:http://127. ...

  8. 如何使用1行代码让你的C++程序控制台输出彩色log信息

    本文首发于个人博客https://kezunlin.me/post/a201e11b/,欢迎阅读最新内容! colorwheel for colored print and trace for cpp ...

  9. flask的模板引擎jinja入门教程 包含一个通过网络实时传输Video视频流的示例

    本文首发于个人博客https://kezunlin.me/post/1e37a6/,欢迎阅读最新内容! tutorial to use python flask jinja templates and ...

  10. Java 创建线程的3种方法及各自优势

    1. 继承 Thread 类,然后调用 start 方法. class MyThread extends Thread { //重写run方法,线程运行后,跑的就是run方法 public void ...