CF1244F Chips
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的更多相关文章
- some problem
CF1257F Make Them Similar $solution:$ 折半搜索后考虑如何维护两个数组的和,可以将 $A$ 中每个数减 $A_1$ ,$B$ 中每个数被减 $B_1$ ,$map$ ...
- 【UVALive - 5131】Chips Challenge(上下界循环费用流)
Description A prominent microprocessor company has enlisted your help to lay out some interchangeabl ...
- 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 ...
- 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 ...
- [2011WorldFinal]Chips Challenge[流量平衡]
Chips Challenge Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- A - Chips
Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells ...
- Chips CodeForces - 333B
Chips CodeForces - 333B 题意:有一个n*n的棋盘,其中有m个格子被禁止.在游戏开始前要将一些芯片(?)放到四条边上(但不能是角上).游戏开始后,每次操作将每一个芯片移动到它四周 ...
- [译]Cookies Without Chocolate Chips
Cookies Without Chocolate Chips In the HTTP sense, a cookie is a name with an associated value. A se ...
- Angular Material 学习笔记 Chips
依据 material guidelines, chips 可以用来做 filter https://material.io/design/components/chips.html#filter-c ...
随机推荐
- jQuery淡入淡出轮播图实现
大家好我是 只是个单纯的小白,这是人生第一次写博客,准备写的内容是Jquery淡入淡出轮播图实现,在此之前学习JS写的轮播图效果都感觉不怎么好,学习了jQuery里的淡入淡出效果后又写了一次轮播图效果 ...
- Django 执行 makemigrations 显示 No changes detected in app
在Django项目配置一下多数据库,但是运行 makemigrations 执行不正常 $ python manage.py makemigrations polls No changes detec ...
- Java学习笔记 -- 头代码
每次写Java程序都会忘记这个main代码怎么写,特意把他写下来,之后忘了还可以温故而知新. 程序猿们请千万不要鄙视我o(╯□╰)o public static void main(String[] ...
- LeetCode刷题191130 --基础知识篇 二叉搜索树
休息了两天,状态恢复了一下,补充点基础知识. 二叉搜索树 搜索树数据结构支持许多动态集合操作,包括Search,minimum,maximum,predecessor(前驱),successor(后继 ...
- ORA-16032和ORA-07286 LOG_ARCHIVE_DEST_1没生效
主备切换在备库startup时出现归档路径没写到spfile里...注意:修改参数时最好带上scope=spfile或scope=both,以免重启出现异常.SQL> startup mount ...
- MySQL 主从复制问题
导致SQL线程故障原因分析及解决方案 原因 1. 版本差异,参数设定不同,比如:数据类型的差异,SQL_MODE影响 2. 要创建的数据库对象已经存在 3. 要删除或修改的对象不存在 4. DML语句 ...
- CentOS自动化安装LAMP脚本
#!/bin/bash #-- #blog:lizhenliang.blog.51cto.com ########## function ########## depend_pkg () { yum ...
- Weather with you主题说明
使用前请确保拥有js权限!!! 源代码: css: /*广告去死*/ #ad_t2 { display: none !important; } #i-amphtml-fill-content { di ...
- 《Web Development with Go》Middleware之使用gorilla.handlers
可以方便的使用日志和压缩等功能. package main import ( "fmt" "log" "net/http" "os ...
- Pipe——高性能IO(一)
System.IO.Pipelines是一个新的库,旨在简化在.NET中执行高性能IO的过程.它是一个依赖.NET Standard的库,适用于所有.NET实现. Pipelines诞生于.NET C ...