直接暴力只有70分
import java.util.*;
class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// max:预测最多的个数;ans:结果;cnt:以 xx 安全指数预测成功的个数
int n = in.nextInt(), max = -1, ans = -1, cnt = 0;
// 存储所有数据,按安全指数升序
PriorityQueue<int[]> p = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
// 存储所有的安全指数
Set<Integer> set = new TreeSet<>();
// 输入
while ( n-- != 0 ) {
int[] temp = new int[]{
in.nextInt(), in.nextInt()};
p.add(temp);
set.add(temp[0]);
// 累计没挂科的人数,后面遍历遇到直接--即可。等同于 所有没挂科人数 - 当前遍历到的没挂科人数
if ( temp[1] == 1 ) cnt++;
}
// set 升序遍历
for ( int next: set ) {
while ( !p.isEmpty() && p.peek()[0] < next ) {
// 对于 type==1 来说,预测对的人数 - 1,对于 type==0 来说,预测对的人数 + 1
if ( p.peek()[1] == 1 ) cnt--;
else cnt++;
p.poll();
}
if ( cnt >= max ) {
ans = next;
max = cnt;
}
}
System.out.println(ans);
}
}