奇怪的解法
1、尺取法
2、反转法:[USACO07MAR]面对正确的方式Face The Right Way
思路:主要是考虑一个点翻转两次后就会变回原样,然后用前缀和判断是否翻转就可以了:
code:
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<stack>
#include<bitset>
const int maxn=10006;
#include<cmath>
int n,m,dir[maxn];
int f[maxn],sum[maxn];
using namespace std;
inline int check(int i,int k)
{
if ((sum[i-1]-sum[max((i-k),0)])%2==0) return 1;
else return -1;
}
inline int solve(int k)
{
int fina=0;
for (int i=1;i<=n;++i)
{
int tmp;
tmp=dir[i]*check(i,k);
if (tmp==1) {sum[i]=sum[i-1];continue;}
else
{
if (n-i+1<k) return 0x3f3f3f3f; //注意特判
fina++;
sum[i]=sum[i-1]+1;
}
}
return fina;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;++i)
{
char tmp=getchar();
if (tmp=='\n') tmp=getchar();
if (tmp=='B') dir[i]=-1;
else dir[i]=1;
}
int ans=n,kk=1;
for (int i=1;i<=n;++i)
{
memset(sum,0,sizeof(sum));
int chang=solve(i);
if (chang<ans)
{
ans=chang;
kk=i;
}
}
printf("%d %d",kk,ans);
return 0;
}