Codeforces Round #354 (Div. 2)767A. Nicholas and Permutation

本文解析了CodeForces上的一道A级题目,通过C++实现了一个算法来解决如何通过一次交换使序列中1和n之间的最大距离最大化的问题。介绍了输入输出格式,并给出了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/contest/676/problem/A
题意:给一个1~n的序列,可以交换一次任意两个数,求交换后1和n的最远的距离

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long LL;
const int maxn = 10005;
const int inf=(1<<28)-1;
int A[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    int pos1,pos2;
    for(int i=1;i<=n;++i)
    {
        int x;
        scanf("%d",&x);
        if(x==1) pos1=i;
        if(x==n) pos2=i;
    }
    int ans=0;
    ans=max(n-pos1,n-pos2);
    ans=max(pos1-1,ans);
    ans=max(pos2-1,ans);
    printf("%d\n",ans);
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值