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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| #include <iostream> #include <cstdio> #include <vector> #include <algorithm>
#define root 1, n, 1 #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1
int n, k; int a[100005]; int up[100005], down[100005]; int res1[100005], cnt1, res2[100005], cnt2; int mx[4000006]; int ans;
struct ST { void Update(int rt) { mx[rt] = std:: max(mx[rt << 1], mx[rt << 1 | 1]); } void Modify(int l, int r, int rt, int p, int v) { if(l == r) { mx[rt] = std:: max(mx[rt], v); return; } int mid = (l + r) >> 1; if(p <= mid) Modify(lson, p, v); else Modify(rson, p, v); Update(rt); } int Query(int l, int r, int rt, int ql, int qr) { if(l >= ql && r <= qr) return mx[rt]; int mid = (l + r) >> 1; if(qr <= mid) return Query(lson, ql, qr); else if(ql > mid) return Query(rson, ql, qr); else return std:: max(Query(lson, ql, qr), Query(rson, ql, qr)); } };
void Up() { for(int i = 1; i <= n; i ++) { if(! cnt1) { res1[++ cnt1] = a[i]; up[i] = i; continue; } int p = std:: upper_bound(res1 + 1, res1 + cnt1 + 1, a[i]) - res1; up[i] = p; res1[p] = a[i]; cnt1 = std:: max(p, cnt1); } }
void Down() { std:: reverse(a + 1, a + n + 1); for(int i = 1; i <= n; i ++) a[i] = -a[i]; for(int i = 1; i <= n; i ++) { if(! cnt2) { res2[++ cnt2] = a[i]; down[i] = 1; continue; } int p = std:: upper_bound(res2 + 1, res2 + cnt2 + 1, a[i]) - res2; down[i] = p; res2[p] = a[i]; cnt2 = std:: max(p, cnt2); } for(int i = 1; i <= n; i ++) a[i] = -a[i]; std:: reverse(a + 1, a + n + 1); std:: reverse(down + 1, down + n + 1); }
void Solve() { ST st; for(int i = n - 1 - k; i >= 1; i --) { st.Modify(root, a[i + k + 1], down[i + k + 1]); ans = std:: max(ans, k + up[i] + st.Query(root, a[i], 1e6)); } }
int main() { scanf("%d%d", &n, &k); for(int i = 1; i <= n; i ++) scanf("%d", &a[i]); if(k == n) { printf("%d", n); return 0; } Up(); Down(); Solve(); printf("%d", ans); return 0; }
|