传送门

文章目录

  • 题意:
  • 思路:

题意:

思路:

恶心的构造题,思路很简单但是代码细节很多,搞了半天。
根据题目的性质不难发现,如果有两个相同颜色的球相邻,那么他们的颜色永远不会改变。
根据这个性质,我们将相同颜色的球作为分割点,将其分成若干段,每段都是WBWBWBWBWBWBWBWBWB或者BWBWBWBWBWBWBWBWBW这样交替来的序列,不难发现每次操作只会将其两端的颜色改变,所以直接模拟就好了。注意首端和末端是相连的,还需要特判一下。
还需要注意,如果kkk的次数不能遍历完整个序列,由于序列没有遍历到的部分也是变化的,所以特判一下kkk的奇偶性,让后提前改变一下即可。

UPD:
貌似可以直接记录一下每个位置最少在第几次操作内改变,让后判断一下跟kkk的关系即可。
最后贴了个代码,比我思路好写1W1W1W倍。

// Problem: F. Chips
// Contest: Codeforces - Codeforces Round #592 (Div. 2)
// URL: https://codeforces.com/contest/1244/problem/F
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n,k;
char s[N];
char ans[N];
bool st[N];bool check() {for(int i=0;i<n;i++) if(st[i]) return true;return false;
}int main()
{//  ios::sync_with_stdio(false);
//  cin.tie(0);cin>>n>>k>>(s);for(int i=0;i<n;i++) {if(s[i]==s[(i-1+n)%n]||s[i]==s[(i+1)%n]) st[i]=true;}if(!check()) {k%=2;if(k) {for(int i=0;i<n;i++) if(s[i]=='B') s[i]='W'; else s[i]='B';}printf("%s\n",s);} else {int end=n-1,begin=0;while(!st[begin]) begin++; begin--;while(!st[end]) end--; end++;if(k%2==1) {for(int i=0;i<=begin;i++) if(s[i]=='B') s[i]='W'; else s[i]='B';for(int i=end;i<n;i++) if(s[i]=='B') s[i]='W'; else s[i]='B';}int cnt=begin+1+n-end;begin+=n; begin%=n; end%=n;for(int l=end,r=begin,t=k;cnt>0&&t;t--,cnt-=2) {char ls=s[(l-1+n)%n],rs=s[(r+1)%n];s[l]=ls; s[r]=rs;l++; r--;r+=n; r%=n; l%=n;}end--; begin++; end+=n; end%=n; begin%=n;for(int i=begin;i<=end;i++) {if(st[i]) continue;int l=i;int cnt=0;while(i<=end&&!st[i]) cnt++,i++; i--;int r=l+cnt-1;if(k%2==1) {for(int i=l;i<=r;i++) if(s[i]=='B') s[i]='W'; else s[i]='B';}for(int t=k;t&&l<=r;t--) {char ls=s[(l-1+n)%n],rs=s[(r+1)%n];s[l]=ls; s[r]=rs;l++; r--;}}printf("%s\n",s);}return 0;
}
/*
16 7
WBBWBWWBBWBWWBWW
WBBBWWWBBBWWWWWW
*/
// Problem: F. Chips
// Contest: Codeforces - Codeforces Round #592 (Div. 2)
// URL: https://codeforces.com/contest/1244/problem/F
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n,k;
char s[N];
char ans[N];
bool st[N];
int op[N];
char change[N];bool check() {for(int i=0;i<n;i++) if(st[i]) return true;return false;
}int main()
{//  ios::sync_with_stdio(false);
//  cin.tie(0);cin>>n>>k>>(s);for(int i=0;i<n;i++) {if(s[i]==s[(i-1+n)%n]||s[i]==s[(i+1)%n]) st[i]=true;}if(!check()) {k%=2;if(k) {for(int i=0;i<n;i++) if(s[i]=='B') s[i]='W'; else s[i]='B';}printf("%s\n",s);} else {memset(op,0x3f,sizeof(op));for(int i=0;i<n;i++) {int cnt,now;           if(st[i]&&!st[(i+1)%n]) {now=(i+1)%n; cnt=0;while(!st[now]) {cnt++;if(op[now]>cnt) {op[now]=cnt;change[now]=s[i];}now++; now%=n;}} if(st[i]&&!st[(i-1+n)%n]) {now=(i-1+n)%n; cnt=0;while(!st[now]) {cnt++;if(op[now]>cnt) {op[now]=cnt;change[now]=s[i];}now--; now+=n; now%=n;}}}for(int i=0;i<n;i++) {if(op[i]<=k) {s[i]=change[i];} else {if(op[i]!=INF&&k%2==1) {if(s[i]=='W') s[i]='B';else s[i]='W';}}}printf("%s\n",s);}return 0;
}
/*
16 7
WBBWBWWBBWBWWBWW
WBBBWWWBBBWWWWWW
*/

