hdu 5532 Almost Sorted Array(LIS)

来源:互联网 发布:midaspro3离线编辑软件 编辑:程序博客网 时间:2024/06/15 14:23

Almost Sorted Array

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


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
332 1 733 2 153 1 4 1 5
 

Sample Output
YESYESNO


题意:在原数列中删除一个数判断是否有序;

解:LIS的n*log(n) 啪啪啪打脸

#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<cmath>#include<string>#include <bits/stdc++.h>using namespace std;typedef long long LL;const int N =2e6+10;const int inf = 0x3f3f3f3f;int a[N], b[N], c[N];int cmp(int A,int B){return A>B;}int main(){    int t, n;    scanf("%d", &t);    while(t--)    {        scanf("%d", &n);        for(int i=0;i<n;i++) scanf("%d", &a[i]), b[n-i-1]=a[i];        int k=1;        c[0]=a[0];        for(int i=1;i<n;i++)        {            if(a[i]>=c[k-1]) c[k++]=a[i];            else  *upper_bound(c,c+k,a[i])=a[i];        }        if(k>=n-1)        {            puts("YES");            continue;        }        k=1;        c[0]=b[0];        for(int i=1;i<n;i++)        {            if(b[i]>=c[k-1]) c[k++]=b[i];            else  *upper_bound(c,c+k,b[i])=b[i];        }        if(k>=n-1)        {            puts("YES");            continue;        }        puts("NO");    }    return 0;}







原创粉丝点击