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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #include <bits/stdc++.h> using namespace std;
const int N = 3e5 + 10;
int n, a[N];
struct Seg_Tree { int mx[N << 2]; void init(int rt = 1, int l = 1, int r = n + 2) { mx[rt] = 0; if(l == r) return; int mid = l + r >> 1; init(rt << 1, l, mid); init(rt << 1 | 1, mid + 1, r); } void pushup(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); } void update(int rt, int l, int r, int pos, int val) { if(l == r) { mx[rt] = max(mx[rt], val); return; } int mid = l + r >> 1; if(pos <= mid) update(rt << 1, l, mid, pos, val); else update(rt << 1 | 1, mid + 1, r, pos, val); pushup(rt); } int query(int rt, int l, int r, int L, int R) { if(L <= l && r <= R) return mx[rt]; int mid = l + r >> 1, ans = 0; if(L <= mid) ans = max(ans, query(rt << 1, l, mid, L, R)); if(R > mid) ans = max(ans, query(rt << 1 | 1, mid + 1, r, L, R)); return ans; } }zero, one;
int dp[2][N][2], len; vector<int> ans[2], all, up[N], down[N];
void init() { len = 0; zero.init(); one.init(); all.clear(); ans[0].clear(); ans[1].clear(); for(int i = 0; i <= n + 1; ++i) up[i].clear(), down[i].clear(); }
int main() { while(~scanf("%d", &n)) { init(); for(int i = 1; i <= n; ++i) { scanf("%d", &a[i]); all.push_back(a[i]); } all.push_back(0); all.push_back(0x3f3f3f3f); sort(all.begin(), all.end()); all.erase(unique(all.begin(), all.end()), all.end()); for(int i = 1; i <= n; ++i) { a[i] = lower_bound(all.begin(), all.end(), a[i]) - all.begin() + 1; } for(int i = 1; i <= n; ++i) { dp[0][i][0] = zero.query(1, 1, n + 2, 1, a[i] - 1) + 1; dp[0][i][1] = max(one.query(1, 1, n + 2, a[i] + 1, n + 2) + 1, dp[0][i][0]); zero.update(1, 1, n + 2, a[i], dp[0][i][0]); one.update(1, 1, n + 2, a[i], dp[0][i][1]); } zero.init(); one.init(); for(int i = n; i; --i) { dp[1][i][0] = zero.query(1, 1, n + 2, 1, a[i] - 1) + 1; dp[1][i][1] = max(one.query(1, 1, n + 2, a[i] + 1, n + 2) + 1, dp[1][i][0]); zero.update(1, 1, n + 2, a[i], dp[1][i][0]); one.update(1, 1, n + 2, a[i], dp[1][i][1]); } for(int i = 1; i <= n; ++i) { len = max(len, dp[0][i][0] + dp[1][i][1] - 1); } for(int i = 1; i <= n; ++i) { assert(len >= dp[0][i][1] + dp[1][i][0] - 1); } for(int i = 1; i <= n; ++i) { if(dp[0][i][0] + dp[1][i][1] - 1 == len) { up[dp[0][i][0]].push_back(i); } if(dp[0][i][1] + dp[1][i][0] - 1 == len) { down[dp[1][i][0]].push_back(i); } } int cur = 0, flag = 0; for(int i = 1; i <= len; ++i) { if(!flag) { vector<int> &v = up[i]; int p = upper_bound(v.begin(), v.end(), cur) - v.begin(); if(p == v.size() || a[v[p]] <= a[cur]) { flag = 1; } else { vector<int> &vv = down[len - i + 1]; int pp = upper_bound(v.begin(), v.end(), cur) - v.begin(); if(pp != vv.size() && a[vv[pp]] < a[cur] && vv[pp] < v[p]) { flag = 1; } else { cur = v[p]; ans[0].push_back(v[p]); } } } if(flag) { vector<int> &v = down[len - i + 1]; int p = upper_bound(v.begin(), v.end(), cur) - v.begin(); assert(a[v[p]] < a[cur]); assert(p < v.size()); cur = v[p]; ans[0].push_back(v[p]); } } cur = flag = 0; for(int i = 1; i <= len; ++i) { if(!flag) { vector<int> &v = up[i]; int p = lower_bound(v.begin(), v.end(), cur, [&](int i, int j){return a[i] > a[j];}) - v.begin() - 1; if(p < 0 || a[v[p]] <= a[cur]) { flag = 1; } else { cur = v[p]; ans[1].push_back(v[p]); } } if(flag) { vector<int> &v = down[len - i + 1]; int p = lower_bound(v.begin(), v.end(), cur, [&](int i, int j){return a[i] < a[j];}) - v.begin() - 1; assert(p >= 0); assert(a[v[p]] < a[cur]); cur = v[p]; ans[1].push_back(v[p]); } } for(int _ = 0; _ < 2; ++_) { for(int i = 0; i < len; ++i) printf("%d%c", ans[_][i], " \n"[i + 1 == len]); } } return 0; }
|