1 #include<bits/stdc++.h> 2 using namespace std; 3 4 struct node { 5 int y,result; 6 }; 7 vector<node> nodes; 8 9 bool cmp(node &a,node &b) { 10 if(a.y != b.y) return a.y < b.y; 11 else return a.result < b.result; 12 } 13 vector<long int> cnt_0,cnt_1; 14 int main() { 15 int m,y,result; 16 scanf("%d",&m); 17 for(int i = 0 ; i < m; ++i) { 18 scanf("%d%d",&y,&result); 19 nodes.push_back({y,result}); 20 } 21 sort(nodes.begin(),nodes.end(),cmp); //先對輸入排序 22 23 //cnt_0存儲y前面result為0的個數 24 //cnt_1存儲y前面result為1的個數 25 cnt_0.resize(m); 26 cnt_1.resize(m); 27 if(nodes[0].result == 0) { 28 cnt_0[0] = 1; 29 cnt_1[0] = 0; 30 } else { 31 cnt_0[0] = 0; 32 cnt_1[0] = 1; 33 } 34 for(int i = 1; i < nodes.size(); ++i) { 35 if(nodes[i].result == 0) { 36 cnt_0[i] = cnt_0[i-1] + 1; 37 cnt_1[i] = cnt_1[i-1]; 38 } else { 39 cnt_1[i] = cnt_1[i-1] + 1; 40 cnt_0[i] = cnt_0[i-1]; 41 } 42 } 43 44 // 初始化 45 int final_y = nodes[0].y; 46 long int final_cnt = cnt_1[m-1]; 47 48 for(int i = 1; i < m; ++i) { 49 long int current_cnt = 0; 50 while(nodes[i].y == nodes[i-1].y && i < m) //如果前面計算過y,則跳過 51 ++i; 52 current_cnt = cnt_0[i-1] + cnt_1[m-1] - cnt_1[i-1]; //y的正確預測次數 = y前面result為0的個數 + y以及y后面result為1的個數 53 if(current_cnt >= final_cnt) { 54 final_cnt = current_cnt; 55 final_y = nodes[i].y; 56 } 57 } 58 printf("%d",final_y); 59 return 0; 60 }