• 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 } 
  • 相关阅读:
    如何用redis/memcache做Mysql缓存层?
    孤儿进程和僵尸进程总结
    二叉树的遍历(非递归)
    Linux进程分配内存的两种方式--brk() 和mmap()
    Hbase
    cgroup 分析之CPU和内存部分
    十道海量数据处理面试题与十个方法大总结
    快速定位性能瓶颈,检查出所有资源(CPU、内存、磁盘IO等)的利用率(utilization)、饱和度(saturation)和错误(error)度量,即USE方法
    红黑树
    tcp 两个重要窗口:滑动窗口 和 拥塞窗口
  • 原文地址:https://www.cnblogs.com/hsd-/p/5536243.html
Copyright © 2020-2023  润新知