# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
152830 | 2019-09-09T17:54:53 Z | vex | K개의 묶음 (IZhO14_blocks) | C++14 | 0 ms | 0 KB |
#include<bits/stdc++.h> #define maxn 100005 #define INF (int)(1e9 + 77) using namespace std; int n,k; int a[maxn]; int dp[maxn][102]; int lv[maxn]; int minn[maxn][22]; void build(int ind) { for(int d=0;(1<<d)<=n+1;d++) { for(int i=0;i+(1<<d)-1<=n;i++) { if(d==0)minn[i][0]=dp[i][ind]; else { minn[i][d]=min( minn[i][d-1], minn[i+(1<<(d-1))][d-1]); } } } } int query(int lt,int rt) { int d=log2(rt-lt+1); return min( minn[lt][d], minn[rt-(1<<d)+1][d]); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>n>>k; for(int i=1;i<=n;i++)cin>>a[i]; stack<int>ms; ms.push(0); a[0]=INF; for(int i=1;i<=n;i++) { while(!ms.empty()) { int vrh=ms.top(); if(a[vrh]<=a[i]) { ms.pop(); } else break; } lv[i]=ms.top(); ms.push(i); } //for(int i=1;i<=n;i++)cout<<lv[i]<<" ";cout<<endl; for(int i=0;i<=n;i++) for(int j=0;j<=k;j++)dp[i][j]=INF; dp[0][0]=0; for(int i=1;i<=n;i++)dp[i][0]=INF; for(int j=1;j<=k;j++) { build(1,0,n,j-1); for(int i=1;i<=n;i++) { dp[i][j]=min( dp[ lv[i] ][j], query(1,0,n,lv[i],i-1)+a[i]); } } cout<<dp[n][k]; return 0; }