Packmen ( 二分答案 )

Posted zhangbuang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Packmen ( 二分答案 )相关的知识,希望对你有一定的参考价值。

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
Copy
7
*..P*P*
Output
3
Input
Copy
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.

 





直接算不好算,考虑二分答案,二分的边界为1到2n

check的时候需要贪心一波,

 

1. 如果先遍历到一个点为‘*’,且前面没P,那么把这点的位置存下来,如果这个前面还有‘*’,就不用存了,只要存位置最小的那个‘*’就OK了。

 

2. 如果遍历到一个点为‘P’,设位置为i:

a. 如果P前面没有食物‘*’,那么到i+m的这段路上就不用考虑了,因为肯定能被这个P吃掉,当然,要注意一点,如果这段路上还有点为P,那么就从这个P开始,而不是i+m开始,因为这个P肯定能走的更远,而直接从i+m开始的话会把这个忽略掉,就会造成损失(就不是最优走法了)。

 

b. 如果P前面有食物‘*’,如果P在m时间内走不到前面食物的那点,那m肯定太小了,即不可能在m时间内吃完所有食物。如果可以,那就要考虑2种情况:

 

如果m>=3*x(x表示当前P到最前面还没被吃掉的食物的距离),那么肯定是往前走吃掉那个再回来。

 

如果m<3*x,那么先往后走再回去刚刚好吃掉那个,这样才能保证往后走尽可能多的距离。

 

假设最远能到tmp这个点,那么到tmp的这段路上就不用考虑了,因为肯定能被这个P吃掉,当然,同样也要注意一点,如果这段路上还有点为P,那么就从这个P开始。

 

以上是关于Packmen ( 二分答案 )的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 847E - Packmen

二分答案模板

二分答案

二分答案模板及讲解

二分与三分

二分三分总结