#GD0000182. Ếch vượt sông (Dynamic Frog)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Ếch vượt sông (Dynamic Frog)
Dynamic Frog
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
Fred the frog starts on the left bank of a river of width meters. There are stones at distinct positions between the banks, listed in increasing order. A big stone (B) can be used repeatedly. A small stone (S) sinks immediately after Fred lands on it, and can be used at most once during the entire round trip. Fred must travel from the left bank to the right bank and back, without touching the water. He may skip stones. Minimize the maximum distance of any single jump in the whole journey.
Input
Line 1 contains two integers and : the number of stones and the distance between the banks in meters.
Line 2 contains exactly stone descriptions in increasing order of position (empty when ). Each description has the form B-M or S-M, where the letter indicates stone type and is its integer distance from the left bank. Positions are distinct and lie strictly between the banks.
Output
Print one integer: the minimum possible maximum jump length over the complete round trip.
Subtasks
- Subtask 1 (20 points): ; ; (when ); every stone is either
BorS. - Subtask 2 (30 points): ; ; (when ); every stone is either
BorS. - Subtask 3 (50 points): ; ; (when ); every stone is either
BorS.
Examples
Example 1
Input
2 10
B-3 S-6
Output
7
Explanation
The banks are at and , with a big stone at and a small stone at . On the outward trip Fred can jump , of lengths . The small stone sinks. The return trip must jump , including a jump of length . If the small stone is reserved for the return trip, the outward jump is instead . Thus is optimal.
Example 2
Input
1 10
S-5
Output
10
Explanation
There is only one small stone at the midpoint. It can be used on at most one leg. The other leg requires a direct jump between the banks of length , which is therefore unavoidable and optimal.