【Codeforces Round 354 (Div 2)A】【水题 贪心】Nicholas and Permutation 恰好交换一次的pos[n]-pos[1]

原创 2016年05月27日 13:18:52

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.


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 105, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n;
int a[N], p[N];
int main()
{
	while (~scanf("%d", &n))
	{
		for (int i = 1; i <= n; ++i)scanf("%d", &a[i]), p[a[i]] = i;
		int ans = abs(p[n] - p[1]);
		gmax(ans, p[n] - 1);
		gmax(ans, p[1] - 1);
		gmax(ans, n - p[n]);
		gmax(ans, n - p[1]);
		printf("%d\n", ans);
	}
	return 0;
}
/*
【题意】
给你一个全排列,恰好交换一次,问maxabsdis(pos[n]-pos[1])

【类型】
贪心

【分析】
直接贪心,
我们肯定交换的位置是与1和n有关的,
然后肯定交换到位置1或n
于是暴力枚举一下5(其实是4)种情况即可

*/


版权声明:题解中哪里写错请一定要指出来QwQ 转载还请注明下出处哦,谢谢^_^ https://blog.csdn.net/snowy_smile/article/details/51514480

Codeforces Round #354 (Div. 2)(A && B && C)

Codeforces Round #354 (Div. 2) A, B, C解题报告
  • qingshui23
  • qingshui23
  • 2016-06-21 18:47:15
  • 726

CodeForces - 676A

Nicholas has an array a that contains n distinct integers from 1 to n. In other words, Nicholas ha...
  • liyuanshuo_nuc
  • liyuanshuo_nuc
  • 2017-03-05 15:39:51
  • 72

Codeforces Round #354 (Div. 2)

Codeforces Round #354 (Div. 2)
  • queuelovestack
  • queuelovestack
  • 2016-05-26 09:32:04
  • 2697

【Codeforces Round 354 (Div 2)A】【水题 贪心】Nicholas and Permutation 恰好交换一次的pos[n]-pos[1]

A. Nicholas and Permutation time limit per test 1 second memory limit per test 256 megab...
  • snowy_smile
  • snowy_smile
  • 2016-05-27 13:18:52
  • 554

codeforces 676A Nicholas and Permutation

A. Nicholas and Permutation time limit per test 1 second memory limit per test 256 megabytes inpu...
  • EventQueue
  • EventQueue
  • 2016-05-26 21:36:44
  • 208

ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) C. Permutation Cycle

C. Permutation CycleFor a permutation P[1... N] of integers from 1 to N, function f is defined as fo...
  • s540239976
  • s540239976
  • 2018-02-23 21:38:43
  • 115

Codeforces #364(Div.2)A.Card【贪心】【水题】

A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input...
  • mengxiang000000
  • mengxiang000000
  • 2016-07-23 13:20:42
  • 296

Coderforces 354Div2

A. Nicholas and Permutation time limit per test:1 second memory limit per test:256 megabytesNich...
  • huayunhualuo
  • huayunhualuo
  • 2016-06-23 13:25:25
  • 338

Codeforces Round #354 (Div. 2) A (水题)

A. Nicholas and Permutation time limit per test 1 second memory limit per test 256 megab...
  • liangzhaoyang1
  • liangzhaoyang1
  • 2016-05-26 22:28:42
  • 350

CodeForces - 676A Nicholas and Permutation (模拟) 水

 CodeForces - 676A Nicholas and Permutation Time Limit: 1000MS   Memory Limit: 26...
  • yanghui07216
  • yanghui07216
  • 2016-06-13 10:47:17
  • 461
收藏助手
不良信息举报
您举报文章:【Codeforces Round 354 (Div 2)A】【水题 贪心】Nicholas and Permutation 恰好交换一次的pos[n]-pos[1]
举报原因:
原因补充:

(最多只允许输入30个字)