Submission #340039

#TimeUsernameProblemLanguageResultExecution timeMemory
340039SprdaloMoney (IZhO17_money)C++17
100 / 100
197 ms15084 KiB
#include <bits/stdc++.h>
 
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;

//Slozenost O(log n)

template<class T, int size>
struct fenwick {
    T a[size];
    /* precondition: pos > 0 */
    void add(int pos, const T& val) {
        while (pos < size) {
            a[pos] += val;
            pos += pos & -pos;
        }
    }
    T sum(int pos) {
        T ret = T();
        while (pos > 0) {
            ret += a[pos];
            pos -= pos & -pos;
        }
        return ret;
    }
};
fenwick<int, 1000010> drvo;
 
signed main()
{
    ios_base::sync_with_stdio(false); 
    cin.tie(nullptr); 
    cout.tie(nullptr); 
    cerr.tie(nullptr);    
 
    int n;
    cin >> n;
 
    int a[n];
    for (auto& i : a){
        cin >> i;
    }
 
    int l = 0, r = 1, sol = 1;
    while(1){
        if (r == n) break;
 
        if (a[r] < a[r-1]){
 
            for (int i = l; i < r; ++i)
                drvo.add(a[i], 1);
 
            ++sol;
            l = r;
            r++;
            drvo.add(a[l], 1);
            continue;
        }
 
        if (a[l]+1 > a[r]-1){
            ++r;
            continue;
        }
 
        int x = drvo.sum(a[r]-1) - drvo.sum(a[l]);
        if (!x){
            ++r;
            continue;
        }
 
        for (int i = l; i < r; ++i)
            drvo.add(a[i], 1);
        l = r;
        ++sol;
        ++r;
    }
 
    cout<< sol << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...