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头奶牛排成一排,有的朝前有的朝后,现在你可以使k(每次翻转必须是k头)头奶牛一次性翻转朝向(n>=k>=1),问你最少的翻转次数和此时对应的k值。
思路:
  首先可以依次枚举区间长度len,在不同的区间长度的情况下,查看这样的长度是否能使奶牛全部朝前,并记录总翻转次数,取最小。
  进而在每种区间长度len的讨论中分析如下:
  对于目前的奶牛,我们的决策就两种,翻或不翻,这要看他在目前朝前还是朝后(对于判断这个我们可以去分析他本身的朝向和已翻过的次数,翻过奇数次则与初始相反,翻过偶数次相当于没翻,与初始相同),而且将一个区间的点翻转时不会影响到区间起点前面的点(无后效性),所以我们可以循环枚举区间[1,1+len-1]~[n-len+1,n],每次使左端点加1,同时要记录所有奶牛总翻转次数。
  决策在循环中,但这个循环结束并不一定所有的奶牛都能朝前,因为我们只能处理到距离最后一个奶牛len-1位置的奶牛(要翻转就只能翻转一个区间),所以如果循环结束但在最后几个未处理到的奶牛中有朝后的,则这个len不能使所有奶牛朝前(当然len=1时是一定可以的),进行下一个len(=len+1)的讨论。
  对于每种区间长度讨论出来的总翻转次数(如果最后都能朝前的话),取最小值。
  最终输出最小值以及它对应的区间长度len。
         嗯。。。。。大体上就是这样
  接下来是在每个决策中判断这个牛目前朝向时,如何查看记录过的这个牛翻过的次数。
  我们可以定义一个变量去记录当前处理的点的反转次数,如果他脱离一个区间,就减去他所脱离这个区间起点翻转的次数,也就是减1或者减0(因为一个区间我们只翻转1次或0次,多了没什么意义),这样这个变量就可以存储目前点翻转的次数了。
代码:
 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=5e3+;
int n;
char fac;
bool face[maxn],f[maxn];//face: 0->qian 1->hou f:0->don't need turn ,1->need
int Fan(int len){
memset(f,,sizeof(f));
int cishu=,sum=;//sum->已经turn的次数 cishu->turn的总次数
for(int i=;i+len-<=n;++i){
if((face[i]+sum)%==){//i朝后
cishu++;
f[i]=;
}
sum+=f[i];
if(i-len+>=)sum-=f[i-len+];//现在 sum 是下一个i已经turn的次数了
}
//因为最后一个i是离最后一头牛len-1长度,检查未处理过的牛是否朝后,if this,无解
for(int i=n-len++;i<=n;++i){
if((face[i]+sum)%==)return -;
if(i-len+>=)sum-=f[i-len+];
}
return cishu;
}
void Solve(){
int K=n,cishu=n;
for(int len=;len<=n;++len){//枚举区间长度
int m=Fan(len);
if(m>=&&cishu>m){
cishu=m;K=len;
}
}
printf("%d %d\n",K,cishu);
return;
}
int main(){
// freopen("1.in","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;++i){
scanf(" %c",&fac);
if(fac=='B')face[i]=;
}
Solve();
return ;
}

Face The Right Way POJ - 3276(区间)的更多相关文章

  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 2955 (区间DP)

    题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...

  4. POJ 1651 (区间DP)

    题目链接: http://poj.org/problem?id=1651 题目大意:加分取牌.如果一张牌左右有牌则可以取出,分数为左牌*中牌*右牌.这样最后肯定还剩2张牌.求一个取牌顺序,使得加分最少 ...

  5. POJ 3468 区间更新,区间求和(经典)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 72265   ...

  6. POJ 3264 区间最大最小值Sparse_Table算法

    题目链接:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total ...

  7. poj 3276(反转)

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

  8. poj 3485 区间选点

    题目链接:http://poj.org/problem?id=3485 题意:X轴上公路从0到L,X轴上下有一些点给出坐标代表村庄,问在公路上最少建几个出口才能使每个村庄到出口的距离不超过D. 以村庄 ...

  9. POJ 2104 区间第k大(主席树)

    题目链接:http://poj.org/problem?id=2104 题目大意:给定还有n个数的序列,m个操作,每个操作含有l,r,k,求区间[l,r]第k大 解题思路:线段树只能维护序列的最大值最 ...

随机推荐

  1. startUML5.0中的tools下怎么没有java、c等选项

    这也是帮一个直系学妹弄得,哈哈~~~ 具体做法如下: 进入到StartUML\modules目录下,里面有很多文件夹,比如startuml-cpp.startuml-csharp等等, 进入到每个文件 ...

  2. 如何为SpringBoot服务添加HTTPS证书

    HTTPS是HTTP的安全版本,旨在提供数据传输层安全性(TLS).当你的应用不使用HTTP协议的时候,浏览器地址栏就会出现一个不安全的提示.HTTPS加密每个数据包以安全方式进行传输,并保护敏感数据 ...

  3. freecplus框架,Linux平台下C/C++程序员提高开发效率的利器

    目录 一.freecplus框架简介 二.freecplus开源许可协议 三.freecplus框架内容 字符串操作 2.xml解析 3.日期时间 4.目录操作 5.文件操作 6.日志文件 7.参数文 ...

  4. 扩展el-input 加一个控制精度的命令(directives)

    使用el-input时 出现需要精度控制的需求,因为每一个el-input需要精度不用 所以我给这个虚拟dom绑定了一个精度值,根据这个值去控制精度. 下面附上代码: html: <el-inp ...

  5. mongo的聚合操作

    对图7-1所示的数据集exampledata1,使用聚合操作实现以下功能: (1)不返回_id字段,只返回age和sex字段. (2)所有age大于28的记录,只返回age和sex. (3)在$mat ...

  6. Java 基础(四):数组

    数组,一种应用非常广泛的数据结构,简单地来说就是一组类型相同且无序的元素的存储在固定长度且有序的内存空间. 创建一个数组 在Java中,我们可以通过[]去声明一个指定类型的数组 int[] a; // ...

  7. 计算机网络 Computer Networks​ 期末复习总提纲

    平时不学习,期末火葬场. 一周时间靠王道考研和各路 pdf 自学计网,留下的提纲都在这里了.全是干货.全文 pdf 可以在这里下载:http://cloud.billc.io/s/xNHarppQPG ...

  8. stm32的hall库新建模板编译错误: #error "Please select first the target STM32F1xx device used in your application (in stm32f1xx.h file)"的处理

    在stm32f1xx.h file文件中找到如下代码: /* Uncomment the line below according to the target STM32L device used i ...

  9. Uncovering thousands of new peptides with sequence-mask-search hybrid de novo peptide sequencing framework (使用序列掩码搜索结合肽段从头测序框架发现了数千个新肽段)-解读人:刘佳维

    期刊名:Molecular & Cellular Proteomics 发表时间:(2019年12月) IF:4.828 单位: 朱拉隆功大学 费城威斯塔研究所 物种:人 技术:de novo ...

  10. hdu1686kmp果题

    kmp字符串匹配原理参考博客:https://blog.csdn.net/bqw18744018044/article/details/90516750 代码如下:(写一遍模板) #include&l ...