Codeforces847E Packmen
来源:互联网 发布:淘宝的亚麻这么垃圾 编辑:程序博客网 时间:2024/06/03 14:54
标签:二分
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;}
- Codeforces847E Packmen
- 二分答案 Packmen CodeForces
- CF 838D(Packmen Strike Back-吃豆人)
- 记录面试问题
- 搭建Windbg和Hyper-V第二代虚拟机,双机调试内核环境
- 图像质量评估算法-SAD SSD MAD MSD SSIM MS-SSIM
- Codeforces847D Dog Show
- 人脸检测——fcn
- Codeforces847E Packmen
- java数据库编程--使用JDBC连接Oracle
- Codeforces847G UniversityClasses
- 尺度不变特征变换匹配算法详解
- PHP解决抢购、秒杀、抢楼、抽奖等阻塞式高并发库存防控超量的思路方法
- Redis应用场景
- RYU控制器代码解析-Hub
- 安装Hadoop系列 — 安装SSH免密码登录
- THinkPhp隐藏入口文件