Round Sugoroku
Time limit2sMemory limit1024 MB
A piece bounces left and right along a row, reversing direction at X and at each # it clears, until every # is erased; report the total time.
- Level
Medium6 of 10
- Topics
- Simulation, Implementation, Two pointers, Greedy
- Solved
- No attempts yet
Problem
Aoi of JOI High School bought a new sugoroku board. The board consists of N+2 squares in a single horizontal row. The squares are numbered from 0 to N+1 in order from the leftmost square to the rightmost square. Initially, squares 0 and N+1 have X written on them, and square i (1 ≦ i ≦ N) has Si written on it. Here, Si is either the character . or #.
Aoi is playing with this board and one piece. Initially, the piece is placed on square A (1 ≦ A ≦ N) facing right. Here, SA is the character .. Every second, Aoi moves the piece one square in the direction it is facing.
The board has the following rules.
- When the piece lands on a square with
X, the direction of the piece is reversed. - When the piece lands on a square with
., nothing happens. - When the piece lands on a square with
#, the direction of the piece is reversed. At this time, the character written on this square is changed to.. Therefore, after that, even if the piece lands on this square, the direction is not reversed.
The time needed to reverse the piece or change a character can be ignored.
Given the initial state of the board and the piece, write a program to find the time required until all squares with # disappear.
Input
The input is given from standard input in the following format.
N A
S
Here, S is a string of length N, and its i-th character (1 ≦ i ≦ N) is Si.
Output
Print in one line to standard output the number of seconds required until all squares with # disappear.
Constraints
2 ≦ N ≦ 200 000.1 ≦ A ≦ N.Siis either the character.or#(1 ≦ i ≦ N).SAis the character..- There is at least
1indexi(1 ≦ i ≦ N) such thatSiis the character#.