zoukankan      html  css  js  c++  java
  • Codeforces Round #258 (Div. 2) . Sort the Array 贪心

    B. Sort the Array

    题目连接:

    http://codeforces.com/contest/451/problem/B

    Description

    Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct 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

    3
    3 2 1

    Sample Output

    yes
    1 3

    Hint

    题意

    给你n个数,然后你可以交换一个子串的顺序,问你能不能使得这n个数是单调递增的。

    题解:

    贪心去交换就好了,交换完再check一下就行。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+7;
    int n;
    int a[maxn];
    bool check()
    {
        for(int i=2;i<=n;i++)
            if(a[i]<a[i-1])return false;
        return true;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
        {
            int j=i+1;
            for(;j<=n;j++)
                if(a[j]>=a[i])
                    break;
            if(j!=i+1)
            {
                reverse(a+i,a+j);
                if(check())
                {
                    printf("yes
    ");
                    printf("%d %d",i,j-1);
                }
                else
                    printf("no
    ");
                return 0;
            }
        }
        if(check())
        {
            printf("yes
    ");
            printf("%d %d",1,1);
        }
        else
            printf("no
    ");
    }
  • 相关阅读:
    Window如何查看cpu核数,更改CPU开启的核数?
    Mysql5.6.47开放远程访问(修改远程访问密码)
    CentOS7.6新增或修改SSH端口号的步骤
    虚拟机下安装Centos设置静态ip,并通过桥接连接
    windows下安装mysql5.6.47版本
    微软官方安装介质Windows10系统安装教程
    【测试编码URI的函数】
    【JavaScript函数】
    【JavaScript运算符与表达式】
    【JavaScript声明变量的规则】
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5856214.html
Copyright © 2011-2022 走看看