题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585
题记:用map存下和尚的等级和id,之后用迭代器找出最等级最接近的两个然后比较即可。
#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
int main(){
int n;
while(cin>>n&&n){
int k,g;
mp.clear();
mp[1000000000]=1;
while(n--){
cin>>k>>g;
mp[g]=k;
int ans;
map<int,int>::iterator it=mp.find(g);
if(it==mp.begin())
ans=(++it)->second;
else{
map<int,int>::iterator it2=it;
it2--,it++;
if(g-it2->first<=it->first-g)
ans=it2->second;
else
ans=it->second;
}
cout<<k<<" "<<ans<<endl;
}
}
return 0;
}