This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define REP(i,l,r) For(i,l,r-1)
#define PER(i,r,l) ForD(i,r-1,l)
#define ff first
#define ss second
#define pb emplace_back
#define all(x) x.begin(),x.end()
#define All(x,n) x+1,x+1+n
#define Alll(x,n) x,x+n
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define mpa make_pair
#ifdef NCGM
#include"debug.h"
#else
#define debug(...) "fr";
#endif
using namespace std;
const int N=1e5+3;
const ll INF=1e17;
int n,k,a[N],l[N],r[N],st[N][20];
ll f[N][104];
stack<int> skull;
void pre() {
For(i,1,n) st[i][0]=a[i];
for(int j=1;j<=16;j++)
for(int i=1;i+(1<<j)-1<=n;i++) st[i][j]=max(st[i][j-1],st[i+(1<<j-1)][j-1]);
}
int get(int l,int r) {
int len=__lg(r-l+1);
return max(st[l][len],st[r-(1<<len)+1][len]);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
For(i,1,n) cin >> a[i];
pre();
fill(l,l+n+1,-1);
fill(r,r+n+1,n+1);
For(i,1,n) {
while(sz(skull) && a[skull.top()]<a[i]) {
r[skull.top()]=i;
skull.pop();
}
skull.push(i);
}
while(sz(skull)) skull.pop();
ForD(i,n,1) {
while(sz(skull) && a[skull.top()]<a[i]) {
l[skull.top()]=i;
skull.pop();
}
skull.push(i);
}
For(i,1,n) l[i]++,r[i]--;
For(i,0,n)
For(j,1,k) f[i][j]=INF;
For(i,1,n) f[i][1]=get(1,i);
For(j,2,k)
For(i,j,n) {
For(k,j,i) f[i][j]=min(f[i][j],f[k-1][j-1]+get(k,i));
}
cout << f[n][k];
return 0;
}
Compilation message (stderr)
blocks.cpp: In function 'void pre()':
blocks.cpp:38:74: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
38 | for(int i=1;i+(1<<j)-1<=n;i++) st[i][j]=max(st[i][j-1],st[i+(1<<j-1)][j-1]);
| ~^~
# | 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... |