1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <iostream> #include <cstdio> #include <cstring> #define MAXN (1000000 + 5) #define INF (2000000000) #define lowbit(x) ((x) & (-(x))) #define LS(dq) ((dq) << 1) #define RS(dq) (((dq) << 1) | 1) #define rint register int #define cint const int using namespace std; struct node { int le, ri, zh; }b1[MAXN << 2], b2[MAXN << 2]; int n, a[MAXN], f[MAXN]; inline int max(cint x, cint y) { return (x > y) ? x : y; } inline int min(cint x, cint y) { return (x < y) ? x : y; } void js(node* b, cint dq, cint le, cint ri) { b[dq].le = le, b[dq].ri = ri; b[dq].zh = INF; if (le == ri) return ; cint mi = (le + ri) >> 1; js(b, LS(dq), le, mi); js(b, RS(dq), mi + 1, ri); } void xg(node* b, cint dq, cint wz, cint zh) { if (b[dq].le == b[dq].ri) { b[dq].zh = min(zh, b[dq].zh); return ; } cint mi = (b[dq].le + b[dq].ri) >> 1; if (wz <= mi) xg(b, LS(dq), wz, zh); else xg(b, RS(dq), wz, zh); b[dq].zh = min(b[LS(dq)].zh, b[RS(dq)].zh); } int cx(node* b, cint dq, cint le, cint ri) { if (b[dq].le == le && b[dq].ri == ri) return b[dq].zh; cint mi = (b[dq].le + b[dq].ri) >> 1; if (ri <= mi) return cx(b, LS(dq), le, ri); else if (le > mi) return cx(b, RS(dq), le, ri); else return min(cx(b, LS(dq), le, mi), cx(b, RS(dq), mi + 1, ri)); } void solve() { ++n; js(b1, 1, 1, n), js(b2, 1, 1, n); xg(b1, 1, n, n), xg(b2, 1, n, 1); for (rint i = n - 1; i >= 1; i--) { if (i + a[i] + 1 <= n) f[i] = min(cx(b1, 1, i + a[i] + 1, n) - i - a[i] - 1, cx(b2, 1, i + 1, i + a[i] + 1) + i + a[i] - n); else f[i] = cx(b2, 1, i + 1, n) + i + a[i] - n; xg(b1, 1, i, f[i] + i);
xg(b2, 1, i, f[i] + n - i + 1);
} } inline int read() { int re = 0; char x = 0; while (x > '9' || x < '0') x = getchar(); while (x <= '9' && x >= '0') re = (re << 1) + (re << 3) + x - '0', x = getchar(); return re; } int main() { freopen("data.in", "r", stdin); n = read(); for (rint i = 1; i <= n; i++) a[i] = read(); solve(); printf("%d", f[1]); return 0; }
|