# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1101530 | ngongocbich | K blocks (IZhO14_blocks) | C++17 | 48 ms | 262144 KiB |
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 int long long
#define mask(k) (1<<(k))
using namespace std;
int n,k,a[1000001],dp[1000001][101],l[1000001],mx[1001][21];
stack<int> st;
int getmn(int l, int r)
{
int p=__lg(r-l+1);
return min(mx[l][p],mx[r-mask(p)+1][p]);
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
freopen("w.inp","r",stdin);
freopen("w.out","w",stdout);
cin>>n>>k;
for (int i=1; i<=n; i++) cin>>a[i],mx[i][0]=0x3f;
for (int j=1; mask(j)<=n; j++)
for (int i=1; i+mask(j)-1<=n; i++)
mx[i][j]=min(mx[i][j-1],mx[i+mask(j-1)][j-1]);
for (int i=1; i<=n; i++)
{
while (!st.empty() && a[st.top()]<=a[i]) st.pop();
if (!st.empty()) l[i]=st.top(); else l[i]=0;
st.push(i);
}
memset(dp,0x3f,sizeof(dp));
dp[0][0]=0;
for (int i=1; i<=k; i++)
for (int j=1; j<=n; j++)
{
dp[i][j]=min(dp[i][j],a[j]+getmn(l[j],j-1));
dp[i][j]=min(dp[i][j],dp[i][l[j]]);
}
cout<<dp[k][n];
return 0;
}
Compilation message (stderr)
# | 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... |