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 ;
}

最新文章

  1. 64位win7下安装SQL Server 2008(图文解说版)
  2. maven工程直接部署在tomcat上
  3. 一款jQuery特效编写的大度宽屏焦点图切换特效
  4. sql 自定义函数--固定格式字符转时间类型
  5. linux管理网络连接指令
  6. Topo软件
  7. Linux ALSA声卡驱动之二:声卡的创建
  8. JS中有关正则表达式的一些常见应用
  9. 简单的C语言编译器--语法分析器
  10. Node.js系列文章:编写自己的命令行界面程序(CLI)
  11. Java 中的纤程库 – Quasar
  12. Python:python抓取豆瓣电影top250
  13. 海纳百川而来的一篇相当全面的Java NIO教程
  14. Android测试(一)——Apk文件结构以及Android组件介绍
  15. gitlab入门
  16. SWD Connect/Transfer Source Code
  17. 自定义 tableviewheader 高度显示不正常
  18. tensorflow的日常Demo
  19. SaltStack系列(四)之实例编写
  20. JVM启动报错: Could not reserve enough space for object heap error

热门文章

  1. CSS布局技巧大全
  2. jQuery实现鼠标滑过导航栏呈现不同的样式
  3. 表单校验demo
  4. 实现NFS共享wordpress
  5. 谈谈.NET,Java,php
  6. vue项目引入bootstrap、jquery
  7. html5 javascript 小型计算器
  8. JAVA基础第九组(5道题)
  9. 团队作业10——Beta版本事后诸葛亮
  10. 201521123102 《Java程序设计》第8周学习总结