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

int n;
int tab[1000100];
int ind[1000100];

int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>tab[i];
        ind[tab[i]]=i;
    }

    int left=10001000;
    int right=-2;
    long long wynik=0;

    for(int i=n;i>0;i--)
    {
        int k=(i+n)/2;
        left=min(left,ind[k]);
        right=max(right,ind[k]);
        if(right-left+1<=n-i+1)
        {
            int a=left-1;
            int b=n-right;
            int m=n-i+left-right;

            int X=a+b-m+1;
            int A=max(0,a-m);
            int B=max(0,b-m);

            wynik+=X-A-B;
        }
      //  cout<<left<<" "<<right<<" "<<wynik<<endl;
    }

    cout<<2*n+1<<" "<<wynik;
}