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
#include <bits/stdc++.h>
using namespace std;

int tab[1000009];
bool special[1000009];
//implementacja ordered_set w pbds z https://codeforces.com/blog/entry/11080
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for (int i = 1; i<=n; i++) {
        cin>>tab[i];
    }
    set<int> back;
    set<int> front;
    int biggest = 0;
    ordered_set X;
    for (int i = 1; i<=n; i++) {
        if (tab[i] > biggest) {
            biggest = tab[i];
            front.insert(tab[i]);
            X.insert(tab[i]);
            special[i] = true;
        }
    }
    int ans = front.size();
    for (int split = n; split>=1; split--) {
        if (special[split]) {
            front.erase(tab[split]);
            X.erase(tab[split]);
        }
        back.erase(back.begin(), back.upper_bound(tab[split]));
        back.insert(tab[split]);
        int val = *back.rbegin()+1;
        int s =  X.size() - X.order_of_key(val);
        ans = max(ans, (int)(back.size() + s));
    }
    cout<<ans<<"\n";
}