zoukankan      html  css  js  c++  java
  • codeforces 676A A. Nicholas and Permutation(水题)

    题目链接:

    A. Nicholas and Permutation

    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Examples
     
    input
    5
    4 5 1 3 2
    output
    3
    input
    7
    1 6 5 3 4 7 2
    output
    6
    input
    6
    6 5 4 3 2 1
    output
    5

    题意:

    交换这个序列里面的两个数使最大的数和最小的数的距离最大,问这个最大的距离是多少;

    思路:

    把最大的和最小的分别和第一个和最后一个交换,最大的就是答案了;

    AC代码:

    #include <bits/stdc++.h>
    /*
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <map>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    */
    using namespace std;
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef long long LL;
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=0x3f3f3f3f;
    const int N=1e5+4;
    int n;
    int a[110],flag[110];
    int main()
    {
           scanf("%d",&n);
           Riep(n)
           {
               scanf("%d",&a[i]);
               flag[a[i]]=i;
           }
           int ans=0;
               ans=max(ans,abs(n-flag[1]));
               ans=max(ans,abs(flag[n]-1));
               ans=max(ans,abs(n-flag[n]));
               ans=max(ans,abs(flag[1]-1));
           cout<<ans<<"
    ";
    
    
    
        return 0;
    }
  • 相关阅读:
    201671010131 2016-2017-2 《Java程序设计》逐渐的进步。
    201671010131 2016-2017-2 《Java程序设计》走向核心。
    201671010131 2016-2017-2 《Java程序设计》艰难的旅程.
    201671010131 2016-2017-2 《Java程序设计》第二周 由简入繁的开始。
    201671010131 2016-2017-2 《Java程序设计》初学Java,所感所学总结。
    ajax提交请求数组时,参数名带[]
    天气接口-高德api
    Lambda 表达式在线程中的使用
    Centos7 下安装和配置 MinDoc
    天气接口 乱码问题
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5539271.html
Copyright © 2011-2022 走看看