hdu4585Shaolin

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585

题意:

第一个人ID为1,战斗力为1e9。

给定n,给出n个人的ID和战斗力。

每个人必须和战斗力最接近他的人战斗,如果一样接近就和ID小的人战斗。

注意:不建议在比较时用很多it--和it++

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n&&n)
    {
        map<int,int>mp;
        mp[1e9]=1;
        for(int i=0;i<n;i++)
        {
            int k,g;
            cin>>k>>g;
            mp[g]=k;
            map<int,int>::iterator it=mp.find(g),it2,it3;
            it2=it;it2++;
            it3=it;it3--;
            if(it==mp.begin())cout<<k<<" "<<it2->second<<endl;
            else if(it==mp.end())cout<<k<<" "<<it3->second<<endl;
            else 
            {
                int tmp=it->first,tmp2=it2->first,tmp3=it3->first;
            
                if(abs(tmp3-tmp)<=abs(tmp-tmp2))cout<<k<<" "<<it3->second<<endl;
                else cout<<k<<" "<<it2->second<<endl;
            }
        }
    }
    return 0;
}