1. 程式人生 > >Codeforces 比賽程式碼記錄及心得

Codeforces 比賽程式碼記錄及心得

  Codeforces Round #354 (Div. 2)

  紀念一下自己差勁的適應能力,cf,要鍛鍊自己,適應能力很關鍵!!!

A. Nicholas and Permutation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

Nicholas has an arrayathat containsndistinctintegers from1ton. In other words, Nicholas has a permutation of sizen

.

Nicholas want the minimum element (integer1) and the maximum element (integern) to be as far as possible from each other. He wants to perform exactly one swap in order to maximize the distance between the minimum and the maximum elements. The distance between two elements is considered to be equal to the absolute difference between their positions.

Input

The first line of the input contains a single integern(2 ≤ n ≤ 100) — the size of the permutation.

The second line of the input containsndistinct integersa1, a2, ..., an(1 ≤ ai ≤ n), whereaiis equal to the element at thei-th position.

Output

Print a single integer — the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap.

Examples input
5
4 5 1 3 2
output
3
input
7
1 6 5 3 4 7 2
output
6
input
6
6 5 4 3 2 1
output
5
Note

In the first sample, one may obtain the optimal answer by swapping elements1and2.

In the second sample, the minimum and the maximum elements will be located in the opposite ends of the array if we swap7and2.

In the third sample, the distance between the minimum and the maximum elements is already maximum possible, so we just perform some unnecessary swap, for example, one can swap5and2.


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
    int n,a[105];
    scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int maxn=1,minn=1;
        for(int i=2;i<=n;i++)
        {
            if(a[i]>a[maxn])
                maxn=i;
            if(a[i]<a[minn])
                minn=i;
        }
        if(maxn>minn)
        {
            if(n-maxn>minn-1)
                printf("%d\n",n-minn);
            else
                printf("%d\n",maxn-1);
        }
        else
        {
             if(maxn-1>n-minn)
                printf("%d\n",minn-1);
             else
                printf("%d\n",n-maxn);
        }
    return 0;
}