STL应用--邻值查找

#include<iostream>
#include<algorithm>
#include<limits.h>
#include<set>
using namespace std;
typedef pair<int,int> PII;
typedef long long LL;
int main(void){
    int n;
    cin>>n;
    set<PII>S;
    S.insert({INT_MAX,0});
    S.insert({INT_MIN,0});
    for(int i=1,v;i<=n;i++){
        cin>>v;
        if(i>1){
            auto it=S.upper_bound({v,0});
            auto jt=it;
            jt--;
            LL rv=it->first-(LL)v,lv=(LL)v-jt->first;
            if(lv<=rv)cout<<lv<<' '<<jt->second<<endl;
            else cout<<rv<<' '<<it->second<<endl;
        }
        S.insert({v,i});
    }
    return 0;
}

 

posted @ 2019-07-28 22:39  YF-1994  阅读(255)  评论(0编辑  收藏  举报