link:https://www.luogu.com.cn/record/191394942
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, x;
map<int, int> m;
signed main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
for (int i = 1, x; i <= n; i ++ ){
cin >> x;
if(i == 1){
m[x] = 1;
continue;
}
if(m.count(x) == 0){
m[x] = i;
auto it = m.find(x), a = it, b = it;
int minn = INT_MAX, p = -1;
if(it != --m.end())
minn = abs(x - (++a)->first), p = a->second;
if(it != m.begin() && minn > (--b)->first - x)
minn = abs(x - b->first), p = b->second;
cout << minn << ' ' << p<< '\n';
}else{
cout << 0 << ' ' << m.find(x)->second;
}
}
return 0;
}