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)

首先枚举k,对于一个特定的k,最左端只有一个区间能够影响得到,所以可以确定是否反转。然后这个端点就不会再被影响,这样长度就减1了,重复以上步骤就能得出答案。还运用到了一个小技巧,用f[i]来记录i是否被反转,这样就不需要真正的实现反转,时间复杂度降低。

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <limits>
#include <new>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;int n;
int dir[5010];//牛的方向(0:F, 1:B)
int f[5010];//区间[i, i+k-1]是否被反转int calc(int k)
{memset(f, 0, sizeof(f));int res = 0, sum = 0;for (int i = 0; i + k <= n; ++i){if ((dir[i]+sum)&1){res++;f[i] = 1;}sum += f[i];if (i - k + 1 >= 0)sum -= f[i-k+1];}for (int i = n-k+1; i < n; ++i){if ((dir[i]+sum)&1)return -1;if (i - k + 1 >= 0)sum -= f[i-k+1];}return res;
}int main()
{cin >> n;char s[5];for (int i = 0; i < n; ++i){scanf("%s", s);dir[i] = (s[0] == 'B');}int K = 1, M = n;for (int k = 1; k <= n; ++k){int m = calc(k);if (m >= 0 && m < M){M = m;K = k;}}cout << K << ' ' << M << endl;return 0;
}

poj3276(Face The Right Way)反转(开关问题)相关推荐

  1. 机电传动控制大作业 第一阶段

    机电传动控制大作业 第一阶段 一.系统硬件接口定义 1.电梯内操作界面: 标有1-7数字的按钮(每个按钮有一个LED灯,按下按钮灯即亮),开门,关门以及紧急报警的按钮和楼层指示的LED数码管.电梯门的 ...

  2. 计算机显示屏幕会对人体产生,一种自动除尘功能的计算机显示屏专利_专利申请于2018-07-13_专利查询 - 天眼查...

    1. 一种自动除尘功能的计算机显示屏,其特征在于:包括边框(31),所述边框(31)内固 定安装有液晶屏(30),所述边框(31)四角处设有对称的两个第一固定块(1)和第二固定块 (16),所述第一固 ...

  3. ZBrush: Alpha纹理生成雕花

    ZBrush2020 步骤 准备 Alpha纹理,Alpha纹理图只有黑白两色,灰度值代表着厚度系数(0~1),这就意味着纯黑部值为0,厚度也为0即镂空部分,所以当你发现该镂空的地方没镂空,说明不够黑 ...

  4. 基于物联网的环境调节系统(ESP32-C3+Onenet+微信小程序)

    这个是我最近做的一个课程设计,实时监测室内的温湿度和光照强度,在微信小程序上实时显示监测数据并下发命令控制风扇开关和舵机正反转(从而实现模拟窗帘的开关).有两种模式控制,一个是手动控制,通过微信小程序 ...

  5. 基于(plc)portal v14的三级传送带设计

    目录 大作业任务书 一.项目概述 1.1 项目任务 1.2 项目总体框图 1.3 项目方案 1.3.1 继电器控制 1.3.2单片机控制 1.3.3可编程逻辑器控制 二.硬件设计 2.1 硬件选型 2 ...

  6. 学习笔记(八):FIREWALL配置与使用

    文章目录 一.FIREWALL概念 1.防火墙原理 (1)四表 (2)五链 2.iptables的一些常用命令 3.firewall的优势 4.firewall的原理 二.FIREWALL服务的配置和 ...

  7. 200Smart模拟量变频器控制应用

    200Smart模拟量变频器控制应用 一.变频器控制 1.开关量多段速 2.模拟量控制 PLC输出0-10V或者0-20mA控制变频器的频率为0-50HZ 给PLC0-27648,PLC会输出标准模拟 ...

  8. 仿货拉拉的物流货运平台开发 物流货运平台源码开发

    开创性的物流货运管理云系统,将货运环节中的制造商.承运商.司机和收货方链接在同一平台,轻松管理运输.可通过浏览器.移动APP.微信多种渠道登录,并由API与各个系统打通,真正链接! 1. 实时卫星监控 ...

  9. 【部分博客已搬家至博客园】对CSDN、博客园和简书的一点比较

    有强迫症,就把部分博客搬家到博客园养老了.      我觉得CSDN和博客园各有各的好吧. 我个人写博客方式的历程: 一开始是直接在CSDN上的富文本编辑器上写,界面简洁大方,可插入数学公式,插入的代 ...

  10. codetop做题笔记

    ##ACM模式 头文件: #include<bits/stdc++.h> #include<iostream> using namespace std; ##206. 反转链表 ...

最新文章

  1. [SIGMOD 10] Pregel 基于BSP的大规模图处理系统 学习总结
  2. 用JQ去实现一个轮播效果
  3. 汇编——NT中读取MBR内容
  4. oracle查询用户权限和角色_详解jenkins配置用户角色权限的实现方法
  5. 【C语言】Pillow(数据溢出问题)
  6. c oracle 存储图片,用c语言如何读取和保存jpg图片文件?
  7. namespace用法
  8. 开课吧:分享一下C语言发展史的点点滴滴
  9. Seq2Seq中的Attention
  10. element-ui的tree配合原生
  11. ubuntu安装Google输入法
  12. antd修改Tooltip背景色
  13. chm文件显示“已取消网页导航”的解决办法
  14. 综合应用_6多字符串-烟锁池塘柳
  15. 【35】存储器层次结构全景:数据存储的大金字塔长什么样?
  16. win10无法连接到win7系统更新服务器,win10系统提示无法连接到windows服务的解决方法...
  17. shell获取当前系统时间
  18. 作文提升~老师整理的优美比喻句太实用
  19. Python学习,元类type 反射 函数与方法 双下方法
  20. selenium3.141 +IE浏览器环境搭建(含驱动下载链接)

热门文章

  1. ViewController的view的创建
  2. Mybatis 一对多
  3. PowerShell中远程管理简单配置
  4. Html和Css学习笔记-html进阶-html5属性
  5. 2017-12-3 Crontab(字符串处理)
  6. MyEclipse使用过程中的问题及对应设置的总结
  7. (5)STM32使用HAL库实现串口通讯——实战操作
  8. 三种方法打印 main函数的返回地址的值(old EIP)(用途,你懂得!)
  9. 【转】艺术设计、数字媒体、环艺、影视动画、摄影、广编专业…等…视频、教程、资讯、图库、作品汇总大全...
  10. DIV+CSS常见问题的14条原因分享