F - Almost Sorted Array

F - Almost Sorted Array 

 
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,,ana1,a2,…,an, is it almost sorted?

InputThe first line contains an integer TT indicating the total number of test cases. Each test case starts with an integer nn in one line, then one line with nn integers a1,a2,,ana1,a2,…,an. 

1T20001≤T≤2000 
2n1052≤n≤105 
1ai1051≤ai≤105 
There are at most 20 test cases with n>1000n>1000.
OutputFor 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

// we need to use the algorithm: the longest increasing subsequence 


#include <the iostream> 
#include <cstdio> 
#include <memory.h> 
#include <algorithm> #define INF 0x3f3f3f the using namespace STD; const int MAXN = + 1E5 . 7 ; int DP [MAXN];
 int A [MAXN];
 int B [MAXN]; int main () {
     int T; 
    CIN >> T;
     the while (T-- ) {
         int n-; 
        Scanf ( " % D " , &



 

 



n-); 
        Memset (DP, INF, the sizeof DP);
         for ( int I = 0 ; I <n-; I ++ ) { 
            Scanf ( " % D " , & A [I]); 
            B [n- - I - . 1 ] = A [I]; 
        } 
        // increment
         // , traversing, the current element is inserted into the appropriate location in the digital dp 
        for ( int I = 0 ; I <n-; I ++ ) {
             * upper_bound, (dp, dp + n-, a [I] ) = a [I]; 
        } 
        // length of the longest increasing sequence determined 
        intCNT1 = lower_bound (DP, DP + n-, INF) - DP;
         // decrement 
        Memset (DP, INF, the sizeof DP);
         for ( int I = 0 ; I <n-; I ++ ) {
             * upper_bound, (DP, DP + n- , B [I]) = B [I]; 
        } 
        int CNT2 = lower_bound (DP, DP + n-, INF) - DP;
         // if the last incrementing and decrementing the length of which is greater than or equal to (n-1), then , outputs YES, otherwise the output NO 
        IF (CNT1> = (n-- . 1 ) || CNT2> = (n-- . 1 )) { 
            the printf ( " YES \ n- " ); 
        } the else  {
            the printf ("NO\n");
        }
    }
    return 0;
}

 

 



Guess you like

Origin www.cnblogs.com/buhuiflydepig/p/10942391.html