제출 #647671

#제출 시각아이디문제언어결과실행 시간메모리
647671sugartheanhK개의 묶음 (IZhO14_blocks)C++14
0 / 100
64 ms164720 KiB
#include <bits/stdc++.h> #define ll long long #define fo(i,a,b) for(int i=a;i<=b;++i) #define fod(i,a,b) for(int i=a;i>=b;--i) #define ii pair<ll,ll> #define iii pair<ll,ii> #define fi first #define se second #define oo 1e18 #define bit(x,i) (((x)>>(i))&1) using namespace std; const int MOD = 1e9 + 7; const int N = 1e5 + 5; const int K = 1e2 + 5; int n,k,a[N]; ll dp[K][N],f[K][N]; int main() { ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>n>>k; fo(i,1,n) cin>>a[i]; memset(dp,0x3f,sizeof(dp)); memset(f,0x3f,sizeof(f)); dp[0][0] = 0; fo(i,1,k) { stack<int>st; int mx = 0; fo(j,1,n) { mx = max(mx, a[j]); if(i == 1) dp[i][j] = f[i][j] = mx; while(!st.empty() && a[st.top()] <= a[j]) { dp[i][j] = min(dp[i][j], f[i-1][st.top()] + a[j]); f[i][j] = min({f[i][j], dp[i][j], f[i][st.top()]}); st.pop(); } if(!st.empty()) { dp[i][j] = min(dp[i][j], f[i-1][st.top()] + a[j]); f[i][j] = min({f[i][j], dp[i][j], f[i][st.top()]}); } st.push(j); // cout<<i<<' '<<j<<' '<<dp[i][j]<<'\n'; } } cout<<dp[k][n]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...