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
#include <iostream>
#include <vector>
#include <set>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> data(n);
    vector<int> place(n +1);
    set<int> included;

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

    int median = (n + 1)/ 2;

    int ni = 0;
    while (data[ni] != n)ni++;
    //included.insert(n);

    int i = ni;
    int j = ni;
    int ans = 1;

    int nexti;
    int l = 0;
    int left;
    int right;
    for (int k = 2; k <= n; k++) {  
        if (k % 2 == 0) {
            nexti = place[n - k / 2];
            //if (nexti <= j && nexti >= i)continue;
            if (nexti < i) i = nexti;
            else if (nexti > j) j = nexti;
        }
        l = k - (j - i + 1) + 1;
        left = i + 1;
        right = n - j;
        if (l > left) l = left;
        if (l > right) l = right;
        l = l > n - k + 1 ? n - k + 1: l;
        if (l > 0) {
            ans += l;
            //cout << "k: " <<k<< " l: "<< l << '\n';
        }
    }

    cout << 2*n + 1 <<" "<< ans;

    return 0;
}