The LIS algorithm with time complexity of N*LOGN is to maintain a maximum increment subsequence with a stack
If there are x < Y and a[x] > A[y], then we can use A[y] to replace a[x] because A[y] is relatively small, with greater potential, so that the latter element and it becomes a longer increment sequence as an example: a[] = {1,4,8,3,6}; We use a stack St Saves the current longest increment subsequence, top = 0, obviously, st[top] = 1 at initialization, and then increments with the I loop variable if a[i] > St[top], then st[++top] = A[i]. It is obvious that the top+1 is the length of the longest increment subsequence, so the time complexity of the judgment is O (1), why can it be judged??? Because St holds the current longest increment subsequence, if a[i] > St[top] then a[i] can be added to the St in order to form a longer, longest increment subsequence. Then there may be an idea that if the data is 1 5 3 4, it is clear that the longest increment subsequence is 1 3 4, but according to the idea above it is 1 5 to form the longest increment subsequence. Don't worry about the processing methods when A[i] < St[top] are described above we said that if there is x < Y and a[x] > A[y] We can use A[y] to replace a[x] because A[y] has greater potential to make the subsequent element and it become a longer increment sequence 。 So when A[i] < St[top], it is obvious that the element in St is a[x], and A[i] is a[y] We use binary search in St to find the first element greater than or equal to A[i], and then use a[i] to replace it such as St = 1, 4, 8 o'clock a[i] = 3, we can Use A[i] to replace 4, so that without affecting the results of the premise, reduce the time complexity of the topic uva10534 given a set of numbers, we want to ask such a sequence on the left side of the sequence is incremented, the right is decremented, And the number of increments and decrements if the same idea: from both sides of the longest increment sub-sequence of DP,DP1 is a DP from subscript 0, n-1DP2 is a DP from subscript n-1, 0so ans = max{min (dp1[i]-1, dp2[i]-1) +1, 0<=i<n};
But the problem of the data with 1w,o (N*N) algorithm is not possible,so we're going to use Nlogn's algorithm
1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <algorithm>5#include <iostream>6#include <queue>7#include <stack>8#include <vector>9#include <map>Ten#include <Set> One#include <string> A using namespacestd; -typedefLong LongLL; - Const intINF =1<< -; the Const intN =10000+Ten; - intMinConst int&a,Const int&b) - { - returnA < b?a:b; + } - intMaxConst int&a,Const int&b) + { A returnA < b?b:a; at } - intSt[n]; - inttop; - voidLIS (int*a,intNint*DP) - { - inti,j; intop =0; -St[top] = a[0]; to for(i=1; i<n; ++i) + { - if(A[i] >St[top]) the { *St[++top] =A[i]; $Dp[i] = top +1 ;Panax Notoginseng } - Else the { + intLow =0, high =top; A while(Low <=High ) the { + intMid = (low + high) >>1; - if(st[mid]<A[i]) $Low = mid +1; $ Else -High = mid-1; - } theSt[low] =A[i]; -Dp[i] = low +1;Wuyi } the } - } Wu intA[n]; - intdp[2][n]; About intMain () $ { - intn,i,j; - while(SCANF ("%d", &n)! =EOF) - { A for(i=0; i<n; ++i) + { thescanf"%d",&a[i]); -dp[0][i] = dp[1][i] =1; $ } theLIS (a,n,dp[0]); the the intLow =0, high = n-1; the while(Low <High ) - { in intt =A[low]; theA[low] =A[high]; theA[high] =T; AboutLow + +; theHigh--; the } theLIS (a,n,dp[1]); + - intAns =0; the for(i=0; i<n; ++i)Bayi { the intt =2* MIN (dp[0][i]-1, dp[1][n-i-1]-1) +1;//because the second DP is to invert the array to the DP, N-i-1 theAns =Max (ans,t); - } -printf"%d\n", ans); the } the return 0; the}View Code
Lis algorithm with time complexity O (NLOGN)