This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://oj.uz/problem/view/IZhO14_blocks
#include <bits/stdc++.h>
using namespace std;
#define ii pair<int , int>
#define iv pair<ii , ii>
#define iii pair<int , ii>
#define fi first
#define se second
#define int long long
const int inf = 1e18 + 7;
const int N = 1e5 + 7;
const int MOD = 1e9 + 7;
main() {
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
int n , k; cin >> n >> k;
vector<int> a(n + 1) , L(n + 1);
a[0] = inf;
deque<int> dq; dq.push_back(0);
for (int i = 1 ; i <= n ;i ++) {
cin >> a[i];
while (!dq.empty() && a[dq.back()] <= a[i]) dq.pop_back();
L[i] = dq.back() + 1;
dq.push_back(i);
}
vector<vector<int>> f(n + 1 , vector<int> (k + 1 , inf));
// nhan xet ta co f[i][j] <= f[i + 1][j] <= f[i + 2][j] .. <= f[n][j]
// nen ta nhan a[i] la max se lay' doan L xa nhat sao cho a[i] la max
f[0][0] = 0;
for (int j = 1 ; j <= k ; j++) {
for (int i = j ; i <= n ;i ++) {
f[i][j] = min(f[max(L[i] - 1 , j - 1)][j] , f[max(L[i] - 1 , j - 1)][j - 1] + a[i]);
}
}
cout << f[n][k];
// for (int j = 1 ; j <= k ;j ++) {
// for (int i = 1 ; i <= n ; i++) {
// cerr << f[i][j] << " ";
// }
// cerr << '\n';
// }
return 0;
}
Compilation message (stderr)
blocks.cpp:17:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
17 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |