7122021-11-18 20:37:00Valaki2Inverziócpp14Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1816 KiB
2Elfogadva0/023ms4512 KiB
3Elfogadva1/11ms2208 KiB
4Elfogadva2/22ms2240 KiB
5Elfogadva7/71ms2240 KiB
6Elfogadva2/226ms4748 KiB
7Elfogadva2/2245ms29288 KiB
8Elfogadva2/2402ms32604 KiB
9Elfogadva2/2451ms35800 KiB
10Elfogadva2/2384ms39116 KiB
11Elfogadva2/2354ms42436 KiB
12Elfogadva2/2347ms45240 KiB
13Elfogadva2/2384ms49012 KiB
14Elfogadva2/2365ms52328 KiB
15Elfogadva2/2256ms55652 KiB
16Elfogadva2/2397ms59096 KiB
17Elfogadva2/2433ms62296 KiB
18Elfogadva2/2412ms65612 KiB
19Elfogadva3/3245ms68932 KiB
20Elfogadva3/3250ms72248 KiB
21Elfogadva2/2259ms75568 KiB
22Elfogadva2/2430ms78804 KiB
23Elfogadva2/2458ms78804 KiB
24Elfogadva2/264ms78804 KiB