Codeforces847e packmen

阅读全文大概需要 3分钟
本文总阅读量
Posted by yjjr's blog on February 6, 2018

标签:二分

A gamefield is a strip of 1 × n squarecells. In some cells there are Packmen, in some cells — asterisks, othercells are empty.

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

In theinitial moment of time all Packmen begin to move. Each Packman can changedirection of its move unlimited number of times, but it is not allowed to gobeyond the boundaries of the game field. Packmen do not interfere with themovement of other packmen; in one cell there can be any number of packmenmoving in any directions.

Your taskis to determine minimum possible time after which Packmen can eat all theasterisks.

Input

The firstline contains a single integer n (2 ≤ n ≤ 105) — the length of thegame field.

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

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

Output

Printminimum possible time after which Packmen can eat all asterisks.

Example

Input

7
*..P*P*

Output

3

Input

10
.**PP.*P.*

Output

2

Note

In thefirst example Packman in position 4 will moveto the left and will eat asterisk in position 1. He will spend 3 time unitson it. During the same 3 time unitsPackman in position 6 will eat both ofneighboring with it asterisks. For example, it can move to the left and eatasterisk in position 5 (in 1 time unit) and then move from theposition 5 to the right and eatasterisk in the position 7 (in 2time units). So in 3 time units Packmen will eat all asterisks on the gamefield.

In thesecond example Packman in the position 4 will moveto the left and after 2time unitswill eat asterisks in positions 3 and 2. Packmen in positions 5 and 8will 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 onthe game field.

Code


#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
char s[maxn];
vector<int>a,b;
 
 
inline int cal(int l,int x,int r)
{
       inta=x-l,b=r-x;
       return2*min(a,b)+max(a,b);
}
int main()
{
       intn,i,j,l,r,mid,ans,ll,rr;
       scanf("%d%s",&n,s);
       for(i=0;i<n;i++){
              if(s[i]=='P')a.push_back(i);
              if(s[i]=='*')b.push_back(i);
       }
       for(l=0,r=2*n;l<=r;){
              mid=l+r>>1;
              for(i=j=0;i<a.size();i++)
                  for(ll=rr=a[i];j<b.size();j++)
                     if(cal(min(ll,b[j]),a[i],max(rr,b[j]))>mid)break;
                      else ll=min(ll,b[j]),rr=max(rr,b[j]);
              if(j<b.size())l=mid+1;
              elser=mid-1,ans=mid;
       }
       printf("%d",ans);
       return0;
}



本文可以转载,但必须附上原文链接,否则你会终生找不到妹子!!!欢迎关注我的CSDN: ahyjjr