Codeforces 比赛代码记录及心得
2016-05-26 02:19
489 查看
Codeforces Round #354 (Div. 2)
纪念一下自己差劲的适应能力,cf,要锻炼自己,适应能力很关键!!!
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
output
input
output
input
output
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.
纪念一下自己差劲的适应能力,cf,要锻炼自己,适应能力很关键!!!
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
66 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<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; int main() { int n,a[105]; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); int maxn=1,minn=1; for(int i=2;i<=n;i++) { if(a[i]>a[maxn]) maxn=i; if(a[i]<a[minn]) minn=i; } if(maxn>minn) { if(n-maxn>minn-1) printf("%d\n",n-minn); else printf("%d\n",maxn-1); } else { if(maxn-1>n-minn) printf("%d\n",minn-1); else printf("%d\n",n-maxn); } return 0; }
相关文章推荐
- Eclipse工具安装OpenExplorer插件--快速打开文档目录
- HashMap深度解析(二)
- HashMap深度解析(一)
- Struts2的convention插件,在步骤中使用Action注解跳转到其他jsp页面
- windows下编译java源文件的编码错误
- Java序列化与反序列化
- 量化分析:把Tushare数据源,规整成PyalgoTrade所需格式
- php array_filter过滤数组为空值
- C语言中访问结构体成员时 点 . 和 箭头 -> 的区别
- POJ 1008 (模拟)
- LeetCode 150. Evaluate Reverse Polish Notation(计算后缀表达式)
- C++第六次作业
- 初试Twitter API
- eclipse建maven web项目运行没有run as server
- 学生信息管理系统(struts2+Hibernate)(1)
- C++作业6
- C++第六次实验-项目1
- php preg_match_all函数笔记
- 生产者和消费者案例
- php urlencode()与urldecode()函数字符编码原理-笔记