传送门
文章目录
- 题意:
- 思路:
题意:
思路:
恶心的构造题,思路很简单但是代码细节很多,搞了半天。
根据题目的性质不难发现,如果有两个相同颜色的球相邻,那么他们的颜色永远不会改变。
根据这个性质,我们将相同颜色的球作为分割点,将其分成若干段,每段都是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
*/