题目链接:

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+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e5+;
int n;
int a[],flag[];
int main()
{
scanf("%d",&n);
Riep(n)
{
scanf("%d",&a[i]);
flag[a[i]]=i;
}
int ans=;
ans=max(ans,abs(n-flag[]));
ans=max(ans,abs(flag[n]-));
ans=max(ans,abs(n-flag[n]));
ans=max(ans,abs(flag[]-));
cout<<ans<<"\n"; return ;
}

最新文章

  1. Oracle补全日志(Supplemental logging)
  2. GJM : 常用网站收集 【不断更新中... ... ... 】
  3. Android selecter背景选择器使用
  4. 恢复SharePoint Server 2013 中的“新颖快建视图”
  5. JS 初学正则表达式
  6. meteor 为基础,联合 Apollo + React + React-Router
  7. input相关问题总结
  8. linux根目录下文件夹概览
  9. ER-STUDIO 6.5工具使用帮助的中文翻译
  10. 针对《来用》的NABC分析
  11. 【Android自动化打包】03. APK的数字签名
  12. 转自 Good morning 的几句精辟的话
  13. 排序Tip
  14. Vim简明教程【CoolShell】
  15. 关于Zend Framework 2中 Zend\Session的使用
  16. dos判断系统版本的语句,
  17. c#—get,set访问器的作用
  18. protocol buffer 编码
  19. jstat查看JVM GC情况
  20. 利用奇异值分解(SVD)进行图像压缩-python实现

热门文章

  1. Map排序——按key排序,按value排序
  2. C:冒泡排序
  3. STL中vector容器实现反转(reverse)
  4. Swift学习笔记十
  5. Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造
  6. UVA 12898 And Or 数学暴力
  7. Mongodb集群配置(sharding with replica set)
  8. 终端I/O之综述
  9. 排序命令sort
  10. SignalR的简单实现