Submission #1061733

#TimeUsernameProblemLanguageResultExecution timeMemory
1061733VMaksimoski008K blocks (IZhO14_blocks)C++17
53 / 100
1069 ms42916 KiB
#include <bits/stdc++.h> //#define int long long using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int mod = 1e9 + 7; const int LOG = 20; const int maxn = 1e5 + 5; struct SegTree { int n; vector<ll> tree, lazy; SegTree(int _n) : n(_n), tree(4*_n+5), lazy(4*_n+5) {} void push(int u, int tl, int tr) { if(!lazy[u]) return ; tree[u] += lazy[u]; if(tl != tr) { lazy[2*u] += lazy[u]; lazy[2*u+1] += lazy[u]; } lazy[u] = 0; } void update(int u, int tl, int tr, int l, int r, ll v) { push(u, tl, tr); if(tl > tr || l > tr || tl > r) return ; if(l <= tl && tr <= r) { lazy[u] += v; push(u, tl, tr); return ; } int tm = (tl + tr) / 2; update(2*u, tl, tm, l, r, v); update(2*u+1, tm+1, tr, l, r, v); tree[u] = min(tree[2*u], tree[2*u+1]); } ll query(int u, int tl, int tr, int l, int r) { if(tl > tr || l > tr || tl > r) return 1e12; push(u, tl, tr); if(l <= tl && tr <= r) return tree[u]; int tm = (tl + tr) / 2; return min(query(2*u, tl, tm, l, r), query(2*u+1, tm+1, tr, l, r)); } void update(int l, int r, ll v) { update(1, 0, n-1, l, r, v); } ll query(int l, int r) { return query(1, 0, n-1, l, r); } }; ll dp[maxn][105], v[maxn]; signed main() { int n, k; cin >> n >> k; for(int i=0; i<=n; i++) for(int j=0; j<=k; j++) dp[i][j] = 1e9; for(int i=1; i<=n; i++) cin >> v[i]; dp[0][0] = 0; ll mx = 0; for(int i=1; i<=n; i++) { mx = max(mx, v[i]); dp[i][1] = mx; } for(int j=2; j<=k; j++) { SegTree tree(n+1); vector<pii> st; st.push_back({ 1e9, 0 }); int SZ = 1; for(int i=1; i<=n; i++) { int last = st.back().second; while(SZ && st.back().first <= v[i]) { if(SZ > 1) tree.update(st[SZ-2].second+1, st[SZ-1].second, v[i]-st.back().first); st.pop_back(); SZ--; } st.push_back({ v[i], i }); SZ++; tree.update(last+1, st.back().second, v[i]); tree.update(i, i, dp[i-1][j-1]); dp[i][j] = tree.query(1, i); } } cout << dp[n][k] << '\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...