Submission #13535

#TimeUsernameProblemLanguageResultExecution timeMemory
13535woqja125K 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 getmin2(int f, int r); void set2(int x, int v); 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 dp[i][j] = min(getmin2(i-1, bb[j]-1), getmin(bb[j], j-1) + a[i]); set2(j, dp[i][j]); } } 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; } int getmin2(int f, int r) { f+=b; r+=b; int re = 200000000; while(f<r) { if(f%2 == 1) re = min(re, IT2[f++]); if(r%2 == 0) re = min(re, IT2[r--]); f/=2; r/=2; } if(f==r) re = min(re, IT2[f]); return re; } void set2(int x, int v){for(IT2[x+=b] = v; x/=2;)IT2[x] = min(IT2[x*2], IT2[x*2+1]);}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...