E. Packmen
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

A game field is a strip of 1 × n square cells. In some cells there are Packmen, in some cells — asterisks, other cells are empty.

Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.

In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.

Your task is to determine minimum possible time after which Packmen can eat all the asterisks.

Input

The first line contains a single integer n (2 ≤ n ≤ 105) — the length of the game field.

The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i — the cell i is empty. If there is symbol '*' in position i — in the cell i contains an asterisk. If there is symbol 'P' in position i — Packman is in the cell i.

It is guaranteed that on the game field there is at least one Packman and at least one asterisk.

Output

Print minimum possible time after which Packmen can eat all asterisks.

Examples
input
7
*..P*P*
output
3
input
10
.**PP.*P.*
output
2
Note

In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.

In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field.

题解:二分时间,判断时用now代表P能够在时间t内到达的最有方的点,pos代表最右方还没到的带点,然后通过这两点位置关系看是否满足条件。

#include<bits/stdc++.h>
#define pb push
#define ll long long
#define PI 3.14159265
using namespace std;
const int maxn=1e5+;
const int inf=0x3f3f3f3f;
ll n;
string a;
bool judge(ll t)
{
int now=-,pos=-;//pos到不了,p能够到的最右的地方
for(int i=;i<n;i++)
{
if(a[i]=='*')
{
if(now<i&&now>=pos)pos=i;
}
else if(a[i]=='P')
{
if(now<pos)
{
if(i-pos>t)return false;
else
{
now=max(t-(i-pos)+pos,i+(t-(i-pos))/);
}
}
else
{
now=i+t;
}
}
}
return now>=pos;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
cin>>n;
cin>>a;
ll l=,r=inf;
while(r-l>)
{
ll mid=(r+l)>>;
if(judge(mid))
{
r=mid;
}
else
{
l=mid+;
}
// cout<<l<<' '<<r<<endl;
}
cout<<(l+r)/<<endl;
return ;
}

http://codeforces.com/problemset/problem/847/E的更多相关文章

  1. http://codeforces.com/problemset/problem/594/A

    A. Warrior and Archer time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. http://codeforces.com/problemset/problem/712/D

    D. Memory and Scores time limit per test 2 seconds memory limit per test 512 megabytes input standar ...

  3. codeforces.com/problemset/problem/213/C

    虽然一开始就觉得从右下角左上角直接dp2次是不行的,后面还是这么写了WA了 两次最大的并不一定是最大的,这个虽然一眼就能看出,第一次可能会影响第二次让第二次太小. 这是原因. 5 4 32 1 18 ...

  4. http://codeforces.com/problemset/problem/545/D

    D. Queue time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  5. codeforces 340C Tourist Problem

    link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...

  6. codeforces B. Routine Problem 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...

  7. Codeforces 527D Clique Problem

    http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...

  8. Codeforces 706C - Hard problem - [DP]

    题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...

  9. Codeforces 1096D - Easy Problem - [DP]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...

随机推荐

  1. python线程与进程手记

    ------------------------------线程---------------------------#线程应用的第一种方式:thread模块是比较底层的模块#import threa ...

  2. SQL查询语句分类

    SQL查询语句有多种,下面总结下.首先先建三张表用于后面的实验 -- 学生表,记录学生信息 CREATE TABLE student( sno ), sname ), ssex ENUM('男','女 ...

  3. C++中static关键字作用总结

    1.先来介绍它的第一条也是最重要的一条:隐藏.(static函数,static变量均可) 当同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性.举例来说明.同时编译两个源文件 ...

  4. UEditor Flash文件上传-crossdomain.xml文件配置

    在使用UEditor富文本时,如果客户端的浏览器是低版本浏览器,如IE7.IE8等,UEditor的文件上传方式将会使用flash方式上传而不是html5,而flash在跨域时唯一的限制策略就是cro ...

  5. 最常见的三个排序(冒泡、直接插入、快速)的JS实现

    //冒泡排序function bubble(arr){ for(var i=0;i<arr.length;i++){ for(var j=0;j<arr.length-i;j++){ if ...

  6. java_eclipse添加DID实现自动提示

    便捷无错开发 对于xml 配置没有自动提示是多么恼火就不用说了,eclipse本身很多都是默认关闭了的,如果开发者不知道的话,就不知道怎么去设置,下面介绍几种自动提示设置的方法: XML:获得提示更好 ...

  7. Hash表分析

    http://baike.baidu.com/link?url=Ua74895uGf1NuPxB4pawmuAXedi427jJvM6aSLh_V1-23ptlMc7XIrr_cylIBn5d

  8. oracle 数据的导入导出

    一.数据导出 1.为输出路径建立一个数据库的directory对象. create or replace directory dumpdir as 'd:\'; 可以通过:select * from ...

  9. 换个角度审视NAT技术

    NAT (Network address translation,网络地址转换 )是局域网连接到互联网的一个对接工作. 首先要知道NAT是一个技术或者说软件而不是协议 后面你会知道NAT 是偏应用层但 ...

  10. 自制ACL+DHCP实验(初版)

    (实验用gns模拟器) ACL 实验拓扑: 实验要求: 1.1.1.1→3.3.3.3 不通 11.11.11.11→3.3.3.3 通 2.2.2.2→3.3.3.3 通 实验步骤: 步骤一:基本配 ...