Submission #1126030

#TimeUsernameProblemLanguageResultExecution timeMemory
1126030AverageAmogusEnjoyerFinancial Report (JOI21_financial)C++17
14 / 100
4094 ms14496 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> bool cmin(T &i, T j) { return i > j ? i=j,true:false; }
template<class T> bool cmax(T &i, T j) { return i < j ? i=j,true:false; }

mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<int> ui(0, 1 << 30);

int rng() { 
    return ui(mrand);
}

int main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(nullptr);
    int n,k;
    cin >> n >> k;
    vector<int> a(n);
    vector<int> order(n);
    for (int i=0;i<n;i++)
        cin >> a[i];
    iota(order.begin(),order.end(),0);
    sort(order.begin(),order.end(),[&](int i,int j) {
        if (a[i]==a[j])
            return i < j;
        return a[i] < a[j];
    });
    vector<int> L(n),R(n),dp(n);
    auto Set = [&](vector<int> &v,int l,int r,int x) {
        for (;l<=r;l++)
            v[l]=x;
    };
    auto Max = [&](int l,int r) {
        int res=0;
        for (;l<=r;l++)
            cmax(res,dp[l]);
        return res;
    };
    set<int> active;
    for (int t=0,t2=0;t<n;t=t2) {
        vector<pair<int,int>> to_insert;
        while(t2 < n && a[order[t]] == a[order[t2]]) {
            int i=order[t2];
            t2++;
            if (active.empty()) {
                L[i]=R[i]=i;
                to_insert.emplace_back(i,1);
                active.insert(i);
                continue;
            }
            auto it=active.lower_bound(i);
            if (it==active.end()) {
                --it;
                int bef=*it;
                if (bef < i - k) {
                    L[i]=R[i]=i;
                    to_insert.emplace_back(i,1);
                } else {
                    int CL=L[bef],CR=i;
                    Set(R,CL,CR,CR);
                    Set(L,CL,CR,CL);
                    to_insert.emplace_back(i,Max(CL,i)+1);
                }
            } else {
                if (it!=active.begin()) {
                    auto it2=it; --it2;
                    int bef=*it2,aft=*it;
                    if (bef < i - k) {
                        if (aft > i + k) {
                            L[i]=R[i]=i;
                            to_insert.emplace_back(i,1);
                        } else {
                            int CL=i,CR=R[aft];
                            Set(R,CL,CR,CR);
                            Set(L,CL,CR,CL);
                            to_insert.emplace_back(i,Max(CL,i)+1);
                        }
                    } else {
                        if (aft > i + k) {
                            int CL=L[bef],CR=i;
                            Set(R,CL,CR,CR);
                            Set(L,CL,CR,CL);
                            to_insert.emplace_back(i,Max(CL,i)+1);
                        } else {
                            int CL=L[bef],CR=R[aft];
                            Set(R,CL,CR,CR);
                            Set(L,CL,CR,CL);
                            to_insert.emplace_back(i,Max(CL,i)+1);
                        }
                    }
                } else {
                    int aft=*it;
                    if (aft > i - k) {
                        L[i]=R[i]=i;
                        to_insert.emplace_back(i,1);
                    } else {
                        int CL=i,CR=R[aft];
                        Set(R,CL,CR,CR);
                        Set(L,CL,CR,CL);
                        to_insert.emplace_back(i,Max(CL,i)+1);
                    }
                }
            }
            active.insert(i);
        }
        for (auto &[p,x]: to_insert)
            dp[p]=x;
        to_insert.clear();
    }
    cout << *max_element(dp.begin(),dp.end()) << "\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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...