#BS0000385. Nhà sư leo núi (Traveling Monk)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Nhà sư leo núi (Traveling Monk)
Traveling Monk
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
A monk climbs a mountain one morning and descends along the same path on another morning. Both trips start at sunrise, defined as time 0. Each trip consists of consecutive segments with constant elevation change per unit time; resting segments have zero elevation change. Total elevation gained equals total elevation lost. After finishing a trip, the monk remains at its destination. Find the earliest elapsed time at which the elevations along both trips coincide.
Input
The first line contains integers , the numbers of ascent and descent segments. Each of the next lines contains integers , elevation gain and duration of an ascent segment. Each of the final lines contains integers , elevation loss and duration of a descent segment. Segments are listed in travel order.
Output
Print the earliest elapsed time. The absolute or relative error must not exceed .
Subtasks
- Subtask 1 (20 points): ; ; for every segment; total elevation gained equals total elevation lost.
- Subtask 2 (30 points): ; ; for every segment; total elevation gained equals total elevation lost.
- Subtask 3 (50 points): ; ; for every segment; total elevation gained equals total elevation lost.
Examples
Input
1 1
10 11
10 10
Output
5.238095
Explanation
The ascent gains 10 in 11 time units, while the descent loses 10 in 10 units. Their elevations are and . Equating them yields .
Input
3 1
4 2
0 3
6 3
10 7
Output
4.200000
Explanation
The ascending monk reaches elevation 4 at time 2 and rests until time 5. The descending elevation is . At time 4.2 it equals 4, and earlier it is higher than the ascending elevation.
Input
3 3
2 3
0 5
3 1
3 4
0 2
2 2
Output
4.000000
Explanation
The mountain height is 5. The ascending monk reaches elevation 2 at time 3 and rests until time 8. The descending monk loses 3 by time 4 and rests until time 6. The elevations coincide throughout [4,6], so the earliest time is 4.