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. bzoj 1408 [Noi2002]Robot(欧拉函数)

    [题目链接]  http://www.lydsy.com/JudgeOnline/problem.php?id=1408 [题意] 求m的所有约数中,满足可以分解成(奇数个不同素数/偶数个不同素数/其 ...

  2. bzoj 4006 [JLOI2015]管道连接(斯坦纳树+状压DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4006 [题意] 给定n点m边的图,连接边(u,v)需要花费w,问满足使k个点中同颜色的 ...

  3. mysql注册服务

    http://www.2cto.com/database/201301/185456.html ____________________________________________________ ...

  4. Myeclipse 快捷键设置和自动提示设置

    1.快捷键设置和调整 2.自动提示信息设置与调整

  5. UVALive 7456 Least Crucial Node (并查集)

    Least Crucial Node 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/C Description http://7 ...

  6. POJ 2387 Til the Cows Come Home (最短路 dijkstra)

    Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...

  7. ASP.NET Web Form和MVC中防止F5刷新引起的重复提交问题

    转载 http://www.cnblogs.com/hiteddy/archive/2012/03/29/Prevent_Resubmit_When_Refresh_Reload_In_ASP_NET ...

  8. TypeScript 素描 - 模块

    /* 其实前面一些都是废话,因为都和C#类似.从模块开始就需要深入的去理解了 文档反复声明了 内部模块现在称做 命令空间 外部模块称为 模块 模块在其自身的作用域里执行,而不是在全局作用域里,也就是说 ...

  9. word2010 ctrl v not work

    终于解决了word 2010中ctrl v 不能用的问题. 0 word ctrl c 可以用,右键粘贴可以正常使用,快捷键ctrl v不能用. 1 在excel中ctrl c 和ctrl v,可以正 ...

  10. 如何彻底隐藏iOS7应用的status bar

    用xcode5开发新的iOS游戏,发现一个坑爹的现象,虽然我已经在info.plist里面把Status bar is initially hidden设置成了YES,但在设备上一跑还是看到丑陋的st ...