codeforces 676A A. Nicholas and Permutation(水题)
题目链接:
1 second
256 megabytes
standard input
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.
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 题意: 交换这个序列里面的两个数使最大的数和最小的数的距离最大,问这个最大的距离是多少; 思路: 把最大的和最小的分别和第一个和最后一个交换,最大的就是答案了; AC代码:
#include <bits/stdc++.h>
/*
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e5+;
int n;
int a[],flag[];
int main()
{
scanf("%d",&n);
Riep(n)
{
scanf("%d",&a[i]);
flag[a[i]]=i;
}
int ans=;
ans=max(ans,abs(n-flag[]));
ans=max(ans,abs(flag[n]-));
ans=max(ans,abs(n-flag[n]));
ans=max(ans,abs(flag[]-));
cout<<ans<<"\n"; return ;
}
最新文章
- Oracle补全日志(Supplemental logging)
- GJM : 常用网站收集 【不断更新中... ... ... 】
- Android selecter背景选择器使用
- 恢复SharePoint Server 2013 中的“新颖快建视图”
- JS 初学正则表达式
- meteor 为基础,联合 Apollo + React + React-Router
- input相关问题总结
- linux根目录下文件夹概览
- ER-STUDIO 6.5工具使用帮助的中文翻译
- 针对《来用》的NABC分析
- 【Android自动化打包】03. APK的数字签名
- 转自 Good morning 的几句精辟的话
- 排序Tip
- Vim简明教程【CoolShell】
- 关于Zend Framework 2中 Zend\Session的使用
- dos判断系统版本的语句,
- c#—get,set访问器的作用
- protocol buffer 编码
- jstat查看JVM GC情况
- 利用奇异值分解(SVD)进行图像压缩-python实现