This is the transformation of the LIS, test instructions is the length of the longest continuous increment sequence that can be obtained when a sequence is removed from a sequential sequence.
The solution of DP is: Bar This sequence is recorded with array A, and then the length of the longest continuous increment sequence ending with I is recorded with two array F, G[i] records the longest continuous increment sequence starting with I. Then iterate through the entire sequence like DP to find the longest sequence at the end of the element in all elements less than a[i] (F[j], then dp[i] = G[j] + f[i], so that the time complexity is O (n^2).
Because it is similar to the ordinary Lis, the optimization method of Lis can be used to optimize the time complexity of the problem to O (Nlogn). The method still uses an array d[i] to record the minimum value of the last element of the successive increment sequence of length I, obviously the sequence is monotonically incrementing, so the operation of the above red font can be obtained by binary lookup directly to the value of f[j], then get a feasible length ans, then update the array D, The updated method is if the value of the last element of the sequence with a[i] less than the length of the A[i] is recorded in the array d, then the value is changed to a[i], i.e. d[f[i]] = min (A[i], d[f[i]); The maximum value of the final ans is the answer.
The code is as follows:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 6 using namespacestd;7 8 Const intMAXN =200050;9 Const intINF =1<< -;Ten intA[MAXN], F[MAXN], G[MAXN], D[MAXN]; One A intMain () - { - intT, n, I; thescanf"%d", &t); - while(t--) - { -scanf"%d", &n); + for(i =1; I <= N; i++) -scanf"%d", &a[i]); + Af[1] =1; at for(i =2; I <= N; i++) - if(A[i] > A[i-1]) -F[i] = f[i-1] +1; - Else -F[i] =1; - inG[n] =1; - for(i = n-1; i >0; i--) to if(A[i] < A[i +1]) +G[i] = g[i +1] +1; - Else theG[i] =1; * $ intAns =0;Panax Notoginseng for(i =0; I <= N; i++) -D[i] = INF;//D[i] Value is all assigned to INF, convenient for binary search and update d[i] the for(i =1; I <= N; i++) + { A intLen = (Lower_bound (d +1, D +1+ I, A[i])-(d +1)) +G[i]; theAns =Max (len, ans); +D[f[i]] =min (a[i], d[f[i]]); - } $printf"%d\n", ans); $ } - return 0; -}
Also attached is the LIS code: (Time Complexity O (NLOGN)
1#include <iostream>2#include <cstdio>3#include <algorithm>4 5 using namespacestd;6 Const intMAXN =100010;7 Const intINF =1<< -;8 intB[MAXN];9 intMain ()Ten { One intN, I, tem; A while(SCANF ("%d", &n)! =-1) - { - for(i =0; I <= n +1; i++) theB[i] =INF; - for(i =0; I < n; i++) - { -scanf"%d", &tem); + intpos = Lower_bound (b, b+i+1, tem)-B;//find the location of the TEM to be inserted in two points -B[pos] = tem;//Update monotonic Stacks + } A for(i =0; B[i]! = INF; i++);//to find the length of a monotonic stack atprintf"%d\n", i); - } -}
UVA1471 (Red Book example LIS)