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.
这场是掉分最严重的一场 woccccccccccccccccc
题意:一串序列 只能交换两个数字的位置一次 使得 最大值与最小值的距离最大 输出最大值
题解:枚举 将最大最小值放到1位置或n位置 (水)
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int a[105]; 5 int minx ,maxn,pos1,pos2; 6 int ans; 7 int main() 8 { 9 scanf("%d",&n); 10 minx=1000; 11 maxn=-1; 12 for(int i=1;i<=n;i++) 13 { 14 scanf("%d",&a[i]); 15 if(a[i]>maxn) 16 { 17 maxn=a[i]; 18 pos1=i; 19 } 20 if(a[i]<minx) 21 { 22 minx=a[i]; 23 pos2=i; 24 } 25 } 26 int ans1,ans2,ans; 27 ans1=max(pos1-1,n-pos1); 28 ans2=max(pos2-1,n-pos2); 29 ans=max(ans1,ans2); 30 ans=max(ans,abs(pos2-pos1)); 31 cout<<ans<<endl; 32 return 0; 33 }