zoukankan      html  css  js  c++  java
  • Codeforces Round #354 (Div. 2) A. Nicholas and Permutation

    Nicholas and Permutation
    time limit :
    1 second
    memory limit: 256 megabytes

    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;
    }
  • 相关阅读:
    词法分析器实验报告(JAVA)
    词法编辑器(Java)
    编译原理的那些事
    Discuz7.2 faq.php页面注入漏洞分析
    Discuz7.2 XML漏洞
    Python 爬取广州商学院新闻----测试版
    进程调度
    DOS下的网络管理命令
    DOS批处理实验
    熟悉使用DOS操作命令
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5531792.html
Copyright © 2011-2022 走看看