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

using namespace std;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int vectorSize;
    cin >> vectorSize;
    int indexesSize = vectorSize / 2;
    int value = vectorSize * 2 + 1;
    long long answer = vectorSize == 1 ? 1 : 2;
    vector<int> V(vectorSize);
    vector<int> indexes(vectorSize);
    vector<int> dist(vectorSize);
    for (int i = 0; i < vectorSize; i++) {
        cin >> V[i];
        indexes[V[i] - 1] = i;
    }
    int maxInd = indexes[vectorSize - 1];
    int minInd = indexes[vectorSize - 1];
    int lastDist = 0;
    for (int i = vectorSize - 1; i >= 0; i--) {
        int x = abs(indexes[i] - maxInd) + 1;
        int y = abs(indexes[i] - minInd) + 1;
        int toPut = max(max(x, y), lastDist);
        minInd = min(indexes[i], minInd);
        maxInd = max(indexes[i], maxInd);
        dist[i] = toPut;
        lastDist = dist[i];
    }
    int leftInd = indexes[vectorSize - 1];
    int rightInd = indexes[vectorSize - 1];
    for (int i = 2; i < vectorSize; i++) {
        int sufix = ((value - i) / 2) - 1;
        leftInd = min(leftInd, indexes[sufix]);
        rightInd = max(rightInd, indexes[sufix]);
        int distOfSubstring = dist[sufix];
        if (distOfSubstring == i) {
            answer++;
            continue;
        }
        if (distOfSubstring < i) {
            int diff = i - distOfSubstring;
            int howManyLeft = max(0, leftInd - diff);
            int howManyRight = min(vectorSize - 1, rightInd + diff);
            int toAdd = howManyRight - howManyLeft - i + 2;
            answer += toAdd;
        }
    }
    cout << value << " " << answer << endl;
}