zoukankan      html  css  js  c++  java
  • acm集训训练赛B题【排序+模拟】

    一、原题

    Description

    Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of ndistinct integers.

    Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.

    Input

    The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a.

    The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).

    Output

    Print "yes" or "no" (without quotes), depending on the answer.

    If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.

    Sample Input

    Input
    3
    3 2 1
    Output
    yes
    1 3
    Input
    4
    2 1 3 4
    Output
    yes
    1 2
    Input
    4
    3 1 2 4
    Output
    no
    Input
    2
    1 2
    Output
    yes
    1 1

    Hint

    Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.

    Sample 3. No segment can be reversed such that the array will be sorted.

    Definitions

    A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].

    If you have an array a of size n and you reverse its segment [l, r], the array will become:

    a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].

    二 、题目源程序(一)

    #include<bits/stdc++.h>
    #define maxn 100005
    typedef long long LL;
    using namespace std;
    int a[maxn];
    int main()
    {
        int n,x=1,y=1;
        cin>>n;
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=1;i<n;i++){
            if(a[i]>a[i+1]){
                x=i;break;
            }
        }
        for(int i=n;i>=1;i--){
            if(a[i]<a[i-1]){
                y=i;
                break;
            }
        }
        for(int i=0;i<y-x;i++)
        {
            int t;
            t=a[x+i];
            a[x+i]=a[y-i];
            a[y-i]=t;
        }
        int flag=0;
        for(int i=1;i<n;i++){
            if(a[i]>a[i+1]){
                flag=1;
                break;
            }
        }
        if(flag) cout<<"no"<<endl;
        else{
            cout<<"yes"<<endl<<x<<" "<<y<<endl;
        }
        return 0;
    }

    题目源程序(二)

    #include<bits/stdc++.h>
    #define maxn 100005
    typedef long long LL;
    using namespace std;
    int a[maxn];
    int main()
    {
        int n,x=1,y=1;
        cin>>n;
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=1;i<n;i++){
            if(a[i]>a[i+1]){
                x=i;break;
            }
        }
        for(int i=n;i>=1;i--){
            if(a[i]<a[i-1]){
                y=i;
                break;
            }
        }
        reverse(a+x,a+y+1);
        int flag=0;
        for(int i=1;i<n;i++){
            if(a[i]>a[i+1]){
                flag=1;
                break;
            }
        }
        if(flag) cout<<"no"<<endl;
        else{
            cout<<"yes"<<endl<<x<<" "<<y<<endl;
        }
        return 0;
    }

    三、解题思路

    题意分析:

    给定一个序列,能否通过逆序它的一个子序列使得整个序列单调递增,如果能,输出需要逆序的下标,否则输出no

    思路

    1、从左到右遍历,第一次出现比前一个数小的下标;

    2、然后从右往左遍历,第一次出现比后一个数小的下标;

    3、将这段区间逆序,然后判断整个序列是否单调递增。

    四、心得体会

    (1)reverse函数的运用,包含于头文件<algorithm>中。

    (2)这是一道比较简单的排序与模拟题。

  • 相关阅读:
    【洛谷P1005】矩阵取数游戏
    【洛谷P1966】火柴排队
    【题解】洛谷P1731 [NOI1999] 生日蛋糕(搜索+剪枝)
    【题解】洛谷P3627 [APIO2009]抢掠计划(缩点+SPFA)
    【题解】洛谷P1262 间谍网络 (强连通分量缩点)
    【题解】洛谷P3200 [HNOI2009] 有趣的数列(卡特兰数+质因数分解)
    点双连通分量 [HNOI2012]矿场搭建
    分块 公主的朋友
    [置顶] 数学的坑,一点点来填
    大暴搜 [NOIP2009]靶形数独
  • 原文地址:https://www.cnblogs.com/fightfor/p/3871422.html
Copyright © 2011-2022 走看看