Submission #13538

#TimeUsernameProblemLanguageResultExecution timeMemory
13538woqja125K blocks (IZhO14_blocks)C++98
0 / 100
0 ms43932 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...