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)
 
题解:
可以发现如果第一个为B那么就必须翻转,同样第一个翻转完以后第二个如果为B那么也必须翻转.
而且同一个区间修改两次是无意义的
于是我们可以一直这样处理下去 就可以得到最小答案
但是不能一个一个改,需要优化,F[i]表示i到i+k-1是否被翻转过, 然后不断记录i到i-k+1的和sum,然后判断sum的奇偶就可以判断这个位置为B还是F
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=;
bool a[N],f[N];int n;
int work(int k)
{
memset(f,,sizeof(f));
int tmp=,sum=;
for(int i=;i<=n-k+;i++)
{
if((a[i]+sum)%)
{
tmp++;
f[i]=true;
sum+=f[i];
}
if(i-k+>)sum-=f[i-k+];
}
for(int i=n-k+;i<=n;i++)
{
if((a[i]+sum)%)return -;
if(i-k+>)sum-=f[i-k+];
}
return tmp;
}
int main()
{
int ansk=2e8,ansm=2e8;
char s[];
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%s",s);
if(s[]=='B')a[i]=true;
}
int t;
for(int k=;k<=n;k++)
{
t=work(k);
if(t!=- && t<ansm)ansm=t,ansk=k;
}
printf("%d %d",ansk,ansm);
return ;
}

POJ 3276 Face The Right Way的更多相关文章

  1. 反转(开关问题) POJ 3276

    POJ 3276 题意:n头牛站成线,有朝前有朝后的的,然后每次可以选择大小为k的区间里的牛全部转向,会有一个最小操作m次使得它们全部面朝前方.问:求最小操作m,再此基础上求k. 题解:1.5000头 ...

  2. POJ 3276 (开关问题)

    题目链接: http://poj.org/problem?id=3276 题目大意:有一些牛,头要么朝前要么朝后,现在要求确定一个连续反转牛头的区间K,使得所有牛都朝前,且反转次数m尽可能小. 解题思 ...

  3. poj 3276(反转)

    传送门:Problem 3276 参考资料: [1]:挑战程序设计竞赛 先献上AC代码,题解晚上再补 题意: John有N头牛,这些牛有的头朝前("F"),有的朝后("B ...

  4. POJ 3276 Face The Right Way 反转

    大致题意:有n头牛,有些牛朝正面,有些牛朝背面.现在你能一次性反转k头牛(区间[i,i+k-1]),求使所有的牛都朝前的最小的反转次数,以及此时最小的k值. 首先,区间反转的顺序对结果没有影响,并且, ...

  5. Enum:Face The Right Way(POJ 3276)

    面朝大海,春暖花开 题目大意:农夫有一群牛,牛排成了一排,现在需要把这些牛都面向正确的方向,农夫买了一个机器,一次可以处理k只牛,现在问你怎么处理这些牛才可以使操作数最小? 这道题很有意思,其实这道题 ...

  6. POJ 3276

    Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2193   Accepted: 103 ...

  7. Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276

    Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2899   Accepted: 133 ...

  8. POJ 3276 Face The Right Way 翻转(开关问题)

    题目:Click here 题意:n头牛排成一列,F表示牛面朝前方,B表示面朝后方,每次转向K头连续的牛的朝向,求让所有的牛都能面向前方需要的最少的操作次数M和对应的最小的K. 分析:一个区间反转偶数 ...

  9. POJ 3276 Face The Right Way(前缀和优化)

    题意:有长度为N的01串,有一个操作可以选择连续K个数字取反,求最小的操作数和最小的K使得最后变成全1串.(N<=5000) 由于K是不定的,无法高斯消元. 考虑枚举K,求出最小的操作数. 显然 ...

随机推荐

  1. vue.js下载及安装配置

    环境 Deepin15.4 下载及配置 node下载地址:http://nodejs.cn/download/ 解压到文件夹 /home/maskerk/vue/ 下 设置软连接: $ ln -s / ...

  2. 视频聊天插件:AnyChat使用攻略之iOS开发指南

    AnyChat使用攻略之iOS开发指南 这套攻略主要指导刚开始使用AnyChat SDK For iOS的同学,快速搭建SDK环境,和实现音视频开发流程. (需要工程案例文件可联系我们) 在iOS平台 ...

  3. AWS中,如果使用了ELB,出现outofservice

    平台:亚马逊AWS EC2 出现状况: 我创建了弹性平衡负载,也注册了实例,但是实例的状态一直是outofservice.为什么? 为什么会出现这个问题呢? 1:实例有问题: 2:负载平衡器创建的有问 ...

  4. MySql数据库的常用命令

    1.连接Mysql 连接本地的mysql数据库 :   mysql -u root -p    (回车之后会提示输入密码) 连接远程主机的mysql数据库 : 假设远程主机的IP为:110.110.1 ...

  5. selenium在页面中多个fream的定位

    在做页面元素定位的时候,遇到多fream的页面定位比较困难,需要先去切换到元素所在的fream才能成功定位. 1,切换到目标fream: driver.switch_to.frame('freamID ...

  6. 剑指offer-反转单词顺序列

    题目描述 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,"st ...

  7. hadoop2.6.0实践:003 检查hadoop是否可用

    start-dfs.sh start-yarn.sh 1.检查hdfs hdfs dfs -ls / http://localhost:50070 2.运行例子程序 hdfs dfs -ls / hd ...

  8. maven常见问题处理(3-1)修改maven 默认使用的 jdk 版本

    Eclipse工程应设定了1.8,maven编译仍然使用1.6的解决办法 解决方式有两种,一种是配置 pom.xml,一种是配置 settings.xml. 方式一:settings.xml 配置 打 ...

  9. RxJava系列3(转换操作符)

    RxJava系列1(简介) RxJava系列2(基本概念及使用介绍) RxJava系列3(转换操作符) RxJava系列4(过滤操作符) RxJava系列5(组合操作符) RxJava系列6(从微观角 ...

  10. python/*args和**kwargs

    *args和**kwargs #coding=utf8 __author__ = 'Administrator' # 当函数的参数不确定时,可以使用*args和**kwargs.*args没有key值 ...