Submission #541334

#TimeUsernameProblemLanguageResultExecution timeMemory
541334AlperenTPo (COCI21_po)C++17
70 / 70
11 ms3028 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

long long n, arr[N], nxt[N], upd[N], cur, ans;

stack<int> stck;

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);

    cin >> n;

    for(int i = 1; i <= n; i++) cin >> arr[i];

    arr[n + 1] = -1;
    stck.push(n + 1);

    for(int i = n; i >= 1; i--){
        while(arr[stck.top()] >= arr[i]) stck.pop();

        nxt[i] = stck.top();

        stck.push(i);
    }

    for(int i = 1; i <= n; i++){
        cur += upd[i];

        if(cur == arr[i]) continue;
        else{
            upd[nxt[i]] -= arr[i] - cur;

            ans++;

            cur += arr[i] - cur;
        }
    }

    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...