-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifeguardsreal.java
More file actions
104 lines (93 loc) · 2.29 KB
/
lifeguardsreal.java
File metadata and controls
104 lines (93 loc) · 2.29 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
ID: grifync1
LANG: JAVA
PROG: lifeguardsreal
*/
//lil lil peezy
import java.util.*;
import java.io.*;
public class lifeguardsreal {
public static int conv(String s) {
return Integer.parseInt(s);
}
public static int max(int a, int b) {
return Math.max(a, b);
}
public static int min(int a, int b) {
return Math.min(a, b);
}
public static void print(int num) {
System.out.println(num);
}
public static void prints(String s) {
System.out.println(s);
}
public static void printa(int[] a) {
for (int i = 0; i < a.length; i++) {
if (i == 0) {
System.out.print(a[i]);
} else {
System.out.print(" " + a[i]);
}
}
}
public static int[] sort(int[] nums) {
Arrays.sort(nums);
return nums;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("lifeguards.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("lifeguards.out")));
//BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//PrintWriter out = new PrintWriter(System.out);
//StringTokenizer st = new StringTokenizer(in.readLine());
int len = conv(in.readLine());
Pair[] intervals = new Pair[len];
for(int i=0; i<len; i++) {
StringTokenizer st = new StringTokenizer(in.readLine());
long f = conv(st.nextToken());
long e = conv(st.nextToken());
intervals[i]=new Pair(f,e);
}
Arrays.sort(intervals);
long le = 0;
long ri = 0;
long tt = 0;
for(int i=0; i<len; i++) {
if(intervals[i].r>ri) {
le = Math.max(intervals[i].l, ri);
tt+=(intervals[i].r-le);
ri = intervals[i].r;
}
}
long min = Long.MAX_VALUE;
long right = 0;
for(int i=0; i<len; i++) {
if(i==len-1) {
min = Math.min(min, intervals[i].r-Math.max(intervals[i].l, right));
}
else {
long leftmax = Math.max(right, intervals[i].l);
long rightmin = Math.min(intervals[i].r, intervals[i+1].l);
min = Math.min(min, rightmin-leftmax);
right = Math.max(right, intervals[i].r);
}
}
min = Math.max(min, 0);
out.println(tt-min);
in.close();
out.close();
}
static class Pair implements Comparable<Pair>{
long l;
long r;
public Pair(long a, long b) {
this.l=a;
this.r=b;
}
@Override
public int compareTo(Pair o) {
return (int) (this.l-o.l);
}
}
}