• 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;
    }
  • 相关阅读:
    构建之法:第二次心得
    构建之法:第一次心得
    tomcat配置限制ip和建立图片服务器
    tomcat8.5优化配置
    java 操作 csv文件
    jsoup教学系列
    (转)js实现倒计时效果(年月日时分秒)
    本地启动tomcat的时候报java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: PermGen space
    使用mybatis执行oracle存储过程
    java 获取web登录者的ip地址
  • 原文地址:https://www.cnblogs.com/hhhhhhhhhhhhhhhhhhhhhhhhhhh/p/3874382.html
Copyright © 2020-2023  润新知