-
Notifications
You must be signed in to change notification settings - Fork 7
/
1661 Help Jimmy - Recursion.java
76 lines (73 loc) · 2.21 KB
/
1661 Help Jimmy - Recursion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.*;
class Board {
int left;
int right;
int height;
Board(int left, int right, int height) {
this.left = left;
this.right = right;
this.height = height;
}
}
class BoardComparator implements Comparator<Board> {
public int compare(Board a, Board b) {
return b.height - a.height;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int testcase = in.nextInt();
while (testcase-- > 0) {
int boardNumber = in.nextInt();
int origX = in.nextInt();
int origY = in.nextInt();
int maxHeight = in.nextInt();
Board[] boards = new Board[boardNumber + 1];
int[] leftMinTime = new int[boardNumber + 1];
int[] rightMinTime = new int[boardNumber + 1];
Arrays.fill(leftMinTime, -1);
Arrays.fill(rightMinTime, -1);
boards[0] = new Board(origX, origX, origY);
for (int i = 1; i <= boardNumber; ++i) {
boards[i] = new Board(in.nextInt(), in.nextInt(), in.nextInt());
}
Arrays.sort(boards, new BoardComparator());
System.out.println(helper(boards, leftMinTime, rightMinTime, maxHeight, 0, true));
}
}
private static int helper(Board[] boards, int[] leftMinTime, int[] rightMinTime, int maxHeight, int boardIndex, boolean left) {
int height = boards[boardIndex].height;
int x = left ? boards[boardIndex].left : boards[boardIndex].right;
int i = boardIndex + 1;
for (i = boardIndex + 1; i < boards.length; ++i) {
if (boards[i].left <= x && boards[i].right >= x) {
break;
}
}
if (i >= boards.length) {
if (height > maxHeight) {
return 0x3f3f3f3f;
} else {
return height;
}
} else {
int divH = height - boards[i].height;
if (divH > maxHeight) {
return 0x3f3f3f3f;
} else {
int leftTime = divH + x - boards[i].left;
int rightTime = divH + boards[i].right - x;
if (leftMinTime[i] == -1) {
leftMinTime[i] = helper(boards, leftMinTime, rightMinTime, maxHeight, i, true);
}
if (rightMinTime[i] == -1) {
rightMinTime[i] = helper(boards, leftMinTime, rightMinTime, maxHeight, i, false);
}
leftTime += leftMinTime[i];
rightTime += rightMinTime[i];
return Math.min(leftTime, rightTime);
}
}
}
}