#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define input_init stringstream ss; string strtoken, token; istringstream is #define input_line getline(cin, strtoken);is.str(strtoken);is.clear(istringstream::goodbit) #define input_token(num) ss.str(""); ss.clear(stringstream::goodbit); getline(is, token, ','); ss << token; ss >> num vector> tmp(4,vector(3)); namespace seg_tree{ typedef pair P; #define st(x) (x).first #define ed(x) (x).second #define A_MAX 1000010 #define A_MIN 0 struct seg_Tree{ P interval; int val[2][3]; int size(){ return(ed(interval) - st(interval) + 1); } void set(int s, int t){ st(interval) = s, ed(interval) = t; } seg_Tree(){ set(1, 0); } }; void make_tree(seg_Tree* tree,int k, int s, int t){ if (s <= t){ tree[k].set(s, t); if (tree[k].size() > 1){ int l = 2 * k + 1; int r = l + 1; make_tree(tree,l, s, (s + t) / 2); make_tree(tree,r, (s + t) / 2 + 1, t); } } for (int i = 0; i < 2; i++){ int* it = tree[k].val[i]; it[0] = A_MIN; it[1] = st(tree[k].interval); it[2] = -1; } return; } void construct_tree(seg_Tree* tree){ make_tree(tree,0, A_MIN, A_MAX - 1); } P find(seg_Tree* tree,int k, P len,int p){ if (ed(len) < st(len))return make_pair(-A_MAX, -1); int l = 2 * k + 1; int r = l + 1; if (tree[k].interval == len){ if (tree[k].val[0][2]!=p) return make_pair(tree[k].val[0][0],tree[k].val[0][1]); else return make_pair(tree[k].val[1][0], tree[k].val[1][1]); } else if (ed(len) <= ed(tree[l].interval)) return find(tree,l, len,p); else if ((st(len) >= st(tree[r].interval))) return find(tree,r, len,p); else{ P L = find(tree,l, make_pair(st(len), ed(tree[l].interval)),p); P R=find(tree,r, make_pair(st(tree[r].interval), ed(len)),p); return max(L,R); } } seg_Tree add(seg_Tree* tree, int k, int key, P val,P prev){ if (tree[k].size() > 1){ int l = 2 * k + 1; int r = l + 1; seg_Tree rec; if (key <= ed(tree[l].interval)) rec = add(tree, l, key, val,prev); else rec = add(tree, r, key, val,prev); for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++){ tmp[i][j] = tree[k].val[i][j]; tmp[i + 2][j] = rec.val[i][j]; } sort(tmp.begin(), tmp.end()); for (int j = 0; j < 3; j++)tree[k].val[0][j] = tmp[3][j]; int i = 2; while (tree[k].val[0][0] == tmp[i][0] && tree[k].val[0][1] == tmp[i][1] && tree[k].val[0][2] == tmp[i][2])i--; for (int j = 0; j < 3; j++)tree[k].val[1][j] = tmp[i][j]; } else{ tree[k].val[0][1] = tree[k].val[1][1] = key; tree[k].val[0][0] = val.first; tree[k].val[1][0] = val.second; tree[k].val[0][2] = prev.first; tree[k].val[1][2] = prev.second; } return tree[k]; } } using namespace seg_tree; seg_Tree dp[2][A_MAX*3]; int main(){ construct_tree(dp[0]); construct_tree(dp[1]); int N; cin >> N; add(dp[0], 0, A_MIN, make_pair(0, -A_MAX), make_pair(A_MIN-1, A_MIN - 2)); add(dp[1], 0, A_MAX - 1, make_pair(0, -A_MAX), make_pair(A_MAX-1, 1 )); for (int i = 0; i < N; i++){ int A; cin >> A; P maxi1, mini1,maxi2,mini2; maxi1 = find(dp[1], 0, make_pair(A + 1, A_MAX - 1),A); mini1 = find(dp[0], 0, make_pair(A_MIN, A - 1),A); // printf("%d,%d %d,%d\n", A + 1, maxi1.second - 1, maxi1.second + 1, A_MAX - 1); maxi2 = max(find(dp[1], 0, make_pair(A + 1, maxi1.second-1), A), find(dp[1], 0, make_pair(maxi1.second+1, A_MAX - 1), A)); // printf("%d,%d %d,%d\n", A_MIN, mini1.second - 1, mini1.second + 1, A - 1); mini2 = max(find(dp[0], 0, make_pair(A_MIN, mini1.second - 1), A), find(dp[0], 0, make_pair(mini1.second + 1, A - 1), A)); add(dp[0], 0, A, make_pair(maxi1.first+1, maxi2.first+1), make_pair(maxi1.second, maxi2.second)); add(dp[1], 0, A, make_pair(mini1.first+1, mini2.first+1), make_pair(mini1.second, mini2.second)); } P res=max(find(dp[0], 0, make_pair(A_MIN, A_MAX - 1),A_MAX), find(dp[1], 0, make_pair(A_MIN, A_MAX - 1),A_MAX)); cout << (res.first < 3 ? 0 : res.first) << endl; }