Algorithm Notebook 2(2.1.2026~2.28.2026)

本文最后更新于 2026年2月1日 下午

CF 266B - Queue at the School (800)

题意:队伍只含 B/G,进行 tt 秒;每秒同时把所有 “BG” 变成 “GB”。输出最终队形。
思路:模拟 tt 轮;每轮从左到右扫描,遇到 BG 交换,并 i += 2 跳过下一位,避免本秒重复参与。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
int n,t;
cin >> n >> t;
cin >> s;
while(t--){
for(int i=0;i<n;){
if(s[i]=='B'&&s[i+1]=='G'){
s[i]='G';
s[i+1]='B';
i+=2;
}
else
i++;
}
}
cout << s;
return 0;
}

Algorithm Notebook 2(2.1.2026~2.28.2026)
https://www.mirstar.net/2026/02/01/alogorithm-notebook-2/
作者
onlymatt
发布于
2026年2月1日
许可协议