/* -*- coding: utf-8 -*- * * 1188.cc: No.1188 レベルX門松列 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int INF = 1 << 30; /* typedef */ /* global variables */ int as[MAX_N], bs[MAX_N], dp[MAX_N]; int ls0[MAX_N], ls1[MAX_N], ls2[MAX_N], ls3[MAX_N]; /* subroutines */ int lis(int n, int as[], int ids[]) { fill(dp, dp + n, INF); int m = 0; for (int i = 0; i < n; ++i) { ids[i] = lower_bound(dp, dp + n, as[i]) - dp; dp[ids[i]] = as[i]; if (m < ids[i]) m = ids[i]; } return m + 1; } /* main */ int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", as + i), bs[i] = -as[i]; lis(n, as, ls0); lis(n, bs, ls1); reverse(as, as + n), reverse(bs, bs + n); lis(n, as, ls2); lis(n, bs, ls3); int maxl = 0; for (int i = 0; i < n; i++) { int l = max(min(ls0[i], ls2[n - 1 - i]), min(ls1[i], ls3[n - 1 - i])); if (maxl < l) maxl = l; } printf("%d\n", maxl); return 0; }