标签:algorithm char usaco scan tac 判断 mem sum algo
思路:主要是考虑一个点翻转两次后就会变回原样,然后用前缀和判断是否翻转就可以了:
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;
}
标签:algorithm char usaco scan tac 判断 mem sum algo
原文地址:https://www.cnblogs.com/bullshit/p/9605454.html