【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 转载还请注明下出处哦,谢谢^_^

相关文章推荐

Codeforces Round #354 (Div. 2)

Codeforces Round #354 (Div. 2)

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

A. Nicholas and Permutation time limit per test 1 second memory limit per test 256 megab...

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

http://codeforces.com/problemset/problem/676/A A. Nicholas and Permutation time limit per ...

【Codeforces Round 272 (Div 2)A】【贪心 暴力 水题】Dreamoon and Stairs n个台阶每次走一步或两步是否有步数恰好为m倍数

A. Dreamoon and Stairs time limit per test 1 second memory limit per test 256 megabytes input ...

【Codeforces Round 336 (Div 2) A】【贪心 水题】Saitama Destroys Hotel 电梯只下不上 接n人到达0层最早时刻

A. Saitama Destroys Hotel time limit per test 1 second memory limit per test 256 megabyt...

【Codeforces Round 331 (Div 2)B】【水题 贪心】Wilbur and Array 修改对p位置后的所有位置生效 最少修改次数使得数列变成b

Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes ...

【Codeforces Round 331 (Div 2)A】【水题】Wilbur and Swimming Pool 给出矩形1~4个顶点让你还原矩形

Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabyte...

【Codeforces Round 269 (Div 2)C】【暴力】MUH and Sticks 纸牌搭楼n张牌恰好搭楼数

C. MUH and House of Cards time limit per test 1 second memory limit per test 256 megabyt...

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)A. A Serial Killer(水题)

题目: A. A Serial Killer time limit per test 2 seconds memory limit per test 256 mega...

【Codeforces Round 374 (Div 2)D】【贪心】 Maxim and Array n个数做K次±X使得乘积尽可能小

D. Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【Codeforces Round 354 (Div 2)A】【水题 贪心】Nicholas and Permutation 恰好交换一次的pos[n]-pos[1]
举报原因:
原因补充:

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