hdoj 5532 Almost Sorted Array 【LIS】



Almost Sorted Array

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


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
 



题意:给定一个n个元素的序列,你可以去掉序列中任意一个元素。问剩余序列是否是一个非上升或者非下降序列。

思路:正反两次LIS就KO了。

AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#define MAXN 101000
#define LL long long
#define Ri(a) scanf("%d", &a)
#define Pi(a) printf("%d\n", (a))
#define Rl(a) scanf("%lld", &a)
#define Pl(a) printf("%lld\n", (a))
#define Rs(a) scanf("%s", a)
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define INF 0x3f3f3f3f
using namespace std;
int a[MAXN], b[MAXN];
int s[MAXN];
int Find(int l, int r, int val)
{
    while(r >= l)
    {
        int mid = (l + r) >> 1;
        if(s[mid] > val)
            r = mid-1;
        else
            l = mid+1;
    }
    return l;
}
int main()
{
    int t; Ri(t);
    W(t)
    {
        int n; Ri(n);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        for(int i = 1; i <= n; i++)
            b[i] = a[n-i+1];
        int len;
        s[0] = -1; len = 1;
        for(int i = 1; i <= n; i++)
        {
            if(a[i] >= s[len-1])
                s[len++] = a[i];
            else
            {
                s[len] = INF;
                int j = Find(0, len, a[i]);
                if(j == len)
                     len++;
                s[j] = a[i];
            }
        }
        if(len-1 >= n-1)
        {
            printf("YES\n");
            continue;
        }
        s[0] = -1; len = 1;
        for(int i = 1; i <= n; i++)
        {
            if(b[i] >= s[len-1])
                s[len++] = b[i];
            else
            {
                s[len] = INF;
                int j = Find(0, len, b[i]);
                if(j == len)
                    len++;
                s[j] = b[i];
            }
        }
        if(len-1 >= n-1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


你可能感兴趣的:(hdoj 5532 Almost Sorted Array 【LIS】)