Submission #1088929

#TimeUsernameProblemLanguageResultExecution timeMemory
1088929kh0iK blocks (IZhO14_blocks)C++17
100 / 100
237 ms84180 KiB
#include "bits/stdc++.h"
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif

using ll = long long;
using pii = pair<int, int>;

#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
    assert(l <= r);
    return uniform_int_distribution<ll> (l, r)(rng);
}

const int N = 1e5 + 3;

int n, k;
ll a[N], f[103][N];

void solve(){
    cin >> n >> k;
    for(int i = 1; i <= n; ++i)
        cin >> a[i];

    memset(f, 0x3f, sizeof(f));
    f[1][1] = a[1];

    for(int i = 2; i <= n; ++i)
        f[1][i] = max(f[1][i - 1], a[i]);

    for(int j = 2; j <= k; ++j){
        vector<pair<int, ll>> st;
        for(int i = 1; i <= n; ++i){
            ll s = f[j - 1][i - 1];
            while(!st.empty() and a[st.back().F] <= a[i]){
                s = min(s, st.back().S);
                st.pop_back();
            }
            int lv = (st.empty() ? 0 : st.back().F);
            f[j][i] = min(f[j][lv], s + a[i]);
            st.push_back({i, s});
        }
    }

    cout << f[k][n];
}

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(0);
    #define task "troll"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int test = 1;
//    cin >> test;
    for(int i = 1; i <= test; ++i){
//        cout << "Case #" << i << ": ";
        solve();
    }
    #ifdef LOCAL
        cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    #endif
    return 0;
}

Compilation message (stderr)

blocks.cpp: In function 'int32_t main()':
blocks.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...