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的格子里,你可以选择任意一个格 ...
随机推荐
- Objective-C中的self和super
1.有过面向对象的人知道,self相当于this,super相当于调用父类的方法 2.self是类的隐藏的参数,指向当前调用方法的类,另一个隐藏参数是_cmd,代表当前类方法的selector. su ...
- postgresql 分区表
1.普通方式建立主表 create table tbl_partition( id integer, name ), gender boolean, join_date date, dept ) ) ...
- 【C++】快排
假设要排序的数据类型为int int main() { qsort(a,len,sizeof(int),cmp); //qsort(数组的起始位置,排序个数,类型大小,比较函数); } int cmp ...
- 2016/09/21 java关键字static
1.static方法 static方法一般称作静态方法,由于静态方法不依赖于任何对象就可以进行访问,因此对于静态方法来说,是没有this的,因为它不依附于任何对象,既然都没有对象,就谈不上th ...
- [terry笔记]更改oracle用户名
更改oracle的用户名 之前有个需求,整理一个schema的表.索引等规划到一个表空间里,利用expdp/impdp然后remap就完成了,但是整理好的用户名remap变更了,应用又不想修改其连接信 ...
- Microsoft Power BI Designer
1/25/2015年1月25发布的预览版本,可以通过以下地址下载,注意有x64 和x32 版本区别(和上次PowerMap一样,一般也推荐的使用x64版本) http://www.microsoft. ...
- Express实现http和https服务
一.介绍Http与Https 概念 HTTP: 超文本传输协议(Hypertext transfer protocol) 是一种详细规定了浏览器和万维网服务器之间互相通信的规则,通过因特网传送万维网文 ...
- 宝马测试(C++实现)
测试目的:对编辑器放大,缩小性能测试. 测试资源:一匹宝马. 测试结果:良好. 实现方法:通过调用本地保存的宝马文件,逐字逐行的显示在编辑器中,并放大,缩小.对不同的符号进 ...
- MIFARE系列1《MIFARE简介》
随着社会的发展,智能卡在很多领域得到了广泛的应用.特别是非接触卡,由于使用方便以及功能强大的特点,在管理.公交.工作证.身份识别等领域得到了快速的普及和推广. 非接触卡已经逐步发展成为一个独立的跨学科 ...
- hdu 2579 Dating with girls(2)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have sol ...