제출 #1222433

#제출 시각아이디문제언어결과실행 시간메모리
1222433__moin__Peru (RMI20_peru)C++20
0 / 100
463 ms29764 KiB
#include <bits/stdc++.h>
using namespace std;

#include "peru.h"

#define ll long long

const ll p = 1e9+7;

int solve(int n, int k, int* v){
    vector<ll> dp(n);  // min effort, last idx of prev segment
    function<ll(int)> get = [&](int idx) {
        if (idx < 0) return 0ll;
        return dp[idx];
    };
    // vector<int> mon_s;
    int last_opt_idx = -1;  // points to last!!! idx such that v[i] is NOT active
    multiset<int> active;
    for (int i = 0; i < n; i++) {
        // while (!mon_s.empty() && v[mon_s.back()] <= v[i]) mon_s.pop_back();
        // mon_s.push_back(i);

        // if (i-k >= 0) active.erase(active.find(v[i-k]));
        active.insert(v[i]);

        // last_opt_idx = max(last_opt_idx, i-k);
        if (last_opt_idx < i-k) {
            last_opt_idx++;
            active.erase(active.find(v[last_opt_idx]));
            assert(last_opt_idx == i-k);
        }

        auto do_increment = [&]() {
            if (last_opt_idx == i-1) return false;
            int curr_val = get(last_opt_idx) + (*active.rbegin());
            last_opt_idx++;
            active.erase(active.find(v[last_opt_idx]));
            int next_val = get(last_opt_idx) + (*active.rbegin());
            active.insert(v[last_opt_idx]);
            last_opt_idx--;
            return next_val <= curr_val;
        };

        while (do_increment()) {
            last_opt_idx++;
            active.erase(active.find(v[last_opt_idx]));
        }

        ll val = get(last_opt_idx) + (*active.rbegin());

        // pair<ll, int> curmin = {1e18, 0};
        // int runmax = v[i];
        // for (int j = i-1; j >= i-k; j--) {
        //     curmin = min(curmin, {(ll)get(j).first + (ll)runmax, j});
        //     if (j < 0) break;
        //     runmax = max(runmax, v[j]);
        // }
        dp[i] = val;
    }
    ll factor = 1;
    ll res = 0;
    for (int i = n-1; i >= 0; i--) {
        res += ((dp[i]%p)*factor) % p;
        res %= p;
        factor *= 23;
        factor %= p;
    }
    return res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...