Submission #1101489

#TimeUsernameProblemLanguageResultExecution timeMemory
1101489LOLOLOK blocks (IZhO14_blocks)C++14
100 / 100
550 ms100176 KiB
#include <bits/stdc++.h> #define int long long #define f first #define pii pair<int, int> #define s second const int N = 1e5 + 5; const int block = 450; const int oo = 99999989; const int base = 31; using namespace std; int n, a[N], dp[N][105], rmq[N][20], k, L[N]; stack <int> st; void build(int k){ for(int i = 0;i <= n;i++) rmq[i][0] = dp[i][k]; for(int j = 1;(1 << j) <= n;j++){ for(int i = 0; i + (1 << j) - 1 <= n;i++){ rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]); } } } int get(int l, int r){ if(r == l) return rmq[l][0]; int k = __lg(r - l + 1); return min(rmq[l][k], rmq[r - (1 << k) + 1][k]); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin >> n >> k; for(int i = 1;i <= n;i++) cin >> a[i]; st.push(0); a[0] = 1e18; for(int i = 1;i <= n;i++){ while(!st.empty() && a[st.top()] <= a[i]) st.pop(); if(st.empty()) L[i] = 1; else L[i] = st.top() + 1; st.push(i); } memset(dp, 0x4f, sizeof(dp)); dp[0][0] = 0; for(int j = 1;j <= k;j++){ build(j - 1); for(int i = j;i <= n;i++){ dp[i][j] = get(L[i] - 1, i - 1) + a[i]; dp[i][j] = min(dp[i][j], dp[L[i] - 1][j]); } } cout << dp[n][k]; 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...