题目描述
Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are facing forward, like good cows. Some of them are facing backward, though, and he needs them all to face forward to make his life perfect.
Fortunately, FJ recently bought an automatic cow turning machine. Since he purchased the discount model, it must be irrevocably preset to turn K (1 ≤ K ≤ N) cows at once, and it can only turn cows that are all standing next to each other in line. Each time the machine is used, it reverses the facing direction of a contiguous group of K cows in the line (one cannot use it on fewer than K cows, e.g., at the either end of the line of cows). Each cow remains in the same *location* as before, but ends up facing the *opposite direction*. A cow that starts out facing forward will be turned backward by the machine and vice-versa.
Because FJ must pick a single, never-changing value of K, please help him determine the minimum value of K that minimizes the number of operations required by the machine to make all the cows face forward. Also determine M, the minimum number of machine operations required to get all the cows facing forward using that value of K.
N头牛排成一列1<=N<=5000。每头牛或者向前或者向后。为了让所有牛都 面向前方,农夫每次可以将K头连续的牛转向1<=K<=N,求操作的最少 次数M和对应的最小K。
输入输出格式
输入格式:Line 1: A single integer: N
Lines 2..N+1: Line i+1 contains a single character, F or B, indicating whether cow i is facing forward or backward.
输出格式:Line 1: Two space-separated integers: K and M
输入输出样例
7 B B F B F B B
3 3
说明
For K = 3, the machine must be operated three times: turn cows (1,2,3), (3,4,5), and finally (5,6,7)
算法分析:
题意:
N头牛排成一列1<=N<=5000。每头牛或者向前或者向后。为了让所有牛都面向前方,农夫每次可以将K头连续的牛转向1<=K<=N,求操作的最少次数M和对应的最小K。
分析:
反转操作来说顺序是没有影响的,这样如果我们每次考虑最左边的牛,那么每次改变之后左边的牛的方向将不会变化,只有右边的牛的方向会变化。那么可以选择从左边开始,枚举区间长度,每次从最左边向右遍历,将区间里面的牛反转,操作下来复杂度大概是O(n^3),这样会超时。
需要优化,想一下,一个牛两次反转之后就等于没有变化,这样我们只需要知道每一头牛翻转了多少次就行了,不必一头一头的操作。
我们用f[i]表示区间[i,k+i-1]是否进行了反转,是为1,否为0。这样每头牛反转的次数是它前面k-1头牛中出现的面朝后的牛的个数和,用sum表示,由于是一个区间[i,i+k-1]的反转,所以对于i,就有[i-k+1,i],[i-k+2,i+1]……[i,i+k-1]会影响i的所以可以形成一种递推关系,sum的计算:i+1只需要在i的基础上加上[i+1,i+k],并减去[i-k+1,i];
如果sum+a[i]为奇数则表示这头牛面朝后方,f[i]就等于1,表示这头牛需要反转,这样对于每头牛的操作都缩到了O(1)的时间复杂度。
代码实现:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
using namespace std;
const double eps = 1e-8;
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1e5 + 10;
const int MAXT = 10000 + 10;
const int N=5005;
int n,f[N],a[N]; //区间[i,i+k-1]是否进行反转
int solve(int k)
{memset(f,0,sizeof(f));int ans=0,sum=0; //sum表示zaii的反转次数for(int i=0;i+k<=n;i++){if((a[i]+sum)%2!=0) //细细体会,自己带带就明白{ans++; //记录反转次数f[i]=1; //区间【i,i-k+1】表示反转}sum+=f[i]; //i+!的反转次数只有当前区间有影响if(i-k+1>=0)sum-=f[i-k+1]; //减去区间范围外的}for(int i=n-k+1;i<n;i++ ) //检查后面的牛{if((a[i]+sum)%2!=0) //如果仍然有向后的return -1;if(i-k+1>=0)sum-=f[i-k+1];}return ans;
}
int main()
{ while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++){char s[2];scanf("%s",s);if(s[0]=='F')a[i]=0;if(s[0]=='B') a[i]=1;}int K=1,M=n,m;for(int k=1;k<=n;k++){m=solve(k);if(m>0&&M>m){M=m;K=k;}}printf("%d %d\n",K,M);} return 0; }