Description
给你一个1→n1\rightarrow n1→n的排列,现在有一次机会可以交换两个数的位置,求交换后最小值和最大值之间的最大距离是多少?
Input
第一行一个数nnn
之后一行nnn个数表示这个排列
(1≤n≤100)(1\le n\le 100)(1≤n≤100)
Output
输出一行一个数表示答案
Sample Input
5
4 5 1 3 2
Sample Output
3
Solution
四种情况,把最值移动到两端,比较一下取最大值即可
Code
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int n,p1,pn;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
if(x==1)p1=i;
if(x==n)pn=i;
}
printf("%d\n",max(max(p1-1,n-p1),max(pn-1,n-pn)));
return 0;
}