Ask the length of the longest ascending sequence (LIS), but the two adjacent numbers must be at least D. The data range is large, and the general DP must be TLE. The line segment tree is enough, or the optimized nlogn's DP.
The Code is a line segment tree solution.
1 # include <set> 2 # include <map> 3 # include <cmath> 4 # include <ctime> 5 # include <queue> 6 # include <stack> 7 # include <cctype> 8 # include <cstdio> 9 # include <string> 10 # include <vector> 11 # include <cstdlib> 12 # include <cstring> 13 # include <iostream> 14 # include <algorithm> 15 using namespace STD; 16 typedef unsigned long ull; 17 typedef long ll; 18 const int INF = 0x3f3f3f; 19 const doubl E eps = 1e-8; 20 const int maxn = 1e5 + 10; 21 int DP [maxn], SEG [maxn <2]; // calculates the maximum value of 22 int A [maxn] In the tree segment; 23 void Update (int l, int R, int POs, int X, int Val) 24 {25 if (L = r) 26 {27 seg [POS] = val; 28 return; 29} 30 int mid = (L + r)> 1; 31 if (x <= mid) 32 Update (L, mid, POS <1, x, Val); 33 If (x> mid) 34 Update (Mid + 1, r, POS <1 | 1, x, Val); 35 seg [POS] = max (SEG [POS <1], SEG [POS <1 | 1]); 36} 37 int query (int l, int R, int Po S, int X, int y) 38 {39 if (x <= L & Y> = r) 40 return seg [POS]; 41 int mid = (L + r)> 1; 42 int ans1 = 0, ans2 = 0; 43 If (x <= mid) 44 ans1 = query (L, mid, pos <1, x, y); 45 if (Y> mid) 46 ans2 = query (Mid + 1, R, POS <1 | 1, x, y ); 47 return max (ans1, ans2); 48} 49 int main (void) 50 {51 # ifndef online_judge52 freopen ("in.txt", "r", stdin ); 53 # endif54 int N, D; 55 while (~ Scanf ("% d", & N, & D) 56 {57 memset (SEG, 0, sizeof (SEG); 58 int Len = 0; 59 for (INT I = 0; I <n; I ++) 60 {61 scanf ("% d", A + I); 62 A [I] + = 2; // because a [I] may be 0, and my line segment tree starts from 1, add 263 Len = max (Len, a [I]); 64} 65 int ans = 1; 66 for (INT I = 0; I <n; I ++) 67 {68 DP [I] = query (1, Len, 1,1, A [I]-1) + 1; // It cannot be equal to, so reducing 1, DP [I] indicates the maximum length of the sequence ending with a [I, 69 if (I> = d) // delay update, which is the key to this question. 70 update (1, Len, 1, a [I-d], DP [I-d]); 71 ans = max (DP [I], ANS); 72} 73 printf ("% d \ n", ANS ); 74} 75 return 0; 76}
Hdu4521-Xiao Ming series problems-Xiao Ming sequence (finding the greatest value in the segment tree)