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

using namespace std;

#define ll long long

int main() {
    int n;
    cin >> n;
    vector<int> a(n+1);

    int best = 2*n+1;

    for (int i=0; i<n; ++i)
        cin >> a[i+1];

    vector<int> pos(n+1);
    vector<int> first_at_least(n+1);
    vector<int> last_at_least(n+1);

    for (int i=1; i<=n; ++i)
        pos[a[i]] = i;

    first_at_least[1] = 1;
    for (int i=2; i<=n; ++i) {
        int j=first_at_least[i-1];
        while (a[j] < i)
            j++;
        first_at_least[i] = j;
    }

    last_at_least[1] = n;
    for (int i=2; i<=n; ++i) {
        int j=last_at_least[i-1];
        while (a[j] < i)
            j--;
        last_at_least[i] = j;
    }

    ll cnt = 0;

    for (int i=1; i<=n; ++i)
        if (a[i] >= (n+1)/2) {
            int m=a[i];
            int l = first_at_least[m];
            int r = last_at_least[m];
            int len = 2*(n-m)+1;
            int min_left = max (r - (len - 1), 1);
            int max_right = min (l + len - 1, n);
            int max_left = max_right - (len-1);
            cnt +=  max (max_left - min_left + 1, 0);

            if (a[i]-1 >= (n+1)/2) {
                l = first_at_least[a[i] - 1];
                r = max(pos[a[i]-1], last_at_least[m]);
                len = 2 * (n - m) + 2;
                min_left = max(r - (len - 1), 1);
                max_right = min(l + len - 1, n);
                max_left = max_right - (len - 1);
                cnt += max(max_left - min_left + 1, 0);
            }
        }

    cout << best << " " << cnt << endl;

    return 0;
}