dp (maximum lift sequence: a binary search time optimization nlogn)

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?
InputThe 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.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 

idea: whether the number of non-down to find the longest sequence or sequences of the longest non-increasing number of the most original series number one, that is, yes. .


#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
int   v[509] , w[509] , dp[100009] , a[100009] , dpp[100009], b[100009];
intLEN1, LEN2; 

int halfsearch ( int * dp, int R & lt, int X) // Returns an array of the first dp than a [i] a large number of index +1 
{
     int L = . 1 , MID;
     the while (R & lt > = L) 
    { 
        MID = (L + R & lt) >> . 1 ;
         IF (DP [MID] <= X) 
        { 

            L = MID + . 1 ; 
        } 
        the else 
        { 
            R & lt = MID - . 1 ; 
        } 
    } 
    return  L;
}


int main () 
{ 
    int n-; 
    Scanf ( " % D " , & n-);
     the while (N-- ) 
    { 
        int m; 
        Scanf ( " % D " , & m);
         for ( int I = . 1 ; I <= m; I ++ ) 
        { 
            Scanf ( " % D " , a & [I]); 
            B [m - I + . 1 ] = a [I]; // here will find the longest clever nonincreasing sequences into maximum seek nondecreasing sequence. 
        } 
        DP [. 1] = A [ . 1 ]; 
        LEN1 = . 1 ;
         for ( int I = 2 ; I <= m; I ++ ) 
        { 
            IF (A [I]> = DP [LEN1]) // is less than the array a maximum of one is inserted behind 
                DP [LEN1 ++] = a [i];
             the else // will replace the first one a [i] than a [i] a large number; 
            { 
                DP [halfsearch (DP, LEN1, a [i])] = A [I]; 
            } 
        } 
        IF (LEN1> m = - . 1 ) 
            the printf ( " YES \ n- " );
        else
        {
            dpp[1] = b[1] ;
            len2 = 1 ;
            for(int i = 2 ; i <= m ; i++)
            {
                if(b[i] >= dpp[len2])
                    dpp[++len2] = b[i];
                else
                {
                    dpp[halfsearch(dpp , len2 , b[i])] = b[i];
                }
            }

            if(len2 >= m - 1)
                printf("YES\n");
            else
                printf("NO\n");


        }


    }

    return 0;
}

 



Guess you like

Origin www.cnblogs.com/nonames/p/11228547.html