[Java] 期末预测之最佳阈值 202012-2
import java.io.*;
import java.util.*;
class Pair implements Comparable<Pair>
{
private int y;
private int result;
public Pair (int y, int result)
{
this.y = y;
this.result = result;
}
public int compareTo (Pair that) {
return this.y - that.y;
}
public String toString() {
return y " " result;
}
public int y() {
return this.y;
}
public int result() {
return this.result;
}
}
public class Main
{
public Main() throws IOException
{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
StringTokenizer st = null;
int m = Integer.parseInt(f.readLine());
Pair[] pairs = new Pair[m];
for (int i = 0; i < m; i )
{
st = new StringTokenizer(f.readLine());
int y = Integer.parseInt(st.nextToken());
int res = Integer.parseInt(st.nextToken());
pairs[i] = new Pair(y, res);
}
Arrays.sort(pairs);
Map<Integer, Integer> map0 = new HashMap<>();
for (int i = 0, prev = -1, count = 0; i < m; i )
{
// now the value is bigger than previous one
if (prev >= 0 && pairs[prev].y() < pairs[i].y()) {
if (pairs[prev].result() == 0) {
map0.put(pairs[i].y(), map0.getOrDefault(pairs[prev].y(), 0) count);
}
else {
map0.put(pairs[i].y(), map0.getOrDefault(pairs[prev].y(), 0));
}
}
if (pairs[i].result() == 0) {
if (prev >= 0 && pairs[prev].y() == pairs[i].y()) {
count ;
}
else {
count = 1;
}
prev = i;
}
}
Map<Integer, Integer> map1 = new HashMap<>();
for (int i = m - 1, prev = m; i >= 0; i--)
{
if (prev < m && pairs[prev].y() > pairs[i].y()) {
if (pairs[prev].result() == 1) {
map1.put(pairs[i].y(), map1.getOrDefault(pairs[prev].y(), 0));
}
}
if (pairs[i].result() == 1) {
map1.put(pairs[i].y(), map1.getOrDefault(pairs[i].y(), 0) 1);
prev = i;
}
}
int best_theta = -1, times = 0;
for (int i = 0; i < m; i ) {
int theta = pairs[i].y();
int temp = map0.getOrDefault(theta, 0) map1.getOrDefault(theta, 0);
if (temp >= times) {
times = temp;
best_theta = theta;
}
}
out.println(best_theta);
out.close();
f.close();
}
public static void main(String[] args) throws IOException {
new Main();
}
}
赞 (0)