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<stdio.h>
int max(int a, int b){return a>b?a:b;}
int min(int a, int b){return a<b?a:b;}
int dp[101][100001];
int IT[300001];
int IT2[300001];
int b = 1;
int a[100001];
int bb[100001];
int getmin(int f, int r);
int main()
{
int n, k;
int i, j;
scanf("%d%d", &n, &k);
for(b=1; b<=n; b*=2);
for(i=1; i<=n; i++) scanf("%d", a+i);
int stk[100001], top=-1;
for(i=1; i<=n; i++)
{
while(top != -1 && a[stk[top]] < a[i])top--;
if(top == -1) bb[i] = -1;
else bb[i] = stk[top];
stk[++top] = i;
}
for(i=1; i<=n; i++) dp[1][i] = max(a[i], dp[1][i-1]);
for(i=2; i<=k; i++)
{
for(j=1; j<=n; j++) IT[b+j] = dp[i-1][j];
for(j=b; j>=1; j--) IT[j] = min(IT[j*2], IT[j*2+1]);
for(j=1; j<=2*b; j++) IT2[j] = 200000000;
for(j=1; j<i; j++) dp[i][j] = 200000000;
for(j=i; j<=n; j++)
{
if(bb[j] == -1) dp[i][j] = getmin(i-1, j-1) + a[j];
else
{
if(bb[j] < i)
{
dp[i][j] = getmin(i-1, j-1) + a[j];
}
else
{
dp[i][j] = min(getmin(bb[j], j-1) + a[j], dp[i][j-1]);
}
}
}
}
printf("%d", dp[k][n]);
return 0;
}
int getmin(int f, int r)
{
f+=b; r+=b;
int re = 200000000;
while(f<r)
{
if(f%2 == 1) re = min(re, IT[f++]);
if(r%2 == 0) re = min(re, IT[r--]);
f/=2; r/=2;
}
if(f==r) re = min(re, IT[f]);
return re;
}
# | 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... |