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
| #include <iostream> #include <cstdio> #define maxn 100010 using namespace std;
int n, a[maxn];
#define lc i << 1 #define rc i << 1 | 1 int T[maxn * 4]; inline void maintain(int i) { T[i] = min(T[lc], T[rc]); }
void update(int i, int l, int r, int k, int v) { if (l == r) return (void) (T[i] = v); int m = l + r >> 1; if (k <= m) update(lc, l, m, k, v); else update(rc, m + 1, r, k, v); maintain(i); }
int query(int i, int l, int r, int L, int R) { if (l > R || r < L) return n + 1; if (L <= l && r <= R) return T[i]; int m = l + r >> 1; return min(query(lc, l, m, L, R), query(rc, m + 1, r, L, R)); }
int pre[maxn], b[maxn], last[maxn];
bool ans[maxn], F = 1; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) { pre[i] = b[a[i]]; b[a[i]] = i; last[a[i]] = i; if (a[i] ^ 1) F = 0; } if (F) return puts("1"), 0; for (int i = 1; i <= n; ++i) { update(1, 1, n, a[i], i); int t = query(1, 1, n, 1, a[i] - 1); if (i - pre[i] >= a[i] && query(1, 1, n, 1, a[i] - 1) > pre[i]) ans[a[i]] = 1; } for (int i = 1; i <= n + 2; ++i) if (n + 1 - last[i] >= i && query(1, 1, n, 1, i - 1) > last[i]) ans[i] = 1; for (int i = 1; i <= n + 2; ++i) if (!ans[i]) return printf("%d\n", i), 0; return 0; }
|