#include <iostream>
#include <algorithm>
using namespace std;
int LocalInArr(int arr[],int canshu,int length){
int left=0;
int right = length-1;
int local=0;
while(left<right && local<length) {
int middle = (left+right)/2;
if(middle == left || middle==right) {
if(canshu == arr[right]) {
local = right;
break;
}else if(canshu==arr[left]) {
local = left;
break;
}
//和left和right都比一下
if(canshu<arr[left]) {
local = left;
break;
}else if(canshu<arr[right] && canshu>arr[left]) {
local = right;
break;
}else if(canshu>arr[right]) {
local = right+1;
break;
}
}
if(arr[middle]>canshu) {
right=middle;
local = right;
}
else if(arr[middle]<canshu) {
left = middle;
local = left;
}
else {
local=middle;
break;
}
}
return local;
}
int main(){
int num;
std::ios::sync_with_stdio(false);
cin>>num;
//把挂科的放到一个数组里
int guake[num]={-1};
//没挂的放到一个数组里
int buguake[num]={-1};
//再建立一个哈希表 key是不重复的阈值 value是用该阈值预测成功了多少次
map<int, int> m1;
map<int,int>::iterator it;
int index=0;
int index1=0;
int temp,temp1;
for(int i=0;i<num;i++){
cin>>temp;
cin>>temp1;
if(temp1==0){
guake[index]=temp;
index++;
}else{
buguake[index1]=temp;
index1++;
}
it = m1.find(temp);
if(it == m1.end()) m1.insert(pair<int, int>(temp, 0));
}
std::ios::sync_with_stdio(false);
cin.tie(0);
sort(guake,guake+index);
sort(buguake,buguake+index1);
//找成功次数最多的阈值 和其成功的次数
int BigYu=INT_MIN;
int BigYuCount = INT_MIN;
//开始对每个阈值进行判断 遍历map
for(it=m1.begin();it!=m1.end();++it){
int value = 0;
int i=0;
//从guake数组里判断有多少个数字比它小
i = LocalInArr(guake,it->first,index);
value+=i;
//从buguake数组里判断有多少个数字比它小
i = LocalInArr(buguake,it->first,index1);
value+=(index1-i);
m1.insert(pair<int, int>(it->first, it->second+=value));
int a = it->second+value;
if(a >= BigYuCount) {
BigYu=it->first;
BigYuCount=a;
}
}
cout<<BigYu<<endl;
return 0;
}
CCF-CSP 202012-2 期末预测之最佳阈值
最新推荐文章于 2022-09-18 13:46:32 发布