-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ1225_3.java
More file actions
53 lines (40 loc) · 1.44 KB
/
Copy pathJ1225_3.java
File metadata and controls
53 lines (40 loc) · 1.44 KB
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
package TIL;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class J1225_3 {
static int hour, min;
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String[] input = buffer.readLine().split(" ");
hour = Integer.parseInt(input[0]);
min = Integer.parseInt(input[1]);
int now = hour * 60 + min;
int umm = Integer.parseInt(buffer.readLine());
int best = Integer.MAX_VALUE;
for(int i = 0; i < umm; i++){
input = buffer.readLine().split(" ");
int busHour = Integer.parseInt(input[0]);
int busMin = Integer.parseInt(input[1]);
int interval = Integer.parseInt(input[2]);
int next = attach(now, busHour, busMin, interval);
if (next < best) best = next;
}
int ans = best % 1440;
int hh = ans / 60;
int mm = ans % 60;
System.out.printf("%02d:%02d\n", hh, mm);
}
private static int attach(int now, int busHour, int busMin, int interval) {
int s = busHour * 60 + busMin;
int d = interval;
int last = s + ((1439 - s) / d) * d;
if (now > last) {
return s + 1440;
}
if (now <= s) return s;
int diff = now - s;
int k = (diff + d - 1) / d;
return s + k * d;
}
}