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

    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
    Note

    In the first sample, one may obtain the optimal answer by swapping elements 1 and 2.

    In the second sample, the minimum and the maximum elements will be located in the opposite ends of the array if we swap 7 and 2.

    In the third sample, the distance between the minimum and the maximum elements is already maximum possible, so we just perform some unnecessary swap, for example, one can swap 5 and 2.

    这场是掉分最严重的一场  woccccccccccccccccc

    题意:一串序列  只能交换两个数字的位置一次  使得 最大值与最小值的距离最大 输出最大值

    题解:枚举 将最大最小值放到1位置或n位置 (水)

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n;
     4 int a[105];
     5 int minx ,maxn,pos1,pos2;
     6 int ans;
     7 int main()
     8 {
     9     scanf("%d",&n);
    10     minx=1000;
    11     maxn=-1;
    12     for(int i=1;i<=n;i++)
    13     {
    14         scanf("%d",&a[i]);
    15         if(a[i]>maxn)
    16         {
    17          maxn=a[i];
    18          pos1=i;
    19         }
    20         if(a[i]<minx)
    21         {
    22            minx=a[i];
    23            pos2=i;
    24         }
    25     }
    26     int ans1,ans2,ans;
    27     ans1=max(pos1-1,n-pos1);
    28     ans2=max(pos2-1,n-pos2);
    29     ans=max(ans1,ans2);
    30     ans=max(ans,abs(pos2-pos1));
    31     cout<<ans<<endl;
    32     return 0; 
    33 } 
  • 相关阅读:
    【WinHec启示录】透过Windows 10技术布局,谈微软王者归来
    管中窥豹,物联网之我见
    微软借力.NET开源跨平台支持,布局物联网平台开发
    面向对象开发方式的开源硬件--.NET Gadgeteer
    【物联网智能网关-18】多通道远程安全升级
    vim 多文件编辑【超实用】
    debian下samba配置
    制作根文件系统的经验
    c语言: inline(gcc)
    Cramfs、JFFS2、YAFFS2的全面对比
  • 原文地址:https://www.cnblogs.com/hsd-/p/5536243.html
Copyright © 2011-2022 走看看