Nicholas and Permutation
来源:互联网 发布:华为算法工程师好进吗 编辑:程序博客网 时间:2024/06/12 01:17
Nicholas has an array a that contains n distinct integers from 1 to n. In other words, Nicholas has a permutation of sizen.
Nicholas want the minimum element (integer 1) and the maximum element (integern) 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.
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 integersa1, a2, ..., an (1 ≤ ai ≤ n), where ai is equal to the element at thei-th position.
Print a single integer — the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap.
54 5 1 3 2
3
71 6 5 3 4 7 2
6
66 5 4 3 2 1
5
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 swap7 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 swap5 and 2.
showing code:
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;struct node{ int v; int p;};node arr[110];int main(){ int n; int d = 0; node ma, mi; while (scanf("%d", &n) != EOF) { d = 0; for (int i=0; i<n; i++) { arr[i].p = i; scanf("%d", &arr[i].v); } mi.v = ma.v = arr[0].v; mi.p = ma.p = arr[0].p; for (int i=1; i<n; i++) { if (ma.v < arr[i].v) { ma.v = arr[i].v; ma.p = arr[i].p; } if (mi.v > arr[i].v) { mi.v = arr[i].v; mi.p = arr[i].p; } } ma.p += 1; mi.p += 1; d = abs(ma.p - mi.p); d = max (abs(ma.p - n), d); d = max (abs(mi.p - n), d); d = max (abs(ma.p - 1), d); //比赛时这里的ma写成了mi, 居然还可以过预测, 多么戏剧性的一幕呀 d = max (abs(mi.p - 1), d); printf("%d\n", d); } return 0;}我为什么总犯低级错误呀~~~5555~
- Nicholas and Permutation
- Nicholas and Permutation
- A. Nicholas and Permutation
- codeforces 676A Nicholas and Permutation
- CodeForces 676A Nicholas and Permutation
- 【CodeForces 676】A - Nicholas and Permutation
- codeforces 676A Nicholas and Permutation
- Codeforces Round #354 (Div. 2)767A. Nicholas and Permutation
- Codeforces Round #354 (Div. 2) A Nicholas and Permutation
- Codeforces Round #354 (Div. 2) A Nicholas and Permutation(水题)
- Codeforces Round #354 (Div. 2)-Nicholas and Permutation
- CodeForces - 676A Nicholas and Permutation (模拟) 水
- CodeForces 676A Nicholas and Permutation(移动数字游戏)
- 【Codeforces Round 354 (Div 2)A】【水题 贪心】Nicholas and Permutation 恰好交换一次的pos[n]-pos[1]
- B. Levko and Permutation
- Codeforces554D:Kyoya and Permutation
- hdoj4985Little Pony and Permutation
- [codeforces]Levko and Permutation
- Example: IPv4 or IPv6 client
- NCBI网页上进行Nr注释
- |Tyvj|BFS|P1117 拯救ice-cream
- jQ源码之animate
- 费马小定理降幂--nkoj3687 整数拆分
- Nicholas and Permutation
- OpennCV中Mat的属性介绍
- Interleaving String
- 【一天一道LeetCode】#64. Minimum Path Sum.md
- 神秘的40毫秒延迟与 TCP_NODELAY
- xcode下出现Undefined symbols for architecture arm64或者armv7: “_OBJC_CLASS_$_XXX
- hdu 1700(圆最大内接三角形)
- iOS开发之高级视图—— UINavigationController(三)结合编辑功能
- Http 一次请求过程