Submission #581158

#TimeUsernameProblemLanguageResultExecution timeMemory
581158FatihSolakK blocks (IZhO14_blocks)C++17
0 / 100
1 ms340 KiB
#include <bits/stdc++.h> #define N 100005 #define K 105 #define M 20 using namespace std; int a[N]; int sp[N][M]; int dp[N]; int ndp[N]; int val[N]; int cost(int l,int r){ return max(sp[l][val[r-l+1]],sp[r - (1<<val[r-l+1]) + 1][val[r-l+1]]); } void dq(int l,int r,int optl,int optr){ if(l > r) return; int m = (l + r)/2; ndp[m] = 1e9; int opt = optl; for(int i = optl;i<=min(optr,m);i++){ if(dp[i-1] + cost(i,m) < ndp[m]){ opt = i; ndp[m] = dp[i-1] + cost(i,m); } } dq(l,m-1,optl,opt); dq(m+1,r,opt,optr); } void solve(){ int n,k; cin >> n >> k; int cnt = 0; for(int i = 1;i<=n;i++){ cin >> a[i]; sp[i][0] = a[i]; if(i > (1<<(cnt+1))) cnt++; val[i] = cnt; dp[i] = 1e9; } for(int j = 1;j<M;j++){ for(int i = 1;i<=n;i++){ if(i + (1<<j) - 1 <= n){ sp[i][j] = max(sp[i][j-1],sp[i + (1<<(j-1))][j-1]); } } } for(int i = 1;i<=k;i++){ dq(1,n,1,n); for(int j = 1;j<=n;j++){ dp[j] = ndp[j]; //cout << dp[j] << " "; } dp[0] = 1e9; //cout << endl; } cout << dp[n]; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef Local freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif int t = 1; //cin >> t; while(t--){ solve(); } #ifdef Local cout << endl << fixed << setprecision(2) << 1000.0*clock()/CLOCKS_PER_SEC << " milliseconds."; #endif }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...