hdu 5532 Almost Sorted Array 【2015ACM/ICPC亚洲区长春站-重现赛】

Almost Sorted Array

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


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
 

Source
2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)
 

题目大意:
有T组数据,每组数据有 m 个数,然后让你判断当去掉其中一个数的时候还是不是一个有序的数列(可以是非递增的 ,也可以是非递减的)
如果是的话,输出YES,否则输出NO。

解题思路:
刚开始我是打算用最长上升子序列做的,但是发现 m 的范围有点大,所以就没有用,我也说一下,以当参考,就是判断这个数列的
最长上升子序列的最大长度(在把序列倒过来计算一遍),如果 LIS 的值 >= m-1的话就是一个有序的,否则不是,但是会TLE,
下面是我的另一种做法。。。
就是分别判断一下 非递增的数列(和非递减的数列),同时判断的时候还要考虑前后两项是不是也满足非递增(非递减)的关系。。。
然后就ok了。

可以参考一下我的代码:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e5+5;
const int mod = 1000000007;
const int INF = 0x3f3f3f3f;
const double eps = 1e-7;

int arr[maxn];
int m;
bool judge1()///从小到大
{
    int sum = 0;
    arr[0] = -INF, arr[m+1] = INF;
    for(int i=2; i<=m; i++)
    {
        if(arr[i] < arr[i-1])
        {
            if(sum == 1)
            return false;
            sum++;
            if(arr[i+1]>=arr[i-1] || arr[i]>=arr[i-2])
                continue;
            else
                return false;
        }
    }
    return true;
}
bool judge2()///从大到小
{
    int sum = 0;
    arr[0] = INF, arr[m+1] = -INF;
    for(int i=2; i<=m; i++)
    {
        if(arr[i] > arr[i-1])
        {
            if(sum == 1)
            return false;
            sum++;
            if(arr[i+1]<=arr[i-1] || arr[i]<=arr[i-2])
                continue;
            else
                return false;
        }
    }
    return true;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&m);
        for(int i=1; i<=m; i++)
            scanf("%d",&arr[i]);
        if(judge1() || judge2())
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}
/**
2
5
1 2 5 3 4
5
1 2 1 4 5
**/



你可能感兴趣的:(hdu 5532 Almost Sorted Array 【2015ACM/ICPC亚洲区长春站-重现赛】)