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
#include <iostream>

using namespace std;

int median(int n, int num_of_thrown_away) {
    return (n + num_of_thrown_away + 1) / 2;
}

int main() {
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;

    int tab[n], prefix_max[n], suffix_max[n];
    int prefix_at_least_position[n + 1], suffix_at_least_position[n + 1];

    for (auto i = 0; i < n; i++) {
        cin >> tab[i];
    }

    prefix_max[0] = tab[0];
    suffix_max[n - 1] = tab[n - 1];

    for (auto i = 1; i < n; i++) {
        prefix_max[i] = max(prefix_max[i - 1], tab[i]);
        suffix_max[n - i - 1] = max(suffix_max[n - i], tab[n - i - 1]);
    }


    int j = 1;
    for (auto i = 1; i <= n; i++) {
        while (prefix_max[i - 1] > j && j <= n) {
            prefix_at_least_position[j] = i;
            j++;
        }
    }

    j = 1;
    for (auto i = n - 1; i >= 0; i--) {
        while (suffix_max[i] > j && j <= n) {
            suffix_at_least_position[j] = n - i;
            j++;
        }
    }

    prefix_at_least_position[n] =  n + 1;
    suffix_at_least_position[n] = n + 1;
    prefix_at_least_position[0] = 0;
    suffix_at_least_position[0] = 0;

    long long res = 1; // add whole sequence
    int pref_range, suf_range;
    int m;

//    for (auto i = 0; i < n; i++) {
//        cout << prefix_max[i] << " ";
//    }
//
//    cout << endl;
//
//    for (auto i = 1; i <=n; i++) {
//        cout << prefix_at_least_position[i] << " ";
//    }
//
//    cout << endl;
//
//    for (auto i = 0; i < n; i++) {
//        cout << suffix_max[i] << " ";
//    }
//
//    cout << endl;
//
//    for (auto i = 1; i <=n; i++) {
//        cout << suffix_at_least_position[i] << " ";
//    }

    for (auto i = 1; i <= n; i++) {
        m = median(n, i);
        pref_range = min(prefix_at_least_position[m - 1] - 1, i);
        suf_range = min(suffix_at_least_position[m - 1] - 1, i);

//        if (max(0, pref_range + suf_range - i + 1) > 0) {
//            cout << "i: " << i << " median: " << m << " pref_range: " << pref_range << " suf_range: " << suf_range << " add: " <<  max(0, pref_range + suf_range - i + 1) << endl;
//        }

        res += max(0, pref_range + suf_range - i + 1);
    }

    cout << 2 * n + 1 << " " << res << endl;


    return 0;
}