Codeforces Round #354 (Div. 2) A. Nicholas and Permutation
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.
Sample Input
5
4 5 1 3 2
Sample Output
3
题意
给你一个n的排列,然后你可以改变一次这个排列的位置
你需要使得最小的数和最大的数距离最大,问你这个距离是多少
题解:
把最小的数扔到边上,或者把最大的数扔到边上
代码:
#include<bits/stdc++.h> using namespace std; int a[105]; int main() { int n,pos1,pos2; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); if(a[i]==1) pos1=i; if(a[i]==n) pos2=i; } int dis1=max(n-pos1,pos1-1); int dis2=max(n-pos2,pos2-1); if(dis1>dis2) cout<<dis1; else cout<<dis2; return 0; }