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 ≤ KN)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)
 

题目大概意思就是说:n头牛,两个方向,向后B,向前F,每次转K头牛,最少转cnt次,

最终使所有的牛向前。求最少的K以及对应的cnt。

 
代码如下:
#include <stdio.h>
#include <string.h> #define maxn 5010 int cow[maxn], N, K, M; // 奶牛们的初始位置,0前1后
int tra[maxn]; // 是否翻转第i头牛,1翻 int cal(int k) // 翻转顺序从左往右
{
int ans = 0, i, sum = 0;
for(i = 0; i + k <= N; ++i)
{
tra[i] = 0;
if((sum + cow[i]) & 1)
{
tra[i] = 1;
++ans;
}
sum += tra[i]; // 尺取法
if(i - k + 1 >= 0)
sum -= tra[i-k+1];
}
// 检查剩下的是否有反向奶牛
for( ; i < N; ++i)
if((sum + cow[i]) & 1)
return -1;
else if(i - k + 1 >= 0)
sum -= tra[i-k+1];
return ans;
} int main()
{
char ch[2];
int i, m, k;
scanf("%d", &N);
for(i = 0; i < N; ++i)
{
scanf("%s", ch);
if(ch[0] == 'B') cow[i] = 1;
else cow[i] = 0;
}
M = N;
K = 1;
for(k = 1; k <= N; ++k)
{
m = cal(k);
if(m >= 0 && m < M)
{
M = m;
K = k;
}
}
printf("%d %d\n", K, M);
return 0;
}

ACM题目————Face The Right Way的更多相关文章

  1. ACM题目————中缀表达式转后缀

    题目描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说)操作符在两个操作数中间:num1 operand num2.同理,后缀表达式就是操作符在两 ...

  2. HDU ACM 题目分类

    模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...

  3. ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]

    原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...

  4. 有一种acm题目叫做,奇葩!

    本文全然没有技术含量,纯粹是娱乐. 我事实上想写点东西.可是近期好像做计算几何做得太多了,一种想说说不出东西的感觉,唯有写一下一些奇葩的题目了. HDU3337:Guess the number pi ...

  5. ACM题目————STL练习之求次数

    题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1112 描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个 ...

  6. ACM题目————zoj问题

    题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20322 解决:3560 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. ...

  7. ACM题目————又见拦截导弹

    描述 大家对拦截导弹那个题目应该比较熟悉了,我再叙述一下题意:某国为了防御敌国的导弹袭击,新研制出来一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:它的第一发炮弹能够到达任意的高度,但是以后每一发炮 ...

  8. ACM题目————还是畅通工程

    Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路 ...

  9. ACM题目————小A的计算器

    Description 以往的操作系统内部的数据表示都是二进制方式,小A新写了一个操作系统,系统内部的数据表示为26进制,其中0-25分别由a-z表示.  现在小A要在这个操作系统上实现一个计算器,这 ...

  10. ACM题目————二叉树的遍历

    一.二叉树的后序遍历: 题目描述 给定一颗二叉树,要求输出二叉树的深度以及后序遍历二叉树得到的序列.本题假设二叉树的结点数不超过1000 输入 输 入数据分为多组,第一行是测试数据的组数n,下面的n行 ...

随机推荐

  1. python中filter(),map()和reduce()的用法及区别

    先看filter()方法 print(list(filter(lambda n : n % 2 == 1, range(20))))# 结果 [1, 3, 5, 7, 9, 11, 13, 15, 1 ...

  2. Hibernate框架 主配置文件 Hibernate.cfg.xml 映射配置 说明

    1 主配置文件 Hibernate.cfg.xml 主配置文件中主要配置:数据库连接信息.其他参数.映射信息! 常用配置查看源码: hibernate-distribution-3.6.0.Final ...

  3. ELK之使用filebeat的多行过滤插件把多行合并成一行

    java运行日志一般有多行,格式类似如下 格式为:日期 + 日志级别 + 日志信息 有些日志是多行的,需要使用filebeat多行插件把多行合并成一行 未使用多行插件的日志格式 修改filebeat配 ...

  4. informix数据库知识积累

    一.嵌套查询 informix子查询:嵌套查询(1)select first 20 * from (select first 40 * from hlrquery_log order by id de ...

  5. linux:正则表达式grep命令

    基本语法一个正则表达式通常被称为一个模式(pattern),为用来描述或者匹配一系列符合某个句法规则的字符串. 一.选择:| |   竖直分隔符表示选择,例如"boy|girl"可 ...

  6. Net Promoter Score

    https://baike.baidu.com/item/净推荐值/3783368?fr=aladdin NPS(Net Promoter Score),净推荐值,又称净促进者得分,亦可称口碑,是一种 ...

  7. Java Native Interface 基于JNI的嵌入式手机软件开发实例

    1.通过JNI和c/c++的库组件.其他代码交互 2.java和c不能互通的原因时数据类型问题 Introduction https://docs.oracle.com/javase/8/docs/t ...

  8. php.exe

    PhpStorm 10.0.2 php  interpreter  填入php.exe 暂且不用填写 Listen 63342

  9. 解决重启服务器以后Nginx无法启动

    今天重启服务器以后发现nginx服务器启动失败. 这应该是因为把nginx进程杀死后pid丢失了,下一次再开启nginx -s reload时无法启动,重装可以解决这个问题,但是太麻烦了. 然后这样就 ...

  10. crawlspider爬虫:定义url规则

    spider爬虫,适合meta传参的爬虫(列表页,详情页都有数据要爬取的时候) crawlspider爬虫,适合不用meta传参的爬虫 scrapy genspider -t crawl it it. ...