codeforces D. Queue 找规律+递推
题目链接:
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
对于这种找规律的题目,如果总体的变化掌控不了,我们可以考虑局部的情况,特殊极端的情况。比如这道题就可以从只考虑两个相邻的女生的角度切入,简化问题,找到递推的规律。其实我们通过观察可以发现,影响一个女生的时间的因素最直接的就是左边第一个‘相邻’的女生了,而且对于所有的女生这点都成立,也就体现出了递推的性质。
codeforces D. Queue 找规律+递推的更多相关文章
- BZOJ1002:[FJOI2007]轮状病毒(找规律,递推)
Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子 和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下 ...
- HDU-2045 不容易系列之(3)—— LELE的RPG难题 找规律&递推
题目链接:https://cn.vjudge.net/problem/HDU-2045 找规律 代码 #include <cstdio> long long num[51][2]; int ...
- 2018南京区域赛G题 Pyramid——找规律&&递推
先手动推出前10项,再上BM板子求出递推式 $A_n = 5A_{n-1} - 10A_{n-2} + 10A_{n-3} - 5A_{n-4} + A_{n-5}$,根据特征根理论可求出特征方程 $ ...
- F(k)<(维护+枚举)\(找规律+递推+枚举)>
题意 小明有一个不降序列(f(1),f(2),f(3),--),f(k)代表在这个序列中大小是k的有f(k)个.我们规定f(n)的前12项如下图. n 1 2 3 4 5 6 7 8 9 10 11 ...
- HDU-2050 折线分割平面 找规律&递推
题目链接:https://cn.vjudge.net/problem/HDU-2050 题意 算了吧,中文题不解释了 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线 ...
- codeforces 735C Tennis Championship(贪心+递推)
Tennis Championship 题目链接:http://codeforces.com/problemset/problem/735/C ——每天在线,欢迎留言谈论. 题目大意: 给你一个 n ...
- ACM-ICPC 2018 徐州赛区网络预赛 A.Hard to prepare 【规律递推】
任意门:https://nanti.jisuanke.com/t/31453 A.Hard to prepare After Incident, a feast is usually held in ...
- POJ 2229 sumset ( 完全背包 || 规律递推DP )
题意 : 给出一个数 n ,问如果使用 2 的幂的和来组成这个数 n 有多少种不同的方案? 分析 : 完全背包解法 将问题抽象==>有重量分别为 2^0.2^1.2^2…2^k 的物品且每种物 ...
- NBUT比赛 方格规律递推题
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26901#problem/A 题意:有一个 2*n的格子里,你可以选择任意一个格 ...
随机推荐
- sender是什么意思C#
/// <summary> /// sender就是事件发起者,e存储事件发起者的一些参数 /// 例如: /// private void button1_Click(object se ...
- C#中判断文件夹中存在某个txt文本
strFileName="D:\\strarray.txt"; if (File.Exists(strFileName))//判断文件是否存在 { }
- webview 中 svg的坑
在这里不会详细介绍如何绘制svg图片,是讲一个很小的bug,看图 在这张图中,上面带有纹理和弧度的图片,原本是直接切了一张png的图片,但是由于是在app的登录注册的首页,那么这个35k的图片就会非常 ...
- 一款jQuery实现重力弹动模拟效果特效,弹弹弹,弹走IE6
一款jQuery实现重力弹动模拟效果特效 鼠标经过两块黑色div中间的红色线时,下方的黑快会突然掉落, 并在掉落地上那一刻出现了弹跳的jquery特效效果,非常不错,还兼容所有的浏览器, 适用浏览器: ...
- Java实现九九乘法表的输出
九九乘法表一般为三角形,每个数分别和从1到自身的数相乘然后把结果列出来,即要用到两层循环,外层是从1到9for(i=1;i<=9;i++),内层是当前数和从1到自身相乘for(j=1;j< ...
- markdown文档编写
(这里面的符号都是英文的:回车是需要:空格 空格 回车) # markdown练习---1.引入图片(1和4只差!) 来转换成正整数 //把数组中的元素按照N进制转换成为正整数 #include <stdio.h> #include <std ...
- Linux 配置 vimrc
由于熟悉了Windows下利用编译器进行编程,所以在刚刚接触Linux后的编程过程中会感觉其vim编译器的各种不方便编写程序,在逐渐的学习过程中了解到可以通过配置vimrc使得vim编译时类似于VS. ...
- swoole 异步队列
安装步骤如下(推荐把安装文件下载到 /usr/local/src 目录下): step 1: wget --no-check-certificate https://github.com/swoole ...
- Android---WebView显示Html乱码问题
webView.loadData(result,"text/html","UTF-8"); 反正是用上面的方法无法解决乱码的问题. 使用下面的方法就能完美解决了 ...