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.
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.
Print a single integer — the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap.
5 4 5 1 3 2
3
7 1 6 5 3 4 7 2
6
6 6 5 4 3 2 1
5
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<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 105, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n;
int a[N], p[N];
int main()
{
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; ++i)scanf("%d", &a[i]), p[a[i]] = i;
int ans = abs(p[n] - p[1]);
gmax(ans, p[n] - 1);
gmax(ans, p[1] - 1);
gmax(ans, n - p[n]);
gmax(ans, n - p[1]);
printf("%d\n", ans);
}
return 0;
}
/*
【题意】
给你一个全排列,恰好交换一次,问maxabsdis(pos[n]-pos[1])
【类型】
贪心
【分析】
直接贪心,
我们肯定交换的位置是与1和n有关的,
然后肯定交换到位置1或n
于是暴力枚举一下5(其实是4)种情况即可
*/