Codeforces Round #354 (Div. 2)-A
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.
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.
Print a single integer — the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap.
5
4 5 1 3 2
3
7
1 6 5 3 4 7 2
6
6
6 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 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.
题意:给定一个序列,只能交换1次[一次交换任意的2个数的位置],问最大值和最小值的距离最大是多少?
思路:因为只能交换一次。所以肯定把某一个[最大值/最小值]反正最边是最优的。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<bitset>
using namespace std;
#define INF 0x3f3f3f3f
#define PI 3.14159
const int MAXN=+;
int num[MAXN];
int main()
{
#ifdef kirito
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int n;
while(~scanf("%d",&n)){
int MAX_pos,MIN_pos;
for(int i=;i<=n;i++)
{
scanf("%d",&num[i]);
if(num[i]==){MIN_pos=i;}
if(num[i]==n){MAX_pos=i;}
}
printf("%d\n",max(n-min(MAX_pos,MIN_pos),max(MAX_pos,MIN_pos)-)); }
return ;
}
Codeforces Round #354 (Div. 2)-A的更多相关文章
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #354 (Div. 2)-D
D. Theseus and labyrinth 题目链接:http://codeforces.com/contest/676/problem/D Theseus has just arrived t ...
- Codeforces Round #354 (Div. 2)-C
C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...
- Codeforces Round #354 (Div. 2)-B
B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth
题目链接: http://codeforces.com/contest/676/problem/D 题意: 如果两个相邻的格子都有对应朝向的门,则可以从一个格子到另一个格子,给你初始坐标xt,yt,终 ...
- Codeforces Round #354 (Div. 2) C. Vasya and String
题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
- Codeforces Round #354 (Div. 2) E. The Last Fight Between Human and AI 数学
E. The Last Fight Between Human and AI 题目连接: http://codeforces.com/contest/676/problem/E Description ...
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth bfs
D. Theseus and labyrinth 题目连接: http://www.codeforces.com/contest/676/problem/D Description Theseus h ...
随机推荐
- Mac会给你一些欣喜
Mac会给你一些欣喜 以前一直没有用过Mac,一直都是用Windows的电脑,只是偶尔会去用Ubuntu这样的Linux系统.Mac OS 确实是一只可以给你欣喜的系统. 上周拿到公司分发的Mac,到 ...
- 将file转变成contenthash
一.将MultipartFile转file CommonsMultipartFile cf= (CommonsMultipartFile)file; DiskFileItem fi = (DiskFi ...
- Android应用主题与横竖屏的切换
很多App,现在都具有了横竖屏切换的功能,或者说"白天"和"黑夜"主题的切换. 实现起来也非常简单.主要需要注意的是,在切换的同时,页面的数据不能丢失,不然给用 ...
- 51nod 1605 棋盘问题 (博弈)
题目:传送门. 题意:中文题.T组数据,每组给定一个n*m的棋盘,棋盘中的1代表黑色,0代表白色,每次可以将1或者非2质数的全黑色方形区域变为白色,不能操作者输,问谁能赢. 题解:每次可以将1或者非2 ...
- DB2中错误信息说明
DB2错误信息SQLCODE SQLSTATE (按sqlcode排序) .分类: db2数据库 2012-10-19 11:35 2942人阅读 评论(0) 收藏 举报 db2sql存储table数 ...
- mysql my.cnf配置文件详解
本配置文件针对Dell R710,双至强E5620.16G内存的硬件配置.CentOS 5.6 64位系统,MySQL 5.5.x 稳定版.适用于日IP 50-100w,PV 100-300w的站点, ...
- JS函数的其他用法【备于取用】
//随机数生成器 Math.random() 日期时间函数(需要用变量调用): var b = new Date(); //获取当前时间 b.getTime() //获取时间戳 b.getFull ...
- Mysql之performance Schema
Performance schema是用于监控Mysql执行,具有如下特征: 1.用于在运行时探查Mysql Server的执行过程,是由Performance_schema引擎和 Performan ...
- Ionic环境搭建
stepts npm install -g ionic@beta Make sure you have NodeJS installed. Download the installer here or ...
- JQ 练习题
1.留言板 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...