Submission #1336963

#TimeUsernameProblemLanguageResultExecution timeMemory
1336963Born_To_LaughMoney (IZhO17_money)C++17
100 / 100
231 ms8256 KiB
// Born_To_Laugh - Hughie Do
#include <bits/stdc++.h>
#define alle(AC) AC.begin(), AC.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
[[maybe_unused]] const ll MOD = 998244353, INF = 1e9 + 7;
const int maxn = 1e6 + 10;
struct fenwick{
    int n;
    vector<int> bit;

    void init(int len){
        n = len;
        bit.assign(n + 1, 0);
    }
    fenwick(int len = 0){
        init(len);
    }
    
    void upd(int pos, int val){
        for(; pos<=n; pos += pos & -pos) bit[pos] += val;
    }
    int qr(int pos){
        int ans = 0;
        for(; pos>0; pos -= pos & -pos) ans += bit[pos];
        return ans;
    }
    int qr(int l, int r){
        if(r < l) return 0;
        return qr(r) - qr(l - 1);
    }
};
int n;
int a[maxn];
fenwick ft;
void solve(){
    ft.init(1e6 + 3);
    cin >> n;
    for(int i=1; i<=n; ++i){
        cin >> a[i];
        ft.upd(a[i], 1);
    }
    
    ft.upd(a[n], -1);
    int ans = 0;
    bool diff = 0;
    for(int i=n - 1; i>=1; --i){
        ft.upd(a[i], -1);
        if(a[i] != a[i + 1]){
            if(a[i] > a[i + 1] || ft.qr(a[i] + 1, a[i + 1] - 1) > 0){
                ans++;
                diff = 0;
                continue;
            }

            if(!diff) diff = 1;
            else if(ft.qr(a[i + 1], a[i + 1]) != 0){
                ans++;
                diff = 0;
            }
        }
    }
    cout << ans + 1 << '\n';
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...