Nicholas and Permutation

来源:互联网 发布:华为算法工程师好进吗 编辑:程序博客网 时间:2024/06/12 01:17
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 array a that contains n distinct integers from 1 to n. In other words, Nicholas has a permutation of sizen.

Nicholas want the minimum element (integer 1) 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 integer n (2 ≤ n ≤ 100) — the size of the permutation.

The second line of the input contains n distinct integersa1, a2, ..., an (1 ≤ ai ≤ n), where ai is 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
54 5 1 3 2
Output
3
Input
71 6 5 3 4 7 2
Output
6
Input
66 5 4 3 2 1
Output
5
Note

In the first sample, one may obtain the optimal answer by swapping elements 1 and 2.

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

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 swap5 and 2.


showing code:

#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;struct node{    int v;    int p;};node arr[110];int main(){    int n;    int d = 0;    node ma, mi;    while (scanf("%d", &n) != EOF)    {        d = 0;        for (int i=0; i<n; i++)        {            arr[i].p = i;            scanf("%d", &arr[i].v);        }        mi.v = ma.v = arr[0].v;        mi.p = ma.p = arr[0].p;        for (int i=1; i<n; i++)        {            if (ma.v < arr[i].v)            {                ma.v = arr[i].v;                ma.p = arr[i].p;            }            if (mi.v > arr[i].v)            {                mi.v = arr[i].v;                mi.p = arr[i].p;            }        }        ma.p += 1;        mi.p += 1;        d = abs(ma.p - mi.p);        d = max (abs(ma.p - n), d);        d = max (abs(mi.p - n), d);         d = max (abs(ma.p - 1), d); //比赛时这里的ma写成了mi, 居然还可以过预测, 多么戏剧性的一幕呀        d = max (abs(mi.p - 1), d);        printf("%d\n", d);    }    return 0;}我为什么总犯低级错误呀~~~5555~

0 0
原创粉丝点击