题目:
分析:
本题是map的应用,只需将实力设为关键字,id设为值,便可在每插入一个会员信息后(map按关键字自动从小到大排序)通过比较前后元素的关键字(frist)来判断比赛对手。
参考代码:
#include<iostream>
using namespace std;
#include<map>
int main(){
int n;
cin >> n;
map<int,int> club;
club[1000000000] = 1;
int id, power;
map<int,int>::iterator it,per,last;
while(n--){
cin >> id >> power;
club[power] = id;
it = club.find(power);
last = per = it;
per--;
last++;
if(it == club.begin()){
cout << id << " " << last->second <<endl;
}
else if(last == club.end()){
cout << id << " " << per->second <<endl;
}
else if(abs(per->first - power) > abs(last->first - power) ){
cout << id << " " << last->second <<endl;
}
else{
cout << id << " " << per->second <<endl;
}
}
return 0;
}
注:以上代码虽然通过了OpenJudge的样例测试,但受限于本人水平,如有疏漏,恳请斧正。