Submission #1088846

#TimeUsernameProblemLanguageResultExecution timeMemory
1088846Khanhcsp2K blocks (IZhO14_blocks)C++14
53 / 100
4 ms3164 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[101][N], l[N], bit[20][N]; vector<int> dp1, dp2; 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 build() { for(int i=1;i<=n;i++) bit[0][i]=a[i]; for(int j=1;j<=18;j++) { for(int i=1;i+(1<<j)-1<=n;i++) { bit[j][i]=max(bit[j-1][i], bit[j-1][i+(1<<(j-1))]); } } } int calc(int l, int r) { int k=__lg(r-l+1); return max(bit[k][l], bit[k][r-(1<<k)+1]); } void dnc(int l, int r, int lll, int rrr) { if(l>r) return; int mid=(l+r)/2; pii s= {1e18, -1}; for(int i=lll; i<=max(mid, rrr); i++) s=min(s, {dp1[i]+calc(i+1, mid),i}); dp2[mid]=s.fi; int opt=s.sc; dnc(l, mid-1, lll, opt); dnc(mid+1, r, opt, rrr); } void sub2() { dp1.assign(n+1, 1e16); dp1[0]=0; dp2.resize(n+1, 1e16); dp2[0]=0; for(int i=1; i<=k; i++) { dnc(i, n, i-1, n-1); for(int j=1; j<=n; j++) dp1[j]=dp2[j], dp2[j]=1e16; } cout << dp1[n]; } void sol() { cin >> n >> k; for(int i=1;i<=n;i++) cin >> a[i]; build(); 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...