이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define PB push_back
#define MP make_pair
#define ft first
#define sd second
#define pii pair<int, int>
using namespace std;
const int oo = 1e9;
const int N = 100100;
const int K = 110;
const int PW = 20;
stack<pii> st;
int f[N][K], a[N], n, k, sp[N][PW], prec[N];
int get_min(int l, int r){
assert(l <= r);
int len = r - l + 1;
return min(sp[l + (1 << prec[len]) - 1][prec[len]], sp[r][prec[len]]);
}
int main(){
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
a[0] = oo;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= k; j++)
f[i][j] = oo;
f[0][0] = 0;
prec[0] = -1;
for (int i = 1; i <= n; i++)
prec[i] = prec[i >> 1] + 1;
for (int j = 1; j <= k; j++) {
while (sz(st)) st.pop();
st.push(MP(0, oo));
for (int i = 1; i <= n; i++){
sp[i][0] = f[i - 1][j - 1];
for (int po = 1; po < PW; po++){
if (i - (1 << po) + 1 < 1) break;
sp[i][po] = min(sp[i][po - 1], sp[i - (1 << (po - 1))][po - 1]);
}
while (a[st.top().ft] <= a[i])
st.pop();
pii cr = st.top();
st.push(MP(i, min(cr.sd, get_min(cr.ft + 1, i) + a[i])));
f[i][j] = st.top().sd;
}
}
cout << f[n][k];
return 0;
}
# | 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... |