80422024-01-12 11:22:23AblablablaRendező robot (80 pont)cpp17Wrong answer 5/80275ms28508 KiB
#include <bits/stdc++.h>

using namespace std;

struct segTree{
    int meret = 1;
    vector<int> fa;

    void letrehoz(int n){
        while(meret < n){
            meret *= 2;
        }

        fa.assign(2 * meret - 1, 0);
    }

    void tisztit(){
        fa.assign(2 * meret - 1, 0);
    }

    int valtoztat(int a, int b, int ind, int cel){
        if(cel < a || b < cel){
            return fa[ind];
        } else if(a == b){
            return fa[ind] = 1;
        }

        int k = (a + b) / 2;
        return fa[ind] = valtoztat(a, k, 2 * ind + 1, cel) + valtoztat(k + 1, b, 2 * ind + 2, cel);
    }

    int keres(int a, int b, int ind, int kezd, int veg){
        if(veg < a || b < kezd){
            return 0;
        } else if(kezd <= a && b <= veg){
            return fa[ind];
        }

        int k = (a + b) / 2;
        return keres(a, k, 2 * ind + 1, kezd, veg) + keres(k + 1, b, 2 * ind + 2, kezd, veg);
    }
};

int main()
{
    int n;
    cin >> n;

    vector<int> ind(n + 1, 0);
    for(int i = 1; i <= n; i++){
        int a;
        cin >> a;

        ind[a] = i;
    }

    segTree fa;
    fa.letrehoz(n);
    vector<int> elol(n + 2, 0);
    vector<int> hatul(n + 2, 0);
    int streak = 0;

    for(int i = 1; i <= n; i++){
        int ujInd = ind[i] + fa.keres(0, fa.meret - 1, 0, ind[i], n - 1);
        elol[i] = elol[i - 1];

        if(i != ujInd){
            fa.valtoztat(0, fa.meret - 1, 0, ind[i] - 1);
            elol[i] += streak + 1;
            streak = 0;
        } else{
            streak++;
        }
    }

    fa.tisztit();

    streak = 0;
    for(int i = n; i >= 1; i--){
        int ujInd = ind[i] - fa.keres(0, fa.meret - 1, 0, 0, max(ind[i] - 2, 0));
        hatul[i] = hatul[i + 1];

        if(i != ujInd){
            fa.valtoztat(0, fa.meret - 1, 0, ind[i] - 1);
            hatul[i] += streak + 1;
            streak = 0;
        } else{
            streak++;
        }
    }

    int mini = 2e9 + 7;

    for(int i = 0; i <= n; i++){
        int felul = (elol[i] == 0 ? 0 : 2 * elol[i] - 1);
        int alul = (hatul[i + 1] == 0 ? 0 : 2 * hatul[i + 1]);

        mini = min(mini, max(felul, alul));
    }

    cout << mini << "\n";
}
SubtaskSumTestVerdictTimeMemory
base5/80
1Accepted0/03ms1852 KiB
2Wrong answer0/03ms2108 KiB
3Wrong answer0/23ms2344 KiB
4Wrong answer0/33ms2428 KiB
5Accepted3/33ms2636 KiB
6Wrong answer0/24ms2928 KiB
7Wrong answer0/24ms3148 KiB
8Wrong answer0/24ms3384 KiB
9Accepted2/24ms3464 KiB
10Wrong answer0/24ms3424 KiB
11Wrong answer0/24ms3632 KiB
12Wrong answer0/24ms4008 KiB
13Wrong answer0/24ms3976 KiB
14Wrong answer0/24ms3844 KiB
15Wrong answer0/24ms3852 KiB
16Wrong answer0/24ms3832 KiB
17Wrong answer0/24ms4312 KiB
18Wrong answer0/4263ms14216 KiB
19Wrong answer0/4275ms15300 KiB
20Wrong answer0/4226ms16560 KiB
21Wrong answer0/4189ms17820 KiB
22Wrong answer0/4194ms19360 KiB
23Wrong answer0/4207ms20656 KiB
24Wrong answer0/4230ms21808 KiB
25Wrong answer0/4266ms23128 KiB
26Wrong answer0/4209ms24532 KiB
27Wrong answer0/4246ms25812 KiB
28Wrong answer0/4209ms27192 KiB
29Wrong answer0/4210ms28508 KiB