Face The Right Way
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5278   Accepted: 2462

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)

Source

 
题目大意:有N头奶牛有些朝前有些朝后。每次可以选择一段长度为K的区间翻转,问最少的翻转次数以及对应的K。
试题分析:如何翻转?对于这个序列我们设一个f数组,当这一位与上一位不一样那么标为1,否则标为0.
     这时就有了一个很好的性质:我们翻转后中间不会变,变的只是翻转的头部和末尾+1.
     那么我们只要遇到不一样的就进行一次这样的翻转,那么N-K+2~N这一段如果有不一样的那么就没有办法了,说明我们现在枚举的K不合法。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
#define LL long long
const int MAXN=10000;
const int INF=999999;
char last='F';
int N; int ansm=INF,ansk;
int f[5001],f2[5001];
int main(){
N=read();
char c;
for(int i=1;i<=N;i++){
cin>>c;
if(c!=last) f[i]=1;
last=c;
}
for(int k=1;k<=N;k++){
int cnt=0;
for(int i=1;i<=N;i++) f2[i]=f[i];
for(int i=1;i<=N-k+1;i++)
if(f2[i]){f2[i+k]^=1;cnt++;}
for(int i=N-k+2;i<=N;i++)
if(f2[i]) {cnt=INF;break;}
if(cnt==INF) continue;
if(cnt<ansm){
ansm=cnt;
ansk=k;
}
}
printf("%d %d\n",ansk,ansm);
}

  

最新文章

  1. mybaties # , $
  2. 单页Web应用:
  3. iOS - Xcode 常用快捷键
  4. uva 10692 Huge Mods 超大数取模
  5. lintcode:快乐数
  6. c++实现输入法窗口自定义的代码
  7. 如何让Vim显示dos下的^M符号
  8. InstallShield:自己备份
  9. VS2012 快捷键 VS Resharper 设置
  10. 常用的一些js和css
  11. C++11 中值得关注的几大变化(网摘)
  12. 【Nginx系列】Nginx虚拟主机的配置核日志管理
  13. 在web应用中使用Log4j 2
  14. 优先使用TimeUnit类中的sleep()
  15. C#嵌入子窗体,判断子窗体是否打开了
  16. docker镜像加速器
  17. JobService相关
  18. 如何利用FPGA进行时序分析设计
  19. CentOS7 安装redis 并且设置成服务自动启动
  20. JavaScript 里面的整数 位 操作

热门文章

  1. Windows Phone 8.1基础教程(1) 页面导航、弹出框
  2. chrome最小字体12px如何修改
  3. 6.0docker Dockerfile文件
  4. perl6中字符串字母编历
  5. linux加载指定目录的so文件
  6. 【LOJ6201】【bzoj4939】【YNOI2016】掉进兔子洞
  7. 【UOJ#169】元旦老人与数列
  8. python初学-元组、集合
  9. 15:django 缓存架构
  10. HDU 2829 Lawrence(四边形优化DP O(n^2))