202012-2
//注意不能使用两重循环 #include <bits/stdc++.h> using namespace std; const int maxn=2e5+5; struct node{ int x,y; }s[maxn]; struct node1{ int x,y,a,b,sum; }s1[maxn]; int cmp(node n1,node n2){ return n1.x<n2.x; } int cmp1(node1 t1,node1 t2){//根据rule1,rule2定义好规则 if(t1.sum==t2.sum) return t1.x<t2.x; return t1.sum<t2.sum; } int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;++i){ scanf("%d%d",&s[i].x,&s[i].y); }//将每对值存储 sort(s+1,s+1+n,cmp);//从小到大排好序 // int k=1; s1[k].x=s[1].x; if(s[1].y==1) s1[k].b++; else s1[k].a++; for(int i=2;i<=n;++i){ if(s[i].x == s1[k].x) {//目的是使得s1成功去重 if(s[i].y==1) s1[k].b++; else s1[k].a++; } else{ k++; s1[k].x=s[i].x; if(s[i].y==1) s1[k].b++; else s1[k].a++; } } int a=0; int b=0; int sum=0; for(int i=1; i<=k; i++) { sum+=s1[i].a; s1[i].a=sum; } sum=0; for(int i=k; i>=1; i--) { sum+=s1[i].b; s1[i].b=sum; }//分别对两种情况(大于1,小于0):正向,反向统计 --->同时利用已失效的空间a,b s1[0].a=0; for(int i=1; i<=k; i++) s1[i].sum=s1[i-1].a+s1[i].b;//归总求和 sort(s1+1,s1+1+k,cmp1);//找到最大的那个 printf("%d\n",s1[k].x); return 0; }