[Codeforces 451B] Sort the Array (实现)

Codeforces - 451B

给定一个序列,其中每个数都不相同
问是否能在翻转其中一段后,整个序列变得单调递增


实现题
首先设一个 B 数组为 A数组排序后的结果
由于只能翻转一个区间,那么我假装 A是满足要求的
找到最小的 A[l]B[l] ,最大的 A[r]B[r]
翻转的区间将会是 [l,r] 这个区间,
翻转这个区间以后,再看是否满足要求

有一个 trick是找不到这样的 [l,r] 的时候,
说明整个序列已经有序了,这时候随便输出一个长度为 1 <script id="MathJax-Element-59" type="math/tex">1</script>的区间即可

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define SQR(a) ((a)*(a))
#define PCUT puts("----------")

const int maxn=1e5+10;
int N, A[maxn], B[maxn];

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
    #endif

    scanf("%d", &N);
    for(int i=1; i<=N; i++) scanf("%d", &A[i]);
    for(int i=1; i<=N; i++) B[i] = A[i];
    sort(B+1, B+1+N);
    int l=-1,r=-1;
    for(int i=1; i<=N; i++)
    {
        if(A[i]!=B[i] && l==-1) l = i;
        if(A[i]!=B[i]) r=i;
    }
    reverse(A+l, A+r+1);
    bool ok=1;
    for(int i=1; i<=N; i++) if(A[i]!=B[i]) {ok=0; break;}
    if(ok)
    {
        puts("yes");
        if(l==-1 && r==-1) puts("1 1");
        else printf("%d %d\n", l, r);
    }
    else puts("no");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值