Submission #284355

#TimeUsernameProblemLanguageResultExecution timeMemory
284355shivensinha4K blocks (IZhO14_blocks)C++17
100 / 100
640 ms5496 KiB
#include <bits/stdc++.h> using namespace std; #define for_(i, s, e) for (int i = s; i < (int) e; i++) #define for__(i, s, e) for (ll i = s; i < e; i++) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> ii; #define endl '\n' int main() { #ifndef ONLINE_JUDGE //freopen("test.in", "r", stdin); #endif ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<ll> nums(n); for_(i, 0, n) cin >> nums[i]; vector<ll> prev(n), curr(n, 1e15); prev[n-1] = nums[n-1]; for (int i = n-2; i >= 0; i--) prev[i] = max(nums[i], prev[i+1]); for_(l, 1, k) { stack<vector<ll>> s; // {max val index, min prev val till next max} //curr[n-l-1] = nums[n-l-1] + prev[n-l]; //s.push({n-l-1, prev[n-l]}); for (int i = n-2; i >= 0; i--) { ll mn = prev[i+1], ans = prev[i+1]+nums[i]; while (s.size()) { if (nums[s.top()[0]] <= nums[i]) { mn = min(mn, s.top()[1]); s.pop(); } else { ans = min(ans, curr[s.top()[0]]); break; } } s.push({i, mn}); curr[i] = min(ans, nums[i] + mn); } swap(prev, curr); curr.assign(n, 1e15); } cout << prev[0] << endl; 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...