7122021-11-18 20:37:00Valaki2Inverziócpp14Accepted 50/50458ms78804 KiB
#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> numbers;
vector<int> values;
vector<int> segtree;

void build(int v, int vl, int vr) {
    if(vl == vr) {
        segtree[v] = vl;
    } else {
        int mid = (vl + vr) / 2;
        build(2 * v, vl, mid);
        build(2 * v + 1, mid + 1, vr);
        segtree[v] = min(segtree[2 * v], segtree[2 * v + 1]);
    }
}

void update(int v, int vl, int vr, int pos) {
    if(vl == vr) {
        segtree[v] = values[vl];
    } else {
        int mid = (vl + vr) / 2;
        if(pos <= mid) {
            update(2 * v, vl, mid, pos);
        } else {
            update(2 * v + 1, mid + 1, vr, pos);
        }
        segtree[v] = min(segtree[2 * v], segtree[2 * v + 1]);
    }
}

int rmq(int v, int vl, int vr, int ql, int qr) {
    if(qr < vl || ql > vr) {
        return 1e9;
    }
    if(vl == ql && vr == qr) {
        return segtree[v];
    }
    int mid = (vl + vr) / 2;
    return min(rmq(2 * v, vl, mid, ql, min(qr, mid)), rmq(2 * v + 1, mid + 1, vr, max(ql, mid + 1), qr));
}

void solve() {
    cin >> n;
    numbers.assign(1 + n, 0);
    values.assign(1 + n, 1e9);
    segtree.assign(1 + 4 * n, 0);
    bool inversion = false;
    for(int i = 1; i <= n; i++) {
        cin >> numbers[i];
        if(i != numbers[i]) {
            inversion = true;
        }
    }
    if(!inversion) {
        cout << "-1\n";
        return;
    }
    build(1, 1, n);
    values[numbers[1]] = 1;
    update(1, 1, n, numbers[1]);
    int ans_dist = 0, ans_left = 0, ans_right = 0;
    for(int i = 2; i <= n; i++) {
        if(numbers[i] == n) {
            continue;
        }
        int pos = rmq(1, 1, n, numbers[i] + 1, n);
        if(i - pos > ans_dist) {
            ans_left = pos;
            ans_right = i;
            ans_dist = i - pos;
        }
        values[numbers[i]] = i;
        update(1, 1, n, numbers[i]);
    }
    cout << ans_left << " " << ans_right << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1816 KiB
2Accepted0/023ms4512 KiB
3Accepted1/11ms2208 KiB
4Accepted2/22ms2240 KiB
5Accepted7/71ms2240 KiB
6Accepted2/226ms4748 KiB
7Accepted2/2245ms29288 KiB
8Accepted2/2402ms32604 KiB
9Accepted2/2451ms35800 KiB
10Accepted2/2384ms39116 KiB
11Accepted2/2354ms42436 KiB
12Accepted2/2347ms45240 KiB
13Accepted2/2384ms49012 KiB
14Accepted2/2365ms52328 KiB
15Accepted2/2256ms55652 KiB
16Accepted2/2397ms59096 KiB
17Accepted2/2433ms62296 KiB
18Accepted2/2412ms65612 KiB
19Accepted3/3245ms68932 KiB
20Accepted3/3250ms72248 KiB
21Accepted2/2259ms75568 KiB
22Accepted2/2430ms78804 KiB
23Accepted2/2458ms78804 KiB
24Accepted2/264ms78804 KiB