AcWing 136. 邻值查找
原题链接
中等
作者:
Value
,
2020-09-09 17:55:37
,
所有人可见
,
阅读 510
#include <iostream>
#include <set>
#include <limits.h>
#define Re register int
#define Rll register long long int
using namespace std;
typedef pair<int, int> pii;
set<pii> st;
int main(){
int n; cin >> n;
for(Re i = 1; i <= n; i ++ ){
Re tmp; cin >> tmp;
st.insert({tmp, i});
if(i == 1) continue;
pii res = {INT_MAX, INT_MIN};
set<pii>::iterator it = st.find({tmp, i});
if( ++ it != st.end()){
res = {it->first - tmp, it->second};
}
it = st.find({tmp, i});
if(it -- != st.begin()){
if(tmp - it->first <= res.first) res = {tmp - it->first, it->second};
}
cout << res.first << ' ' << res.second << endl;
}
return 0;
}