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

Example input

7

B

B

F

B

F

B

B

Example output

3 3

译成人话:

有n头奶牛排成一排,有的朝前有的朝后,现在你可以使k(每次翻转必须是k头)头奶牛一次性翻转朝向(n>=k>=1),问你最少的翻转次数和此时对应的k值。

题目要求我们找到最优的k值,也就是区间长度,我们可以从小到大枚举这个k,这稍微有点区间DP的味道,但并不是一码事,对于反转来讲,如果反转的次数为奇数,那么这个区间的牛算是被扭了,但如果次数为偶数,就相当于没动,我们可以依此对1~n-len+1(len就是k)进行遍历,我们每枚举一个点,这个点就可以看成一个len区间的最左端,如果这个牛是反的,我们就把后面的区间都扭一下

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=5000+10;
int n,f[maxn];//若f[i]为1,则说明从i到i+len-1都被扭了
char cow[maxn];
int solve(int len){
memset(f,0,sizeof(f));
int ans=0;//记录反转的次数
int sum=0;//记录区间中牛被反转的次数
for(int i=1;i<=n-len+1;i++){
if(sum%2==0&&cow[i]=='B')//因为反转次数是偶数,所以牛都相当于没变,为了把i扭过来,次数要加1
f[i]=1,ans++;
else if(sum%2==1&&cow[i]=='F')//因为反转次数是奇数,而且牛i朝前
f[i]=1,ans++;
sum+=f[i];
if(i-len+1>=1) sum-=f[i-len+1];//当一个len区间被扭完,为了避免后面不该标记的被标记,把前面的减去
}
for(int i=n-len+2;i<=n;i++){
if(sum%2==0&&cow[i]=='B') return -1;//不满足则返回-1
else if(sum%2==1&&cow[i]=='F') return -1;
sum+=f[i];
if(i-len+1>=1) sum-=f[i-len+1];
}
return ans;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)
scanf(" %c",&cow[i]);
int k=0x3f3f3f3f,minn=0x3f3f3f3f;
for(int i=1;i<=n;i++){
int p=solve(i);
if(p>=0&&p<minn){
minn=p,k=i;
}
}
cout<<k<<" "<<minn<<endl;
return 0;
}

Face the right way(反转问题,思维题)的更多相关文章

  1. UVA.679 Dropping Balls (二叉树 思维题)

    UVA.679 Dropping Balls (二叉树 思维题) 题意分析 给出深度为D的完全二叉树,按照以下规则,求第I个小球下落在那个叶子节点. 1. 默认所有节点的开关均处于关闭状态. 2. 若 ...

  2. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  3. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  4. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  5. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)

    思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...

  6. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  8. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  9. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

  10. cf796c 树形,思维题

    一开始以为是个树形dp,特地去学了..结果是个思维题 /* 树结构,设最大点权值为Max,则答案必在在区间[Max,Max+2] 证明ans <= Max+2 任取一个点作为根节点,那么去掉这个 ...

随机推荐

  1. PAT 在霍格沃茨找零钱

    如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易.”现在,给定 ...

  2. mac下使用VMVARE安装win10虚拟机的一些坑

    最近Mac上安装windows踩到了几个坑: 坑一:启动虚拟机后,提示找不到CD-ROM中找不到对应的ISO文件 硬盘格式请选择 在虚拟机->设置中选择启动磁盘为CD_ROM,然后重新启动. 坑 ...

  3. BigDecimal的setScale常用方法(ROUND_UP、ROUND_DOWN、ROUND_HALF_UP、ROUND_HALF_DOWN)

    BigDecimal的setScale四大常用方法总结 // 设置小数点后第三位数字一大一小观察效果BigDecimal num = new BigDecimal("3.3235667&qu ...

  4. 洛谷P1012 拼数 【题解】

    **原题链接** 题目描述 设有n个正整数(n ≤ 20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213 又如:n=4时 ...

  5. const修饰this指针的用法

    #include <iostream> #include <string> using namespace std; class Base { }; class Excepti ...

  6. 2个线程A-B-A-B或者B-A-B-A循环输出

    代码: /** * 两个线程循环打印输出a-b-a-b */ public class AandBforTOthread { private static Object o = new Object( ...

  7. 附020.Nginx-ingress部署及使用

    一 手动部署-官网版 1.1 获取资源 [root@master01 ~]# mkdir ingress [root@master01 ~]# cd ingress/ [root@master01 i ...

  8. CENTOS的备份和恢复

    CENTOS的备份和恢复其实非常简单,我们只要把全部文件用TAR打包就行,下次需要恢复的适合再解压开覆盖就可以了 下面详解CENTOS备份和还原的过程 tar打包命令的特点:1.保留权限2.适合备份整 ...

  9. java中工厂模式

    最近在项目中使用了工厂模式来重构下之前的代码,在这里做个小结. 工厂模式最主要的特点是每次新增一个产品的时候,都需要新增一个新的工厂,这样在对于新的产品做扩展的时候,减少对客户端代码的修改. 我在项目 ...

  10. Windows程序设计(2) - API-02 文件系统

    一.磁盘分区的基本概念 1.磁盘分区(Patitions): 分区就是物理存储设备分割成多个不同的逻辑上的存储设备.分区从实质上说就是对硬盘的一种格式化.当我们创建分区时,就已经设置好了硬盘的各项物理 ...