zoukankan      html  css  js  c++  java
  • ACM训练二B题

    这是比赛后打的题目,思路很清晰:申明一个结构体,将输入的数复制在这个结构体数组中,排序后比对下标,找到变动的首下标和尾下标,再看这段是否逆序了。

    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

    #include <iostream>
    #include<algorithm>
    using namespace std;
    
    int a[100017];
    
    struct NUM{
     int x,id;
    }b[100017];
    
    bool cmp(NUM x,NUM y)
    {
    
        return x.x < y.x;
    }
    
    int main()
    {
        int n;
        cin >> n;
        for(int i = 1;i <= n;i++)
        {
    
            cin >> a[i];
            b[i].x = a[i];
            b[i].id = i;
        }
    
        sort(b+1,b+n+1,cmp);
    
        int i,j;
        for(i = 1;i <= n;i++)
        {
    
            if(b[i].id != i)
            {
                break;
            }
        }
    
        if(i == n+1)
        {
    
            cout << "yes" << endl << "1  1" << endl;
            return 0;
        }
        for(j = n;j > 0;j--)
        {
    
            if(b[j].id != j)
            {
    
                break;
            }
        }
    
        int tt = i;int t = j;
        while(i<=j)
        {
    
            if(a[i] != b[t--].x)
            {
    
                break;
    
            }
            i++;
        }
    
        if(i == j+1)
        {
    
            cout << "yes" << endl << tt << "  " << j << endl;
        }
        else
        {
    
            cout << "no" << endl;
        }
        return 0;
    }
  • 相关阅读:
    记一道乘法&加法线段树(模版题)
    2021CCPC网络赛(重赛)题解
    Codeforces Round #747 (Div. 2)题解
    F. Mattress Run 题解
    Codeforces Round #744 (Div. 3) G题题解
    AtCoder Beginner Contest 220部分题(G,H)题解
    Educational Codeforces Round 114 (Rated for Div. 2)题解
    Codeforces Global Round 16题解
    Educational Codeforces Round 113 (Rated for Div. 2)题解
    AtCoder Beginner Contest 182 F
  • 原文地址:https://www.cnblogs.com/hhhhhhhhhhhhhhhhhhhhhhhhhhh/p/3874382.html
Copyright © 2011-2022 走看看