您的位置:首页 > 产品设计 > UI/UE

codeforces D. Queue 找规律+递推

2016-07-29 22:00 316 查看

题目链接:

http://codeforces.com/problemset/problem/353/D?mobile=true

H. Queue

time limit per test 1 secondmemory limit per test 256 megabytes

问题描述


There are n schoolchildren, boys and girls, lined up in the school canteen in front of the bun stall. The buns aren't ready yet and the line is undergoing some changes.

Each second all boys that stand right in front of girls, simultaneously swap places with the girls (so that the girls could go closer to the beginning of the line). In other words, if at some time the i-th position has a boy and the (i + 1)-th position has a girl, then in a second, the i-th position will have a girl and the (i + 1)-th one will have a boy.

Let's take an example of a line of four people: a boy, a boy, a girl, a girl (from the beginning to the end of the line). Next second the line will look like that: a boy, a girl, a boy, a girl. Next second it will be a girl, a boy, a girl, a boy. Next second it will be a girl, a girl, a boy, a boy. The line won't change any more.

Your task is: given the arrangement of the children in the line to determine the time needed to move all girls in front of boys (in the example above it takes 3 seconds). Baking buns takes a lot of time, so no one leaves the line until the line stops changing.


输入


The first line contains a sequence of letters without spaces s1s2... sn (1 ≤ n ≤ 106), consisting of capital English letters M and F. If letter si equals M, that means that initially, the line had a boy on the i-th position. If letter si equals F, then initially the line had a girl on the i-th position.


输出


Print a single integer — the number of seconds needed to move all the girls in the line in front of the boys. If the line has only boys or only girls, print 0.


样例


sample input

MMFF

sample output

3


题意


有n个同学在排队买包子,每秒钟所有的排在左边的男生会与他相邻的排在他右边的女生交换位置,问多少秒钟之后所有的女生会在所有的男生的左边。


题解


我们每次观察两个相邻的女生,右边的女生会有两种情况:一种是撞上左边的女生了,那她最后到达指定位置的时间一定是左边的女生到达时间+1;另一种是她很顺利,一直都畅通无阻,那她到达目的地的时间就是左边的男生数了。分析到这里,我们发现,只要知道左边的女生到达目的地的时间,我们是可以推出右边女生的时间的:T右=max(T左+1,左边男生数)。 而最左边女生到达的时间是可以确定的,所以,我们只要从左到右递推一遍就可以得到答案。


代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M (l+(r-l)/2)
using namespace std;

const int maxn = 1e6 + 10;

char str[maxn];
int sumv[maxn];

int main() {
int a = 0, b = 0;
scanf("%s", str);
int n = strlen(str);
int p = 0,cntm=0,ans=0;
while (p < n&&str[p] == 'F') p++;
for (int i = p; i < n; i++) {
if (str[i] == 'M') {
cntm++;
}
else {
ans = max(ans + 1, cntm);
}
}
printf("%d\n", ans);
return 0;
}

Notes


对于这种找规律的题目,如果总体的变化掌控不了,我们可以考虑局部的情况,特殊极端的情况。比如这道题就可以从只考虑两个相邻的女生的角度切入,简化问题,找到递推的规律。其实我们通过观察可以发现,影响一个女生的时间的因素最直接的就是左边第一个‘相邻’的女生了,而且对于所有的女生这点都成立,也就体现出了递推的性质。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: