Skip to content

Commit 36db26b

Browse files
authored
Merge pull request #1523 from AlgorithmWithGod/Ukj0ng
[20251127] BOJ / G3 / Social Distancing
2 parents b573865 + c15a422 commit 36db26b

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static final long INF = (long)1e18;
9+
private static long[][] grass;
10+
private static int N, M;
11+
public static void main(String[] args) throws IOException {
12+
init();
13+
long answer = binarySearch();
14+
15+
bw.write(answer + "\n");
16+
bw.flush();
17+
bw.close();
18+
br.close();
19+
}
20+
21+
private static void init() throws IOException {
22+
StringTokenizer st = new StringTokenizer(br.readLine());
23+
N = Integer.parseInt(st.nextToken());
24+
M = Integer.parseInt(st.nextToken());
25+
26+
grass = new long[M][2];
27+
28+
for (int i = 0; i < M; i++) {
29+
st = new StringTokenizer(br.readLine());
30+
grass[i][0] = Long.parseLong(st.nextToken());
31+
grass[i][1] = Long.parseLong(st.nextToken());
32+
}
33+
34+
Arrays.sort(grass, (o1, o2) -> Long.compare(o1[0], o2[0]));
35+
}
36+
37+
public static long binarySearch() {
38+
long left = 0;
39+
long right = INF;
40+
long result = -1;
41+
42+
while (left <= right) {
43+
long mid = left + (right - left) / 2;
44+
45+
if (valid(mid)) {
46+
result = mid;
47+
left = mid + 1;
48+
} else {
49+
right = mid - 1;
50+
}
51+
}
52+
53+
return result;
54+
}
55+
56+
private static boolean valid(long target) {
57+
long count = 0;
58+
long pos = -INF;
59+
60+
for (long[] e : grass) {
61+
long nextPos = pos + target;
62+
63+
if (nextPos > e[1]) continue;
64+
65+
nextPos = Math.max(nextPos, e[0]);
66+
67+
long canPlace = (e[1] - nextPos) / target + 1;
68+
count += canPlace;
69+
70+
pos = nextPos + (canPlace - 1) * target;
71+
}
72+
73+
return count >= N;
74+
}
75+
}
76+
```

0 commit comments

Comments
 (0)