Submission #156181

#TimeUsernameProblemLanguageResultExecution timeMemory
156181theboatmanMoney (IZhO17_money)C++17
45 / 100
1571 ms40808 KiB
#include <bits/stdc++.h>

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma comment(linker, "/stack:200000000")

#define y1 theboatman
#define make_struct(args...) {args}

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
template <typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const long long INF = 1e18 + 10;
const int inf = 1e9 + 10;
const int N = 1e6 + 10;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout.tie(0);
    
    int n;
    cin >> n;

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

    int now = 0, x = 0;
    while(now < n && x <= a[now]) {
        x = a[now];
        now++;
    }

    ordered_set <int> bank;
    for(int i = 0; i < now; i++) {
        bank.insert(a[i]);
    }

    int ans = 0;
    for(int i = now; i < n; i++) {
        ans++;

        int l = *bank.begin(), r = *--bank.end();

        //cout << a[i] << " " << l << " " << r << "\n";
        if (a[i] >= r) {
            int x = 0;
            while(i < n && x <= a[i]) {
                x = a[i];
                bank.insert(x);
                i++;
            }
            i--;
        }
        else {
            vector <int> lazy;
            if (a[i] < l) {
                int x = 0;
                while(i < n && x <= a[i] && a[i] <= l) {
                    x = a[i];
                    lazy.push_back(x);
                    i++;
                }
                i--;
            }
            else {
                int x = 0;
                int les = *--bank.upper_bound(a[i]);

                while(i < n && x <= a[i]) {
                    auto more = bank.lower_bound(a[i]);
                    if (more == bank.end()) {
                        break;
                    }

                    int pos = bank.order_of_key(les);
                    int pos1 = bank.order_of_key(*more);

                    //cout << les << " " << *more << " " << pos << " " << pos1 << " " << i << "\n";

                    if (pos1 - pos <= 1) {
                        x = a[i];
                        lazy.push_back(x);
                        i++;
                    }
                    else {
                        break;
                    }
                }

                i--;
            }

            for(auto it : lazy) {
                bank.insert(it);
            }
        }
    }

    cout << ans + 1 << "\n";
    return 0;
}

/*
find_by_order(X) k -> элемент, нумерация с нуля
order_of_key(X) кол-во < чем X
*/

Compilation message (stderr)

money.cpp:8:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(linker, "/stack:200000000")
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...