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
// #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    FAST int n, tmp;
    cin >> n;
    if (n == 1)
    {
        cout << "3 1" << endl;
        return 0;
    }
    if (n == 2)
    {
        cout << "5 2" << endl;
        return 0;
    }
    vector<int> a(n + 1);

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

    int mi = a[n], ma = a[n];
    int sz;
    int targetSz = 1;
    int currIdx;
    int miMaSz = 1;
    int total = 1;
    int lHeadroom, rHeadroom;
    for (int i = n - 1; i >= (n + 1) / 2; i--)
    {
        targetSz++;
        currIdx = a[i];

        if (currIdx > ma)
            ma = currIdx;
        else if (currIdx < mi)
            mi = currIdx;

        miMaSz = ma - mi + 1;
        lHeadroom = mi;
        rHeadroom = n - ma - 1;

        if (miMaSz == targetSz)
            total++;
        else if (miMaSz < targetSz)
            total += min(lHeadroom, (targetSz - miMaSz)) + min(rHeadroom, (targetSz - miMaSz)) - (targetSz - miMaSz) + 1;
        targetSz++;
        if (i != ((n + 1) / 2) || n % 2)
            if (miMaSz == targetSz)
                total++;
            else if (miMaSz < targetSz)
                total += min(lHeadroom, (targetSz - miMaSz)) + min(rHeadroom, (targetSz - miMaSz)) - (targetSz - miMaSz) + 1;
    }

    cout << n * 2 + 1 << " " << total << endl;
    return 0;
}