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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <bits/stdc++.h>
using namespace std;

typedef long long int LL;

const int MAX_N = 1000005;

int n;
int nums[MAX_N], indexes[MAX_N];

int median2(const vector<int>& vec)
{
    const int halfSize = vec.size()/2;
    if(vec.size()%2 == 0)
    {
        return vec[halfSize-1]+vec[halfSize];
    }
    return 2*vec[halfSize];
}

int calcF(int start, int end)
{
    vector<int> vec(nums+start, nums+end);
    sort(vec.begin(), vec.end());
    return vec.size() + median2(vec);
}

LL solveSlow()
{
    int maxF = 2*n + 1;
    LL res = 0;
    for(int i = 0; i < n; ++i)
    {
        for(int j = i+1; j <= n; ++j)
        {
            if(calcF(i, j) == maxF)
            {
                ++res;
            }
        }
    }

    return res;
}

pair<int, int> extend(const pair<int, int>& range, int newPoint)
{
    if(newPoint >= range.first && newPoint <= range.second)
    {
        return range;
    }
    if(newPoint < range.first)
    {
        return make_pair(newPoint, range.second);
    }
    return make_pair(range.first, newPoint);
}

int len(const pair<int, int>& range)
{
    return range.second-range.first+1;
}

LL solveFast()
{
    for(int i = 0; i < n; ++i)
    {
        indexes[nums[i]] = i;
    }

    int maxF = 2*n + 1;
    LL res = 1;
    pair<int, int> range = make_pair(indexes[n], indexes[n]);

    for(int i = 2; i <= n; ++i)
    {
        //cout << "------" << endl;
        //cout << "i: " << i << endl;

        if(i%2 == 0)
        {
            range = extend(range, indexes[n-(i/2)]);
            //cout << "numToInclude: " << n-(i/2) << ", index: " << indexes[n-(i/2)] << endl;
        }

        const int toAdd = i-len(range);
        //cout << "range: [" << range.first << "; " << range.second << "], toAdd: " << toAdd << endl;
        if(toAdd < 0)
        {
            continue;
        }
        if(toAdd == 0)
        {
            ++res;
            continue;
        }
        int minRest = min(range.first, n-1-range.second);
        int maxRest = max(range.first, n-1-range.second);
        //cout << "minRest: " << minRest << ", maxRest: " << maxRest << endl;

        if(minRest+maxRest < toAdd)
        {
            continue;
        }

        const int actToAdd = max(0, min(toAdd, minRest) - max(0, toAdd-maxRest) + 1);
        res += actToAdd;

        //cout << "minRest: " << minRest << ", actToAdd: " << actToAdd << endl;
    }
    return res;
}

int main()
{
    ios_base::sync_with_stdio(0);

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

    const LL maxF = 2*n + 1;
    //const LL maxFCount = solveSlow();

    //cout << maxF << ' ' << maxFCount << " =? " << solveFast() << endl;
    //cout << maxF << ' ' << maxFCount << endl;
    cout << maxF << ' ' << solveFast() << endl;

    return 0;
}