Untitled
By: a guest | Mar 20th, 2010 | Syntax:
None | Size: 0.81 KB | Hits: 74 | Expires: Never
You have a man standing at position P on a line. The length of the line is M. In the next N lines his movements are given(in the order he should make them), he can either move left or right but he can't move to the left more than 0 and to the right more then M. We shoud find the rightmost position in which he can be after the N movements.
in: out:
5 10 10
3
5 3 7
in: out:
8 20 -1
4
15 2 9 10
first example: the start position is 5 and you should make 3 movements. You can achive the rightmost position if you make 5 steps left (5-5=0), then 3 steps right (0+3=3) and then 7 steps right (3+7=10).
second example: the start position is 8 and the stage length is 20. Because the first step is 15 if we can't go left (8-15<0) and we can't go right (8+15>20) so the result is -1.