Submission #1088853

#TimeUsernameProblemLanguageResultExecution timeMemory
1088853Khanhcsp2K blocks (IZhO14_blocks)C++14
100 / 100
135 ms81564 KiB
#include<bits/stdc++.h> #define el '\n' #define fi first #define sc second #define int ll #define pii pair<int, int> #define all(v) v.begin(), v.end() using namespace std; using ll=long long; using ull=unsigned long long; using ld=long double; const int mod=1e9+7; const int N=1e5+11; int n, a[N], k, dp[102][N]; void sub1() { for(int i=1;i<=k;i++) for(int j=1;j<=n;j++) dp[i][j]=1e18; int mx=0; for(int i=1;i<=n;i++) { mx=max(mx, a[i]); dp[1][i]=mx; } for(int i=2;i<=k;i++) { for(int j=1;j<=n;j++) { int mx=0; for(int o=j;o>=i;o--) { mx=max(mx, a[o]); dp[i][j]=min(dp[i][j], dp[i-1][o-1]+mx); } } } cout << dp[k][n]; } void sub2() { for(int i=0;i<=k;i++) for(int j=0;j<=n;j++) dp[i][j]=1e18; int mx=0; for(int i=1;i<=n;i++) { mx=max(mx, a[i]); dp[1][i]=mx; } for(int i=2;i<=k;i++) { stack<pii> st; for(int j=i;j<=n;j++) { int s=dp[i-1][j-1]; while(!st.empty() && a[st.top().fi]<=a[j]) { s=min(s, st.top().sc); st.pop(); } if(st.empty() || s+a[j]<a[st.top().fi]+st.top().sc) st.emplace(j, s); dp[i][j]=a[st.top().fi]+st.top().sc; } } cout << dp[k][n]; } void sol() { cin >> n >> k; for(int i=1;i<=n;i++) cin >> a[i]; if(n<=100) sub1(); else sub2(); // sub2(); } signed main() { // freopen("divisor.INP", "r", stdin); // freopen("divisor.OUT", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); int t=1; //cin >> t; while(t--) { sol(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...