Submission #1311767

#TimeUsernameProblemLanguageResultExecution timeMemory
1311767olocFinancial Report (JOI21_financial)C++20
0 / 100
203 ms214820 KiB
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vect;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
#define pb push_back
#define f first
#define s second

const int N = 1024 * 512;
pii tree[2 * N];

bool cmp(pii a, pii b){
    if(a.f != b.f) return a.f > b.f;
    return a.s < b.s;
}

void upd(int v, int val){
    int ind = v;
    v += N;
    tree[v] = {val, ind};
    v /= 2;
    while(v){
        if(cmp(tree[2 * v], tree[2 * v + 1])){
            tree[v] = tree[2 * v];
        }else{
            tree[v] = tree[2 * v + 1];
        }
        v /= 2;
    }
}

pii query(int l, int r){
    l += N; r += N;
    pii wyn = {0, 1e9};
    while(l <= r){
        if(l % 2 == 1){
            if(cmp(tree[l], wyn)){
                wyn = tree[l];
            }
            l++;
        }
        if(r % 2 == 0){
            if(cmp(tree[r], wyn)){
                wyn = tree[r];
            }
            r--;
        }
        l /= 2; r /= 2;
    }
    return wyn;
}


int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, d;
    cin >> n >> d;

    vect a(n + 1);
    a[0] = 0;

    for(int i = 0; i < 2 * N; i++){
        tree[i] = {0, (int)1e9};
    }
    for(int i = 1; i<= n; i++){
        cin >> a[i];
    }

    map<int, int> skal;
    int it = 1;
    for(int i = 1; i <= n; i++){
        skal[a[i]] = 1;
    }
    for(auto& x : skal){
        x.s = it++;
    }


    for(int i = 1; i <= n; i++){
        a[i] = skal[a[i]];
    }

    deque<pii> dq[n + 7];

    vector<pii> dp(n + 1);
    dp[0] = {0, 0};
    upd(0, 0);

    for(int i = 1; i <= n; i++){
        pii w1 = query(0, a[i] - 1); w1.f++; w1.s = a[i];
        pii w2 = query(a[i], it - 1);
        if(cmp(w1, w2)){
            dp[i] = w1;
        }else{
            dp[i] = w2;
        }

        int s = a[i];
        while(!dq[s].empty() && dq[s].back().f <= 1){
            dq[s].pop_back();
        }
        dq[s].pb({1, i});
        if(!dq[s].empty()) upd(s, dq[s].front().f); else upd(s, 0);

        int maxi = dp[i].s;
        while(!dq[maxi].empty() && dq[maxi].back().f <= dp[i].f){
            dq[maxi].pop_back();
        }
        dq[maxi].pb({dp[i].f, i});
        if(!dq[maxi].empty()) upd(maxi, dq[maxi].front().f); else upd(maxi, 0);

        if(i - d > 0){
            int ind = i - d;
            int val = a[ind];
            while(!dq[val].empty() && dq[val].front().s <= ind){
                dq[val].pop_front();
            }
            if(!dq[val].empty()) upd(val, dq[val].front().f); else upd(val, 0);

            int dm = dp[ind].s;
            while(!dq[dm].empty() && dq[dm].front().s <= ind){
                dq[dm].pop_front();
            }
            if(!dq[dm].empty()) upd(dm, dq[dm].front().f); else upd(dm, 0);
        }

    }

    cout << dp[n].f;

    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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...