Face The Right Way
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2564   Accepted: 1177

Description

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

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.

Output

Line 1: Two space-separated integers: K and M

Sample Input

7
B
B
F
B
F
B
B

Sample Output

3 3

Hint

For K = 3, the machine must be operated three times: turn cows (1,2,3), (3,4,5), and finally (5,6,7)
 #include"iostream"
#include"cstring"
#include"cstdio"
#include"algorithm"
#include"cstdlib"
#include"ctime"
using namespace std;
const int ms=;
int dir[ms];
int f[ms];
int N;
int calc(int K)
{
memset(f,,sizeof(f));
int res=;
int sum=;//f的∑
for(int i=;i+K<=N;i++)
{
if((dir[i]+sum)&)
{
res++;
f[i]=;
}
sum+=f[i];
if(i-K+>=)
{
sum-=f[i-K+];
}
}
for(int i=N-K+;i<N;i++)
{
if((dir[i]+sum)&)
return -;
if((i-K+)>=)
sum-=f[i-K+];
}
return res;
}
void solve()
{
int K=,M=N;
for(int k=;k<=N;k++)
{
int m=calc(k);
if(m>=&&M>m)
{
M=m;
K=k;
}
}
printf("%d %d\n",K,M);
}
int main()
{
scanf("%d",&N);
char str[];
for(int i=;i<N;i++)
{
scanf("%s",str);
if(str[]=='B')
dir[i]=;
else
dir[i]=;
}
solve();
return ;
}

随机推荐

  1. Hadoop上路-03_Hadoop JavaAPI

    一.Eclipse安装 1.下载解压 下载:http://www.eclipse.org/downloads/ 解压:SHELL$ sudo tar -zxvf eclipse.tar.gz 2.快捷 ...

  2. Java8新特性 1——利用流和Lambda表达式操作集合

    Java8中可以用简洁的代码来操作集合,比如List,Map,他们的实现ArrayList.以此来实现Java8的充分利用CPU的目标. 流和Lambda表达式都是Java8中的新特性.流可以实现对集 ...

  3. openstack 网络

    物理节点hosts解析配置

  4. free 和 fclose

    想到一个场景,具体代码如下 #include <stdio.h> #include <stdlib.h> int main(int argc, const char *argv ...

  5. Xcode 的正确打开方式——Debugging

    程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode.这篇博客就主要介绍了 Xcode 中几种能够大幅提升代码调试效率的方式. “If debuggi ...

  6. Android实例-Delphi开发蓝牙官方实例解析(XE10+小米2+小米5)

    相关资料:1.http://blog.csdn.net/laorenshen/article/details/411498032.http://www.cnblogs.com/findumars/p/ ...

  7. Redis安装与调试

    Redis安装与调试 Redis安装与调试linux版本:64位CentOS 6.5 Redis版本:2.8.17  (更新到2014年10月31日) Redis官网:http://redis.io/ ...

  8. poj3041

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12162   Accepted: 6620 Descri ...

  9. C#学习笔记(十四):GC机制和弱引用

    垃圾回收(GC) 垃圾回收即Garbage Collector,垃圾指的是内存中已经不会再使用的对象,通过收集释放掉这些对象占用的内存. GC以应用程序的root为基础,遍历应用程序在Heap上动态分 ...

  10. PictureEdit中拖放图片

    public partial class Form2 : Form { string fileName = string.Empty; public Form2() { InitializeCompo ...