zoukankan      html  css  js  c++  java
  • Almost Sorted Array

    Almost Sorted Array

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 430    Accepted Submission(s): 196


    Problem Description
    We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But sometimes it is an overkill to use these algorithms for an almost sorted array.

    We say an array is sorted if its elements are in non-decreasing order or non-increasing order. We say an array is almost sorted if we can remove exactly one element from it, and the remaining array is sorted. Now you are given an array a1,a2,,an, is it almost sorted?
     
    Input
    The first line contains an integer T indicating the total number of test cases. Each test case starts with an integer n in one line, then one line with n integers a1,a2,,an.

    1T2000
    2n105
    1ai105
    There are at most 20 test cases with n>1000.
     
    Output
    For each test case, please output "`YES`" if it is almost sorted. Otherwise, output "`NO`" (both without quotes).
     
    Sample Input
    3
     
     
    3
    2 1 7
     
    3
    3 2 1
     
    5
    3 1 4 1 5
     
    Sample Output
    YES
    YES
    NO
    题意: 定义一数组类型为almost sorted,满足在数组中最多存在一个数,删除后数组满足非递增或非递减。
     
    #include<iostream>
    #include<cstdio>
    
    using namespace std;
    
    #define maxn 100005
    #define INF 0x3f3f3f3f
    
    int a[maxn], n;
    
    bool stobig()   // 判断该数组变成 非递减数组是否只需要删除一个数
    {
        int sum = 0;
        a[0] = -INF, a[n+1] = INF;
        for(int i = 1; i <= n; i++)
        {
            if(a[i-1] > a[i])
            {
                if(sum == 1)
                    return false;
                sum++;
                if( a[i-1] > a[i+1] && a[i] < a[i-2] )
                    return false;
            }
        }
        return true;
    }
    
    bool btosmall()   // 判断该数组变成 非递增数组是否只需要删除一个数
    {
        int sum = 0;
        a[0] = INF, a[n+1] = -INF;
        for(int i = 1; i <= n; i++)
        {
            if(a[i] < a[i+1])
            {
                if(sum == 1)
                    return false;
                sum++;
                if( a[i-1] < a[i+1] && a[i+2] > a[i] )
                    return false;
            }
        }
        return true;
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        while(t--)
        {
            scanf("%d", &n);
            for(int i = 1; i <= n; i++)
            {
                scanf("%d", &a[i]);
            }
            if(stobig() || btosmall())
                printf("YES
    ");
            else
                printf("NO
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    PHP中利用jQuery操作json格式数据,实现$_POST的数据传输和接收
    如何快速掌握一门技术【婴儿最强学习回头看一看】
    显示桌面.scf
    注册表数据库
    win10home_fixgpedit.msc
    Eclipse 中 jetty 调试模式(debug)正常启动无法访问;非调试模式正常
    svn中的与资源库同步操作 讲解
    windows下二进制mysql的卸载以及安装教程
    mysql服务正在启动或停止中请稍后片刻再试一次,服务强制杀死的方法
    Eclipse中git检出、更新、提交、合并分支、以及解决冲突
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4937081.html
Copyright © 2011-2022 走看看