Codeforces 比赛代码记录及心得

来源:互联网 发布:java统计报表怎么做 编辑:程序博客网 时间:2024/05/19 02:19

  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 array a that contains n distinct integers from 1 to n. In other words, Nicholas has a permutation of size n.

Nicholas want the minimum element (integer 1) and the maximum element (integer n) 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 integers a1, a2, ..., an (1 ≤ ai ≤ n), where ai is equal to the element at the i-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 swap 7 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 swap 5 and 2.


#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;}


0 1
原创粉丝点击