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

using namespace std;
typedef long long int ll;

struct Rating
{
    int val, idx;
};

bool cmpRatings(const Rating& a, const Rating& b)
{
    return a.val > b.val;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin >> n;
    
    vector<Rating> a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i].val;
        a[i].idx = i;
    }
    sort(a.begin(), a.end(), cmpRatings);
    
    ll res = 1;
    int L, R, largestCnt = 1;
    L = R = a.front().idx;
    for (int i = 1; i < n; ++i)
    {
        L = min(L, a[i].idx);
        R = max(R, a[i].idx);
        ++largestCnt;
        
        while (i + 1 < n && a[i + 1].idx >= L && a[i + 1].idx <= R)
        {
            ++largestCnt;
            ++i;
        }
      
        int len = R - L + 1;
        int cntOnSides = 2 * largestCnt - len - 1;
        if (cntOnSides <= 0)
        {
            res += cntOnSides == 0;
            continue;
        }
     
        ll total = ((1LL + cntOnSides) * (2LL + cntOnSides)) / 2LL;
        
        int leftMinIdx = L - cntOnSides;
        leftMinIdx = max(leftMinIdx, 0);
        if (i + 1 < n && a[i + 1].idx <= L)
            leftMinIdx = max(leftMinIdx, a[i + 1].idx + 1);
        int leftMaxCnt = L - leftMinIdx;
        
        int rightMaxIdx = R + cntOnSides;
        rightMaxIdx = min(rightMaxIdx, n - 1);
        if (i + 1 < n && a[i + 1].idx >= R)
            rightMaxIdx = min(rightMaxIdx, a[i + 1].idx - 1);
        int rightMaxCnt = rightMaxIdx - R;
        
        int left = cntOnSides - leftMaxCnt;
        ll invalidLeft = ((1LL + left) * left) / 2LL;
        int right = cntOnSides - rightMaxCnt;
        ll invalidRight = ((1LL + right) * right) / 2LL;
        int both = cntOnSides - leftMaxCnt - rightMaxCnt;
        ll common = 0;
        if (both > 0)
            common = ((ll)both * ((ll)both - 1LL)) / 2LL;
        res += total - invalidLeft - invalidRight + common;
    }

    cout << 2 * n + 1 << " " << res << "\n";
    return 0;
}