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.
Input
Lines 2.. N+1: Line i+1 contains a single character, F or B, indicating whether cow i is facing forward or backward.
Output
Sample Input
7
B
B
F
B
F
B
B
Sample Output
3 3
Hint
1 /************************************************************************* 2 > File Name: k.cpp 3 > Author: LiuGeXian 4 > Mail: 1019630230@qq.com 5 > Created Time: 2020/4/7 12:52:01 6 ************************************************************************/ 7 8 #include <iostream> 9 #include <cstdio> 10 #include <cstring> 11 #include <algorithm> 12 using namespace std; 13 const int maxn = 5e3 + 5; 14 int n, a[maxn], flag[maxn]; 15 int Jud(int k){ 16 memset(flag, 0, sizeof(flag));//ans是牛需要翻转的次数 17 int sum = 0, ans = 0;//sum是以当前点结尾的长度为k的区间中所有牛翻转的次数 18 for (int i = 1; i + k - 1 <= n; i++){ 19 if ((a[i] + sum)% 2 == 1){//flag[i]表示以i开头的长度为k的区间是否翻转 20 ans++; 21 flag[i] = 1; 22 } 23 sum += flag[i]; 24 if (i - k + 1 >= 1){ 25 sum -= flag[i - k + 1]; 26 } 27 } 28 for (int i = n - k + 2; i <= n; i++){//此时无法再将长度为k的区间翻转 29 if ((a[i] + sum) % 2 == 1) return 0;//需要判断是否能符合条件 30 if (i - k + 1 >= 1) sum -= flag[i - k + 1]; 31 } 32 return ans; 33 } 34 int main(){ 35 scanf("%d", &n); 36 getchar(); 37 for (int i = 1; i <= n; i++){ 38 char ch = getchar(); 39 getchar(); 40 if (ch == 'B') a[i] = 1;//定义牛如果朝后为则a[i]为1否则为0 41 else a[i] = 0; 42 } 43 int t, ans = 0x7fffffff; 44 for (int k = 1; k <= n; k++){ 45 int x = Jud(k); 46 if (x < ans && x != 0){ 47 t = k; 48 ans = x; 49 } 50 } 51 cout << t << ' ' << ans; 52 return 0; 53 }