|
||||||||||
Catch The HeartTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 133 Accepted Submission(s): 7 Problem Description Do you know Popeye? He likes spinach(²¤²Ë) very much. Maybe you have played some games about him. Now here comes one game. There are many boards, on one board there is a heart, Popeye wants to catch the heart and send it to his GF, Oliver, so that, she will be happy. At the beginning, Popeye is on one board, and the heart is on another board. Every board is described as h, l, and r, the height, the left end and the right end. You can look it as a line (l, h) (r, h) in XY coordinate system. Popeye can walk on the board, jump from one board to another. Notes: 1.He can walk on a board 1 unit per second. 2.He can only jump vertically(´¹Ö±µØ), and it takes 1s for each jumping. 3.He can jump only 1 unit high, so that he can jump only if the difference of the boards¡¯ height is 1. 4.He can only jump up to the left or right point of the board. 5.He can only jump down from the left or right point of the board. You can look the picture: The problem is, find the shortest time to catch the heart. Input The first line of the input contains an integer T (T <= 20) which means the number of test cases. For each case, first line is an integer m (2 <= m <= 10000) which means the number of the boards. Then comes two lines, the first line contains h0, l0, r0, p0 (l0 <= p0 <= r0), which mean the information of the board and the position that the heart on it. Next line contains h1, l1, r1, p1 (l1 <= p1 <= r1), which mean the information of the board and the position that Popeye on it. Then comes m-2 lines, each contains hi, li, ri mean the ith (2 <= i < m) board¡¯s information. There is no common point or common parts for every two boards. All inputs are integers, for each board, 0 <= h <= 10000, 0 <= l < r <= 100000000. Output For each case, output the shortest time to catch the heart if Popeye can catch it, otherwise output -1. Sample Input
Sample Output
Author AppleMan Source | ||||||||||
|