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;
}

转载于:https://www.cnblogs.com/qscqesze/p/5531341.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值