Submission #1171583

#TimeUsernameProblemLanguageResultExecution timeMemory
1171583kiennguyendinhK blocks (IZhO14_blocks)C++20
100 / 100
116 ms2184 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
int dp_prev[100005], dp_curr[100005], a[100005];
int n,k;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> k;
    for(int i = 0;i <= n;i++) dp_prev[i] = 1e9;
    int maxv = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        maxv = max(maxv, a[i]);
        dp_prev[i] = maxv; 
    }
    for (int i = 2; i <= k; i++) {
        stack<int> s1, s2;
        for(int i = 0;i <= n;i++) dp_curr[i] = 1e9;
        for (int j = 1; j <= n; j++) {
            int my_dp = dp_prev[j-1];
            while (!s1.empty() && s1.top() < a[j]) {
                my_dp = min(my_dp, s2.top());
                s1.pop();
                s2.pop();
            }
            if (s1.empty() || my_dp + a[j] < s1.top() + s2.top()) {
                s1.push(a[j]);
                s2.push(my_dp);
            }
            if (j >= i) dp_curr[j] = s1.top() + s2.top();
        }
        swap(dp_prev, dp_curr);
    }
    cout << dp_prev[n];
    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...