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. HDU-4747 Mex 线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意:求一个数列中,所有mex(L,R)的和. 注意到mex是单调不降的,那么首先预处理出mex ...

  2. 现代程序设计 homework-08

    现代程序设计 homework-08 第八次作业. 理解C++变量的作用域和生命周期 作用域就是一个变量可以被引用的范围,如:全局作用域.文件作用域.局部作用域:而生命周期就是这个变量可以被引用的时间 ...

  3. Hibernate关联关系之双向1—n

    •双向 1-n 与双向 n-1 是完全相同的两种情形 •双向 1-n 需要在1的一端可以访问n的一端,反之依然. 测试实例代码: 实体类: package com.elgin.hibernate.nt ...

  4. crontab 获取本机ip

    写了个shell获取ip的函数,如下 function GetLocalIP() { ifconfig | grep 'inet '| grep -v '127.0.0.1' | cut -d: -f ...

  5. Spring Auto-Wiring Beans with @Autowired annotation

    In last Spring auto-wiring in XML example, it will autowired the matched property of any bean in cur ...

  6. inline(内联函数)

    一般来说,调用函数会造成:目前的指令位置被存储下来,程序流跳转到所调用的函数,然后执行调用函数,最后跳转回之前存储的位置.对于需要常常调用的小函数来说,这会影响程序的运行效率.所以,c99新增了inl ...

  7. HDU 3664 Permutation Counting (DP)

    题意:给一个 n,求在 n 的所有排列中,恰好有 k 个数a[i] > i 的个数. 析:很明显是DP,搞了好久才搞出来,觉得自己DP,实在是太low了,思路是这样的. dp[i][j]表示 i ...

  8. Android应用开发学习之相对布局

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 相对布局RelativeLayout是指按照组件之间的相对位置进行布局,如一个组件在另一个组件的左边.右边.上边或下 ...

  9. 解决iPhone上select时常失去焦点,随意跳到下一个输入框,影响用户操作

    window.addEventListener('load', function() { FastClick.attach(document.body); }, false); //300s延迟,解决 ...

  10. MATLAB remove outliers.

    Answer by Richard Willey on 9 Jan 2012 Hi Michael MATLAB doesn't provide a specific function to remo ...