C++:(前缀和+二分)
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100010;
struct Node{
int y, r;
bool operator<(const Node n){
return y < n.y;
}
}node[N];
int find(int l, int r, int x){
while(l < r){
int mid = l + r >> 1;
if(node[mid].y >= x) r = mid;
else l = mid + 1;
}
return l;
}
int main()
{
int m;
scanf("%d", &m);
for(int i = 1; i <= m; i++){
scanf("%d %d", &node[i].y, &node[i].r);
}
int s[N];
sort(node + 1, node + 1 + m);
for(int i = 1; i <= m; i++){
s[i] = s[i - 1] + node[i].r;
}
int max = -1, max_th = node[1].y, l;
for(int i = 1; i <= m; i++){
int resL = 0, resR = 0, res = 0;
l = find(1, m, node[i].y) - 1; //找到小于阈值的最大值的坐标
resL = l - s[l]; //找到左边预测正确(预测为0)的个数
resR = s[m] - s[l];//找到右边预测正确(预测为1)的个数
res = resL + resR;
if(res >= max){
max = res;
max_th = node[i].y;
}
}
// cout << max << endl;
cout << max_th << endl;
}