HDU 1950 Bridging signals (LIS, dichotomy, O (NLOGN))

Source: Internet
Author: User

Test instructions

Given a sequence of numbers, it is required to find the LIS and output its length.

Ideas:

Sweep over + two points, complexity O (NLOGN), Spatial complexity O (n).

Method: Add an array, using d[i] to represent the last element of the increment subsequence of length I, and the element always remains the current minimum. Initialize D[1]=a[i], the length of the current LIS is len=1. From 2 to N, if A[i]>d[len], then d[++len]=a[i], otherwise, find in the array D a[i] should be inserted in place, instead of the first larger than the number, such as d[k]<a[i]<=d[k+1], directly a[i] Instead of d[k+1]. After completion len is the length of the LIS.

1#include <bits/stdc++.h>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <map>7#include <algorithm>8#include <vector>9#include <iostream>Ten #definePII pair<int,int> One #defineINF 2147483647 A #defineLL unsigned long Long - using namespacestd; - Const DoublePI = ACOs (-1.0); the Const intn=40100; - Const intmod=1e9+7; - intA[n], d[n]; - int* Lower_ (int*s,int*e,intVal)//binary Find value, return subscript + { -     intL=0, r=e-s-1, mid; +      while(l<R) A     { atmid=r-(r-l+1)/2;//guaranteed at least 1 reduction -         if(S[mid]<val) L=mid+1;//at least 1 increase -         ElseR=mid; -     } -     return&S[r]; - } in  - intMain () to { +Freopen ("Input.txt","R", stdin); -     intT, N, Len; theCin>>T; *      while(t--) $     {Panax Notoginsengscanf"%d",&n); -          for(intI=1; i<=n; i++) scanf ("%d",&a[i]); thelen=1; +d[len]=a[1]; A  the          for(intI=2; i<=n; i++ ) +         { -             if(A[i]>d[len]) d[++len]=A[i]; $             Else*lower_ (d+1, d+len+1, a[i]) =A[i]; $             //else *lower_bound (D+1,d+len+1,a[i]) =a[i]; the previous line of code can be replaced by this line -         } -printf"%d\n", Len); the     } -     return 0;Wuyi}
AC Code

HDU 1950 Bridging signals (LIS, dichotomy, O (NLOGN))

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.