Hdu4521 Xiaoming sequence (line segment tree + dp)

Source: Internet
Author: User

Question: The meaning of the question is very simple. Given a sequence, find the length of the maximum incrementing subsequence, and the difference between the two adjacent subsequences must be greater than D.

Analysis: just to get the question, just add a restriction condition to the longest incrementing subsequence and follow the idea of the longest incrementing subsequence, then DP [I] = max (DP [I], DP [J] + 1), (I-j> D & A [I]> A [J])

From the state transition equation, we can see that the key to the problem is that the maximum Dp value of the maintenance interval (0, I-d-1) is smaller than a [I].

We use the value of a [I] As the subscript to guarantee the current range (0, a [I]-1) if the Dp value in all satisfies (I-j> D), then DP [I] is equal to the maximum value of the range (0, a [I]-1, this can be solved using the line segment tree.

Hdu4521

# Include <iostream> # Include <Algorithm> # Include <Stdio. h> # Include < String . H> # Include <Stdlib. h>Using   Namespace  STD;  Const   Int N = 100000 + 10  ;  Struct  Node {  Int  L, R, Max;} p [n * 3  ];  Int  A [n], d, n; Int  DP [N];  Void Build ( Int S, Int T, Int  K) {P [K]. L = S; P [K]. r = T; P [K]. Max = 0  ;  If (S = T) Return  ;  Int Mid = (S + T)> 1 , KL = k < 1 , Kr = KL + 1  ; Build (S, mid, KL); Build (Mid + 1  , T, KR );}  Void Insert ( Int K, Int S, Int  Val ){  If (P [K]. L = P [K]. R & P [K]. L = S) {P [K]. Max = Max (P [K]. Max, Val );  Return  ;}  Int Mid = (P [K]. L + P [K]. R)> 1 , KL = k < 1 , Kr = KL + 1  ;  If (S <= Mid) insert (KL, S, Val );  Else  Insert (KR, S, Val); P [K]. Max = Max (P [Kl]. Max, P [Kr]. max );} Int Query ( Int K, Int S, Int  T ){  If (S <= P [K]. L & T> = P [K]. R ){  Return  P [K]. Max ;}  Int Mid = (P [K]. L + P [K]. R)> 1 , KL = k < 1 , Kr = KL + 1 ;  Int A = 0 , B = 0  ;  If (S <= mid) A = Query (KL, S, T );  If (T> mid) B = Query (KR, S, T );  Return  Max (a, B );}  Int  Main (){  While (Scanf ( " % D  " , & N, & D) = 2  ){  Int Maxa = 0  ;  For ( Int I = 1 ; I <= N; ++ I) {scanf (  "  % D  " , A + I ); ++ A [I]; maxa = Max (maxa, a [I]);} memset (DP,  0 , Sizeof  (DP); Build (  0 , Maxa, 1  );  Int Ans = 1  ;  For ( Int I = 1 ; I <= N; ++I ){  Int TMP = query ( 1 , 0 , A [I]- 1  ); DP [I] = Max (DP [I], TMP + 1  ); Ans = Max (ANS, DP [I]);  If (I-d> 0 ) Insert ( 1 , A [I-d], DP [I-d]); // Insert a Dp value that meets the subscript in the online segment tree  } Printf (  "  % D \ n  "  , ANS );}  Return   0  ;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.