邻值查找
  • 板块灌水区
  • 楼主cdxxx04
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/11/27 13:58
  • 上次更新2024/11/27 16:58:39
查看原帖
邻值查找
1256312
cdxxx04楼主2024/11/27 13:58

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;
}
2024/11/27 13:58
加载中...