Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 20th, 2010 | Syntax: None | Size: 0.81 KB | Hits: 74 | Expires: Never
Copy text to clipboard
  1. 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.
  2.  
  3. in:         out:
  4. 5 10        10
  5. 3
  6. 5 3 7
  7.  
  8. in:         out:
  9. 8 20        -1
  10. 4
  11. 15 2 9 10
  12.  
  13. 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).
  14.  
  15. 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.