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 ...
随机推荐
- Android 项目优化(二):启动页面优化
一.启动页黑屏的问题 1.1 问题现象描述 Android App 启动页面黑屏的问题,现象表现为:Android 开发 App 启动时若没有做特殊处理的话,会出现一瞬间的白屏现象.即使启动页界面就加 ...
- 10.jenkins 按角色分配
在实际的生产中,需要项目比较多.不同的用户需要对应 不同的项目工程 .这个时候,我们需要按角色给与权限. 要实现这个功能,需要一个插件来完成 . Role-based Authorization St ...
- Network出现两次相同请求?
出现的状况 Network中出现了两个相同的请求(如图),两个发起了同样的请求,花的时间却不同,一个55ms,一个花了294ms. 两个相同的请求 什么情况啊?研究了一番,我发现有一个地方是不同的 ...
- 基于appium的fixture应用之代码重构
一.痛点分析 在appium自动化中,会话启动参数较多,我们使用了yaml配置文件来进行管理,并使用了PyYaml模块进行yaml文件内容的读取,我们知道,在测试场景中,不可能只会用到一种启动类型的参 ...
- go语言的错误处理
1.系统自己抛异常 //go语言抛异常 func test3_1() { l := [5] int {0,1,2,3,4} var index int = 6 fmt.Println(l) l[ind ...
- 传统jdbc存在的问题总结
1.数据库连接创建.释放频繁造成系统资源浪费,影响系统性能,可使用数据库连接池解决此问题. 2.sql语句中在代码中硬编码,代码不易维护,sql变动需要改变java代码. 3.使用preparedSt ...
- 【SDOI 2015】约数个数和
Problem Description 设 \(d(x)\) 为 \(x\) 的约数个数,给定 \(N\).\(M\),求 \[ \sum_{i=1}^N \sum_{j=1}^M d(ij) \] ...
- ef实现一次查询多个聚合函数的字段
想用ef来写一个统计字段的语句,如下所示 select sum(price) as price_total, sum(amount) as amount_total from table1 发现似乎实 ...
- PlayJava Day021
容器: Collection接口:定义了存取一组对象的方法,其子接口Set和List分别定义了存储方式 List:存储数据有序且可重复 ----> ArrayList Set:存储数据无序且不可 ...
- 面试题:python 中 staticmethod 和 classmethod有什么区别
面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...