Codeforces Round #354 (Div. 2) A. Nicholas and Permutation 水题
2016-05-26 15:31
387 查看
A. Nicholas and Permutation
题目连接:
http://www.codeforces.com/contest/676/problem/ADescription
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.Sample Input
54 5 1 3 2
Sample Output
3Hint
题意
给你一个n的排列,然后你可以改变一次这个排列的位置你需要使得最小的数和最大的数距离最大,问你这个距离是多少
题解:
显然就是把最小的数扔到边上,或者把最大的数扔到边上就好了代码
#include<bits/stdc++.h> using namespace std; const int maxn = 106; int a[maxn],b[maxn]; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); int ans1 = 0,ans2 = 0; for(int i=1;i<=n;i++) { if(a[i]==1)ans1=i; if(a[i]==n)ans2=i; } int ans=max(abs(n-ans2),abs(ans2-1)); ans=max(ans,abs(n-ans1)); ans=max(ans,abs(ans1-1)); cout<<ans<<endl; }
相关文章推荐
- Async函数介绍
- 第3篇 解决问题的逻辑
- 5.26 第一天
- iOS开发之SQLite-C语言接口规范(二) —— Prepared Your SQL Statements
- java之单例模式的七种写法
- PHP实现冒泡排序的简单实例
- 关于GEM5-GPU编译问题
- 买二手房不后悔先知道8件事 高楼层未必卖得好
- Python通过LDAP验证、查找用户(class,logging)
- Form表单提交
- 数据保存成功后,需要刷新页面,但是刷新后还是老数据
- hive根据身份证号判断性别
- 群晖 NAS DSM 系统,只要三步使用 Docker 安装迅雷远程下载
- url链接传递中文的解决办法
- 苏鑫老师谈如何学好编程
- c++中构造函数初始化的方法以及主要区别
- 玩转Android之二维码生成与识别
- iOS开发之SQLite-C语言接口规范(一)——Ready And Open Your SQLite
- html/css学习随笔
- 玩转Android之二维码生成与识别