Introduction
Input
Output
输出到标准输出。
输出一个整数,表示最佳阈值 θ*。
Sample
input
6
0 0
1 0
1 1
3 1
5 1
7 1
output
3
input
8
5 1
5 0
5 0
2 1
3 0
4 0
100000000 1
1 0
output
100000000
Solution
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class Main20201202 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int m=s.nextInt();
TreeMap<Integer,Integer> treeMap=new TreeMap();
int count=0;
int temp=0;
int max;
for(int i=0;i<m;i++){
int y=s.nextInt();
int result=s.nextInt();
if(result==1){
count++;
temp=1;
}else {
temp=-1;
}
if(!treeMap.containsKey(y)){
treeMap.put(y,temp);
}else {
treeMap.replace(y,treeMap.get(y)+temp);
}
}
int num=treeMap.firstKey();
max=count;
int len=treeMap.size()-1;
for(int i=0;i<len;i++){
Map.Entry<Integer, Integer> integerIntegerEntry = treeMap.pollFirstEntry();
count+=-integerIntegerEntry.getValue();
if(count>=max){
max=count;
num=treeMap.firstKey();
}
}
System.out.println(num);
}
}
Experience
终于搞对一回了,csp第2题就是要多动脑想一想方法,不能靠暴力,而是要保证O(n)的线性解题技巧就一定不会错