Repository navigation
Expand file tree
/
Copy pathBalancedQueue1797.java
More file actions
104 lines (88 loc) · 2.29 KB
/
Copy pathBalancedQueue1797.java
File metadata and controls
104 lines (88 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
package boj;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
public class BalancedQueue1797 {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
List<MyFan> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int sex = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
list.add(new MyFan(sex, x));
}
Collections.sort(list);
int count = 1;
int max = 0;
for (int i = 1; i < n; i++) {
MyFan pre = list.get(i-1);
MyFan cur = list.get(i);
if (cur.isDifferent(pre)) {
count++;
} else {
if (count > 1) {
if (count % 2 == 0) {
int d = pre.getDistance(list.get(i - count));
max = Math.max(max, d);
}
else {
int d1 = pre.getDistance(list.get(i - (count - 1)));
int d2 = list.get(i-2).getDistance(list.get(i - count));
max = Math.max(max, Math.max(d1, d2));
}
}
count = 1;
}
}
if (count > 1) {
if (count % 2 == 0) {
int d = list.get(n-1).getDistance(list.get((n - 1) - (count - 1)));
max = Math.max(max, d);
}
else {
int d1 = list.get(n-1).getDistance(list.get((n - 1) - (count - 2)));
int d2 = list.get(n-2).getDistance(list.get((n - 2) - (count - 1)));
max = Math.max(max, Math.max(d1, d2));
}
}
System.out.println(max);
}
}
class MyFan implements Comparable<MyFan>{
private int sex;
private int x;
public int getSex() {
return sex;
}
public int getX() {
return x;
}
public MyFan(int sex, int x) {
this.sex = sex;
this.x = x;
}
public boolean isDifferent(MyFan other) {
return this.sex != other.sex;
}
public int getDistance(MyFan other) {
return this.x - other.x;
}
@Override
public String toString() {
return "MyFan [sex=" + sex + ", x=" + x + "]";
}
@Override
public int compareTo(MyFan f) {
// TODO Auto-generated method stub
if (this.x < f.x) return -1;
if (this.x > f.x) return 1;
return 0;
}
}