Codeforces847E Packmen

45 篇文章 0 订阅
29 篇文章 0 订阅

标签:二分

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值