This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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<=min(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, 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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |