Submission #1088849

#TimeUsernameProblemLanguageResultExecution timeMemory
1088849Khanhcsp2K blocks (IZhO14_blocks)C++14
53 / 100
3 ms2652 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; dp[0][0]=0; for(int i=1;i<=k;i++) { stack<pii> st; for(int j=i-1;j<=n;j++) { int s=dp[i-1][j]; 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.push({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...