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

    题目链接:http://codeforces.com/contest/451/problem/B


    思路:首先找下降段的个数,假设下降段是大于等于2的,那么就直接输出no,假设下降段的个数为1,那么就把下降段的起始位置和结束位置记录下来然后进行推断,在进行推断时,有几种特殊情况:(s表示起始位置,e表示结束位置)

    1.当e==n&&s!=1时,满足a[n]>a[s-1]输出yes;

    2当s==1&&==n时,满足a[1]<a[e+1] 输出yes;

    3当s==1&&e==n时,直接输出yes;

    4当s!=1&&e!=n时,满足(a[s]<a[e+1])&&(a[e]>a[s-1])时,输出yes


    code:

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    int a[100010];
    int main()
    {
        int n;
        while(scanf("%d",&n)==1)
        {
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
            }
            int flag=1,flag1=1;    //用flag记录下降段的个数,这儿我们默认至少有一个下降段
            int s=1,e=1;
            for(int i=2;i<=n;i++)
            {
                //printf("AAA%d
    ",i);
                if(flag==1)       //
                {
                    if(a[i-1]>a[i])
                    {
                        if(flag1==1)    //记录第一个下降段的起始位置
                        {
                            flag1=2;
                            s=i-1;
                        }
                        if(i==n)       //记录第一个下降段的结束位置
                        {
                            e=n;
                        }
                    }
                    if(flag1==2)      //推断在第一个下降段结束时,并把flag++;
                    {
                        if(a[i-1]<a[i])
                        {
                            e=i-1;
                            flag++;
                        }
                    }
    
                }
                if(flag==2)    //假设有第二个下降段,那么就直接输出
                {
                    if(a[i-1]>a[i])
                    {
                        flag++;
                        //printf("AAA%d %d
    ",s,e);
                        printf("no
    ");
                        break;
                    }
                }
            }
            if(flag==2||flag==1)     //对各种情况进行推断
            {
                if(s==1&&e!=n)       //情况1
                {
                    if(a[1]<a[e+1])
                    {
                        printf("yes
    %d %d
    ",s,e);
                    }
                    else
                    {
                        printf("no
    ");
                    }
                }
                else if(e==n&&s!=1)       //情况2
                {
                    if(a[n]>a[s-1])
                    {
                        printf("yes
    %d %d
    ",s,e);
                    }
                    else
                    {
                        printf("no
    ");
                    }
                }
                else if(s==1&&e==n)    //情况3
                {
                    printf("yes
    %d %d
    ",s,e);
                }
                else if(s!=1&&e!=n)    //情况4
                {
                    if((a[s]<a[e+1])&&(a[e]>a[s-1]))
                    {
                        printf("yes
    %d %d
    ",s,e);
                    }
                    else
                    {
                        printf("no
    ");
                    }
                }
    
    
            }
        }
        return 0;
    }
    


  • 相关阅读:
    Spring事务隔离级别、传播机制、实现方式
    包装类缓存
    Object类详解
    String类详解
    自己实现一个Map
    锁机制
    各容器区别比较
    Map-CurrentHashMap
    Javascript中bind()方法的使用与实现
    this、new、call和apply的相关问题
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4305550.html
Copyright © 2011-2022 走看看