Packmen CodeForces - 847E
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.
Example
7
*..P*P*
3
10
.**PP.*P.*
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.
题解:二分时间,然后检查答案(emmmmm,然而check不会写),借鉴大佬的代码(》《)
官方:这是 codeforces 343c Read Time的题解,不过和这道题是相同的解法
Let's search the answer t with the binary search. Fix some value of t. Look at the first head from the left h[i] that can read track p[0]. If p[0] > h[i], then h[i] goes to the right t seconds and reads all tracks on its way. Otherwise if p[0] ≤ h[i], then the head has two choices:
- go to the right
seconds, then
to the left and h[i] - p[0] again to the left;
- go to the left h[i] - p[0] seconds, then h[i] - p[0] to the right and t - 2·(h[i] - p[0]) again to the right.
Obviously, for h[i] it is more advantageous to visit the track positioned as much as possible to the right. So we choose by . Then we move the pointer onto the first unread track, and repeat the algorithm for h[i + 1], and so on with each head.
Solution complexity: . Problem authors
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=1e5+5; 5 6 int n,pn,sn; 7 int p[maxn],x[maxn]; 8 char S[maxn]; 9 10 int calc(int l,int r,int pos){ 11 return min(abs(pos-l),abs(pos-r))+abs(r-l); 12 } 13 14 bool check(int temp){ 15 int pre=-1,pos=-1; 16 for(int i=0;i<pn;i++){ 17 while((pos<sn-1)&&(calc(x[pre+1],x[pos+1],p[i])<=temp)) ++pos; 18 pre=pos; 19 } 20 return pos==sn-1; 21 } 22 23 int main() 24 { scanf("%d%s",&n,S); 25 pn=0,sn=0; 26 for(int i=0;i<n;i++){ 27 if(S[i]=='P') p[pn++]=i; 28 if(S[i]=='*') x[sn++]=i; 29 } 30 int l=0,r=2*n; 31 while(l<r){ 32 int mid=(l+r)>>1; 33 if(check(mid)) r=mid; 34 else l=mid+1; 35 } 36 printf("%d\n",r); 37 }