Submission #882712

#TimeUsernameProblemLanguageResultExecution timeMemory
882712Sokol080808Money (IZhO17_money)C++17
100 / 100
149 ms23084 KiB
#ifdef ONPC
    #define _GLIBCXX_DEBUG
#endif

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <malloc.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using ll = long long;
using ull = unsigned long long;
using ld = long double;

template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& p) {in >> p.first >> p.second; return in;}
template<typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2>& p) {out << p.first << ' ' << p.second; return out;}
template<typename T> istream& operator>>(istream& in, vector<T>& v) {for (auto& x : v) in >> x; return in;}
template<typename T> ostream& operator<<(ostream& out, vector<T>& v) {for (auto& x : v) out << x << ' '; return out;}
template<typename T> ostream& operator<<(ostream& out, set<T>& v) {for (auto& x : v) out << x << ' '; return out;}
template<typename T> ostream& operator<<(ostream& out, multiset<T>& v) {for (auto& x : v) out << x << ' '; return out;}

const int MAX_INT = 2147483647;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

struct Fenwick {
    vector<ll> t;
    int SIZE;

    Fenwick(int n): SIZE(n) {
        t.assign(SIZE, 0);
    }

    void inc(int ind, ll delta) {
        for (int i = ind; i < SIZE; i = (i | (i + 1))) t[i] += delta;
    }

    ll sum(int r) {
        ll res = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1) res += t[r];
        return res;
    }

    ll sum(int l, int r) {
        if (l > r) return 0;
        return sum(r) - sum(l - 1);
    }
};

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

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

    vector<int> dp(n + 1, 1e9);
    dp[0] = 0;

    Fenwick f(1000001);
    int j = 1;
    for (int i = 1; i <= n; i++) {
        while (f.sum(a[j] + 1, a[i] - 1) > 0) {
            f.inc(a[j], 1);
            j++;
        }
        dp[i] = dp[j - 1] + 1;
        if (i < n && a[i + 1] < a[i]) {
            for (int k = j; k <= i; k++) f.inc(a[k], 1);
            j = i + 1;
        }        
    }

    cout << dp[n] << '\n';

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...