题解:
贪心
枚举k
然后判断一下是否可行
代码:
#include<bits/stdc++.h> using namespace std; const int N=5005; int n,a[N],b[N],sum,c[N]; char ch[10]; int main() { scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%s",&ch); if (ch[0]=='B')a[i]=1; else a[i]=0; } int ans=n,pos; for (int i=1;i<=n;i++) { int flag=0,zs=sum=0; for (int j=1;j<=n;j++){b[j]=a[j]; c[j]=0;} for (int j=1;j+i-1<=n;j++) { sum^=c[j]; if (b[j]^sum){sum^=1; c[j+i]=1; zs++;} } for (int j=n-i+2;j<=n;j++) { sum^=c[j]; if (b[j]^sum) { flag=1; break; } } if (!flag&&zs<ans) { ans=zs; pos=i; } } printf("%d %d",pos,ans); }