1 条题解

  • 2
    @ 2021-8-7 20:42:20

    C++ :

    #include <bits/stdc++.h>
     
    using namespace std;
    const int maxn = 1e5 + 10;
    const int inf = 0x3f3f3f3f;
     
    struct node {
        int val, pos;
     
        bool operator<(const node &rhs) const {
            return val < rhs.val;
        }
    };
     
    set<node> p;
     
    int main() {
        int n, a, b;
        scanf("%d", &n);
        scanf("%d", &a);
        p.insert({a, 1});
        set<node>::iterator q, t;
        for (int i = 2; i <= n; i++) {
            scanf("%d", &b);
            p.insert({b, i});
            if (p.size() == 2) {
                printf("%d 1\n", abs(a - b));
                continue;
            }
            q = p.find({b, i});
            if ((++q) == p.end()) {
                q--;
                node x = (*(--q));
                x.val = abs(b - x.val);
                printf("%d %d\n", x.val, x.pos);
                continue;
            } else if ((--q) == p.begin()) {
                node x;
                x.val = abs(b - (*(++q)).val);
                x.pos = (*(q)).pos;
                printf("%d %d\n", x.val, x.pos);
                continue;
            }
            node x, y;
            q--;
            x.val = (*(q)).val;
            x.pos = (*q).pos;
            q++;
            y.val = (*(++q)).val;
            y.pos = (*(q)).pos;
            if (abs(b - x.val) < abs(b - y.val))
                printf("%d %d\n", abs(b - x.val), x.pos);
            else if (abs(b - x.val) > abs(b - y.val))
                printf("%d %d\n", abs(b - y.val), y.pos);
            else {
                printf("%d %d\n", abs(b - y.val), x.pos);
            }
        }
        return 0;
    }
    
    • 1

    信息

    ID
    47
    时间
    1000ms
    内存
    128MiB
    难度
    1
    标签
    递交数
    67
    已通过
    50
    上传者