Codeforces Round #592 (Div. 2) F. Chips 构造 + 细节相关推荐

  1. Codeforces Round #592 (Div. 2) G. Running in Pairs 构造(水)

    传送门 文章目录 题意: 思路: 题意: 思路: 史上最水GGG题,没有之一. 考虑最小的情况如何构造,显然就是让a,ba,ba,b都1−n1-n1−n依次排列即可,这样的最小值为n∗(n+1)2\f ...

  2. Codeforces Round #699 (Div. 2) F - AB Tree(贪心、树上DP)超级清晰,良心题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) F - AB Tree Problem ...

  3. Codeforces Round #644 (Div. 3) F.Spy-string

    Codeforces Round #644 (Div. 3) F.Spy-string 题目链接 You are given n strings a1,a2,-,an: all of them hav ...

  4. Codeforces Round #849 (Div. 4) F. Range Update Point Query

    Codeforces Round #849 (Div. 4) F. Range Update Point Query 题目大意: 给一串数字,有两个操作: 操作1:将 l − r l-r l−r 的数 ...

  5. Codeforces Round #742 (Div. 2) F. One-Four Overload 构造 + 二分图染色

    传送门 文章目录 题意: 思路: 题意: 给你一个n∗mn*mn∗m的矩形,包含...和XXX,你有两种颜色,你需要给...染色使得每个XXX上下左右相邻的...其两种颜色个数相同,输出一种合法方案. ...

  6. Codeforces Round #585 (Div. 2) F. Radio Stations 2-sat + 神仙建模

    传送门 文章目录 题意: 思路: 题意: 你现在有ppp种电台,有nnn对关系(x,y)(x,y)(x,y)代表xxx电台或yyy电台中至少有一个,mmm对关系(x,y)(x,y)(x,y)代表xxx ...

  7. Codeforces Round #628 (Div. 2) F. Ehab‘s Last Theorem dfs树

    传送门 文章目录 题意: 思路: 题意: 给你个nnn个点mmm条边的图,可以选择完成以下两个任务中的一个: (1)(1)(1)找出大小恰好为n\sqrt nn​的一个独立集. (2)(2)(2)找出 ...

  8. Codeforces Round #538 (Div. 2) F. Please, another Queries on Array? 线段树 + 欧拉函数

    传送门 文章目录 题意: 思路: 题意: 给你一个序列aaa,你需要实现两种操作: (1)(1)(1) 将[l,r][l,r][l,r]的aia_iai​都乘rrr. (2)(2)(2) 求ϕ(∏i= ...

  9. Codeforces Round #740 (Div. 2) F. Top-Notch Insertions 线段树 / 平衡树 + 组合数学

    传送门 文章目录 题意: 思路: 题意: 思路: 考虑最终的序列是什么鸭子的,首先序列肯定单调不降,也就是a1≤a2≤a3≤...≤ana_1\le a_2\le a_3\le ...\le a_na ...

最新文章

  1. 亚马逊马超:如何使用DGL进行大规模图神经网络训练?
  2. 用人工神经网络控制真实大脑,MIT的科学家做到了
  3. 200kb以上图片储存
  4. 2016 Multi-University Training Contest 10 [HDU 5861] Road (线段树:区间覆盖+单点最大小)...
  5. Android 系统到底提供了哪些东西,供我们可以开发出优秀的应用程序(文末送书)
  6. 【Qt】MainWindow窗口状态栏
  7. SAP C4C计价(Pricing)中折扣(Discount)的使用
  8. Wordpress表结构详细说明
  9. 这5个有趣的Python库带你花式编码!
  10. 推荐一个简洁优雅的博客系统,farbox
  11. mha数据备份_MySQL备份与恢复之保证数据一致性(5)
  12. 寄存器之通用寄存器(一)
  13. android gps原始数据格式,Android编程获取GPS数据的方法详解
  14. openssl建立证书和私钥方法
  15. ios设备备份,更新路径(mac os)
  16. 爬虫工程师是干嘛的?Python爬虫工程师需要掌握哪些技能?
  17. JavaScript中的模块化之AMD和CMD
  18. 从今以后我一个人唱悲伤情歌:伤感的QQ空间日志
  19. 如何有效预防ddos攻击
  20. CentOS升级内核版本_linux升级内核版本_Redhat升级内核版本

热门文章

  1. 飞信linux下载文件,OpenFetion(飞信for Linux)
  2. 怎么做图片文字二维码一起_怎么做?才能让文字编排更出彩
  3. 据说很多女生都想知道男生是如何上厕所的?
  4. 13个圆可以画什么?数学与艺术完美邂逅!原来数学也可以这么美
  5. 这个地球仪太惊艳了,陪孩子畅聊天文地理。
  6. 浅谈协同过滤推荐算法
  7. mysql null值和空格_MySQL中NULL与空字符串空格问题
  8. java高并发类_Java 高并发之魂
  9. python求函数极值_python 遗传算法求函数极值的实现代码
  10. python3.7安装keras教程_Python3.7安装keras和TensorFlow的教程图解