Codeforces Round #354 (Div. 2) A. Nicholas and Permutation 水题
A. Nicholas and Permutation
题目连接:
http://www.codeforces.com/contest/676/problem/A
Description
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
Hint
题意
给你一个n的排列,然后你可以改变一次这个排列的位置
你需要使得最小的数和最大的数距离最大,问你这个距离是多少
题解:
显然就是把最小的数扔到边上,或者把最大的数扔到边上就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 106;
int a[maxn],b[maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int ans1 = 0,ans2 = 0;
for(int i=1;i<=n;i++)
{
if(a[i]==1)ans1=i;
if(a[i]==n)ans2=i;
}
int ans=max(abs(n-ans2),abs(ans2-1));
ans=max(ans,abs(n-ans1));
ans=max(ans,abs(ans1-1));
cout<<ans<<endl;
}