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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <utility>
#include <cmath>
#include <set>
#define MAX 1000003

using namespace std;

int n;
int a[MAX];
int perm[MAX];

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

int bf_test(int r, int l){
    vector<int> sa;
    for (int i = r; i <= l; i++)
    {
        sa.push_back(a[i]);
    }
    sort(sa.begin(), sa.end());
    int size = l-r+1;
    if(size%2==0){
        return sa[size/2] + sa[size/2 - 1] + size;
    }
    return sa[size/2] * 2 + size;
}

pair<int, int> bf(){
    int max = 0;
    int rep = 0;
    for (int l = 0; l < n; l++)
    {
        for (int r = l; r < n; r++)
        {
            int rt = bf_test(l,r);
            //cout << rt << ' ';
            if(rt == max) rep++;
            else if(rt > max){
                max = rt;
                rep = 1;
            }
        }
        //cout << '\n';
    }
    return make_pair(max, rep);
}

void gen_data(){
    n = MAX;
    for (int i = 0; i < n; i++)
    {
        a[i] = i + 1;
    }

    for (int i = 0; i < n-1; i++)
    {
        int j = rand() % (n-1-i) + i + 1;
        int tmp = a[j];
        a[j] = a[i];
        a[i] = tmp;
    }
}

pair<int, int> alg(){
    if(n == 1) return make_pair(3, 1);
    for (int i = 0; i < n; i++)
    {
        perm[a[i]-1] = i; 
    }
    int b = 0, e = n-1, res = 2, 
    cm = (n+1)/2, maxi = perm[n-1];

    for (int i = 1; i < cm; i++)
    {
        int ci = perm[i-1];
        if(ci == e) {
            do{
                e--;
            }while(a[e] < i);
        }
        else if(ci == b){
            do{
                b++;
            }while(a[b] < i);
        }
    }

    for (int i = n-1; i > 1; i--)
    {
        if(i%2 == 1){
            int ci = perm[cm-1];
            if(ci == e) {
                do{
                    e--;
                }while(a[e] < cm);
            }
            else if(ci == b){
                do{
                    b++;
                }while(a[b] < cm);
            }
            cm++;
        }

        res += max(min(min(min(b+1,n-e),n-i+1),i-e+b),0);
    }

    return make_pair(n*2+1, res);    
}

set<int> used_numbers;

void all_perm(int x){
    if(x == n){
        pair<int, int> bfs = bf();
        pair<int, int> s = alg();
        if(bfs != s){
            cout << 'F';
            alg();
        }else{
            cout << "OK";
        }
        
        return;
    }

    for (int i = 1; i <= n; i++)
    {
        if(used_numbers.find(i) != used_numbers.end()) continue;
        used_numbers.insert(i);
        a[x] = i;
        all_perm(x+1);
        used_numbers.erase(i);
    }
}

int main()
{
    load_data();
    pair<int, int> q = alg();
    cout << q.first << ' ' << q.second << '\n';

    // for (int i = 1;; i++)
    // {
    //     n = i;
    //     all_perm(0);
    // }
    

    // srand(12);
    // while (true)
    // {
    //     gen_data();
    //     //pair<int, int> bfs = bf();
    //     pair<int, int> s = alg();
    //     cout << "OK";
    //     // if(bfs != s){
    //     //     cout << 'F';
    //     //     alg();
    //     // }else{
    //     //     cout << "OK";
    //     // }
    // }
    
}