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. Log4j详细教程

    一.入门实例 1.新建一个JAva工程,导入包log4j-1.2.17.jar,整个工程最终目录如下 2.src同级创建并设置log4j.properties ### 设置### log4j.root ...

  2. 为微软samples-for-ai贡献代码是种怎么样的体验?

    推送原文链接:传送门 关注SomedayWill,了解为微软项目贡献代码的始终. 还记得微软神器samples-for-ai吗?它可不仅仅可以用来安装框架,它其实是个开源的AI样例库,以Visual ...

  3. nyoj 聪明的kk

    聪明的kk 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 聪明的"KK"非洲某国展馆的设计灵感源于富有传奇色彩的沙漠中陡然起伏的沙丘,体现出本国 ...

  4. JAVA_SE基础——55.自定义异常类

    在Java中已经提供了大量的异常类,但是这些异常类有时野很难满足开发者的要求,所以用户可以根据自己的需要来定义自己的异常类.但自定义的异常类必须继承自Exception或其子类. 可以自定义出的问题称 ...

  5. js判断IE浏览器版本(IE8及以下)

    var DEFAULT_VERSION = 8.0; var ua = navigator.userAgent.toLowerCase(); var isIE = ua.indexOf("m ...

  6. Python内置函数(13)——bytearray

    英文文档: class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cla ...

  7. java程序员最不愿意看到的十件事

     0.遍历结果集并构造对象如果你是个时髦的开发者而不是专业人员,显然你从某篇博客中读过有开发者遇到Hibernate的“性能问题”,因而认为ORM都不好,觉得手动编码“明显更好”.喜欢的话你当然可以用 ...

  8. 新概念英语(1-107)It's Too Small.

    Lesson 107 It's too small. 太小了. Listen to the tape then answer this question. What kind of dress doe ...

  9. Spring知识点回顾(03)Bean的 Scope

    sigleton prototype request session globalsession stepscope

  10. java stream 原理

    java stream 原理 需求 从"Apple" "Bug" "ABC" "Dog"中选出以A开头的名字,然后从中选 ...