Skip to content

Commit dccb512

Browse files
authored
20240426 Create cf1029c_iron_buster.java (Yawn-Sean#1641)
1 parent 451f07c commit dccb512

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class Main {
2+
3+
// https://codeforces.com/problemset/problem/1029/C
4+
// https://codeforces.com/contest/1029/submission/258176653
5+
public static void solve() throws Exception {
6+
int n = in.nextInt();
7+
var a = new long[n][2];
8+
long maxLeft = Long.MIN_VALUE / 2;
9+
long minRight = Long.MAX_VALUE / 2;
10+
var map1 = new TreeMap<Long, Integer>();
11+
var map2 = new TreeMap<Long, Integer>();
12+
for (int i = 0; i < n; i++) {
13+
long l = in.nextLong();
14+
long r = in.nextLong();
15+
a[i][0] = l;
16+
a[i][1] = r;
17+
maxLeft = Math.max(maxLeft, l);
18+
minRight = Math.min(minRight, r);
19+
map1.merge(l, 1, Integer::sum);
20+
map2.merge(r, 1, Integer::sum);
21+
}
22+
long ans = 0;
23+
for (int i = 0; i < n; i++) {
24+
long l = a[i][0], r = a[i][1];
25+
if (l == maxLeft && map1.get(l) == 1) {
26+
map1.remove(l);
27+
if (r == minRight && map2.get(r) == 1) {
28+
map2.remove(r);
29+
ans = Math.max(ans, map2.firstKey() - map1.lastKey());
30+
map2.put(r, 1);
31+
} else {
32+
ans = Math.max(ans, minRight - map1.lastKey());
33+
}
34+
map1.put(l, 1);
35+
} else {
36+
if (r == minRight && map2.get(r) == 1) {
37+
map2.remove(r);
38+
ans = Math.max(ans, map2.firstKey() - maxLeft);
39+
map2.put(r, 1);
40+
} else {
41+
ans = Math.max(ans, minRight - maxLeft);
42+
}
43+
}
44+
}
45+
out.println(ans);
46+
}
47+
}

0 commit comments

Comments
 (0)