ZOJ3349 -- Special Subsequence

Source: Internet
Author: User

ZOJ3349 -- Special Subsequence

Special Subsequence Time Limit: 5 Seconds Memory Limit: 32768 KB

There a sequenceSWithNIntegers, andAIs a special subsequence thatsatisfies |Ai-Ai-1| <=D(0

Now your task is to find the longest special subsequence of a certain sequenceS

Input

There are no more than 15 cases, process till the end-of-file

The first line of each case contains two integerNAndD(1 <=N& Lt; = 100000, 0 & lt; =D<= 100000000) as in the description.

The second line contains exactNIntegers, which consist the sequneceS. Eachinteger is in the range [0, 00000000]. There is blank between each integer.

There is a blank line between two cases

Output

For each case, print the maximum length of special subsequence you can get.

Sample Input

5 21 4 3 6 55 01 2 3 4 5

Sample Output

31



Water dp + water line segment tree

Haha today's first 1Y


#include #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          
           #include 
           
            #include using namespace std;const int N = 100010;int dp[N], xis[N], arr[N];int n, d, cnt;struct node { int l, r; int val; }tree[N << 2]; int BinSearch(int x) { int l = 1, r = cnt, mid; while (l <= r) { mid = (l + r) >> 1; if (xis[mid] > x) { r = mid - 1; } else if (xis[mid] < x) { l = mid + 1; } else { break; } } return mid; } int left_binsearch(int x){int l = 1, r = cnt, mid, ans = -1;while (l <= r){mid = (l + r) >>1;if (xis[mid] >= x){ans = mid;r = mid - 1;}else{l = mid + 1;}}return ans;}int right_binsearch(int x){int l = 1, r = cnt, mid, ans = -1;while (l <= r){mid = (l + r) >>1;if (xis[mid] <= x){ans = mid;l = mid + 1;}else{r = mid - 1;}}return ans;} void build(int p, int l, int r) { tree[p].l = l; tree[p].r = r; tree[p].val = -1; if (l == r) { return; } int mid = (l + r) >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); } void update(int p, int pos, int val) { if (tree[p].l == tree[p].r) { tree[p].val = max(tree[p].val, val); return; } int mid = (tree[p].l + tree[p].r) >> 1; if (pos <= mid) { update(p << 1, pos, val); } else { update(p << 1 | 1, pos, val); } tree[p].val = max(tree[p << 1].val, tree[p << 1 | 1].val); } int query(int p, int l, int r) { if (l <= tree[p].l && r >= tree[p].r) { return tree[p].val; } int mid = (tree[p].l + tree[p].r) >> 1; if (r <= mid) { return query(p << 1, l, r); } else if (l > mid) { return query(p << 1 | 1, l, r); } else { return max(query(p << 1, l, mid), query(p << 1 | 1, mid + 1, r)); } }int main(){while(~scanf("%d%d", &n, &d)){cnt = 0;int ans = 0;memset (dp, 0, sizeof(dp));for (int i = 1; i <= n; ++i){scanf("%d", &arr[i]);xis[++cnt] = arr[i];}sort (xis + 1, xis + cnt + 1);cnt = unique(xis + 1, xis + cnt + 1) - xis - 1;build(1, 1, cnt);for (int i = 1; i <= n; ++i){int cur = BinSearch(arr[i]);int l = left_binsearch(arr[i] - d);int r = right_binsearch(arr[i] + d);if (l < 0){l = 1;}if (r < 0){r = cnt;}int tmp = query(1, l, r);if (tmp == -1){dp[i] = 1;}else{dp[i] = tmp + 1;}update(1, cur, dp[i]);ans = max(ans, dp[i]);}printf("%d\n", ans);}return 0;}
           
          
         
        
       
      
     
    
   
  


Related Article

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.