アイデア:
質問の意味に答えはあるすべての合計 対応する ==番号です現在よりも大きな 1を加えたすべての 対応 ==番号です 小さい よりも現在の1 。
原理は非常に単純です。データ1e5 は、キー値としてソートされる構造を 直接記述し、プレフィックスチェックポイントを使用して、に対する答えを計算します。
答えには最大の 値が必要 であり、その過程で質問の==の状態に注意を払うので、マップを使用して現在の 初期位置を判断し、==の状況を確認することに注意 して ください。
コード:
import java.util.Scanner;
import java.util.HashMap;
import java.util.*;
class node implements Comparable<node>
{
int x, y;
public node(int x, int y){
this.x = x;
this.y = y;
}
public int compareTo(node a) {
if (this.x != a.x)
return this.x-a.x;
else
return this.y-a.y;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
node a[] = new node[100010];
for (int i = 1; i <= n; i ++){
int x = in.nextInt();
int y = in.nextInt();
a[i] = new node(x, y);
}
Arrays.sort(a, 1, n+1);
int [] pre1 = new int[100010];
int [] pre0 = new int[100010];
for(int i = 1; i <= n; i ++){
pre1[i] = pre1[i-1]+a[i].y;
pre0[i] = i-pre1[i];
// System.out.println("i: " + i + " pre0: " + pre0[i] + " pre1: " + pre1[i]);
}
int tmp = 0, ans = 0, pos = 1;
Map<Integer, String> mp = new HashMap<Integer, String>();
for (int i = 1; i <= n; i ++) {
if( mp.get(a[i].x)!="1"){
pos = i-1;
mp.put(a[i].x, "1");
}
// System.out.println("pos: " + pos);
int res = pre1[n]-pre1[i - 1]+pre0[pos]-pre0[1];
// System.out.println("res: " + res);
if (res >= tmp) {
if (a[i].x > ans) {
tmp = res;
ans = a[i].x;
}
}
}
System.out.println(ans);
}
}