Lis-program E

Source: Internet
Author: User

Maximum Ascending sub-sequence

Description

  The world financial crisis is quite a subject. Some people is more relaxed while others is quite anxious. John is one of the them. He is very concerned about the evolution of the stock exchange. He follows stock prices every day looking for rising trends. Given a sequence of numbers p1, p2,..., pn representing stock prices, a rising trend is a subsequence pi1 < Pi2 <. . < Pik, with I1 < I2 < ... < IK. John ' s problem is to find very quickly the longest rising trend.

Input

Each data set in the file stands for a particular set of the stock prices. A data set starts with the length L (l≤100000) of the sequence of numbers, followed by the numbers (a number fits a long Integer).
White spaces can occur freely in the input. The input data is correct and terminate with an end of file.

Output

The program prints the length of the longest rising trend.
For each set of data the program prints the result to the standard output from the beginning of a line.

Sample Input

6 5 2 1 4 5 3 3  1 1 1 4 4 3 2 1

Sample Output

3 1 1


The main idea: to give a series of sequences, which require the output of the maximum ascending sub-sequence.

Analysis: With dynamic planning, high efficiency; If the latter number is larger than the previous one, the ascending sequence length is increased by 1, otherwise the length is reduced by 1, and the smaller number is replaced by the former.




The code is as follows:


#include<iostream>#include<cstdio>#include<algorithm>Constint MAXN=100005;UsingNamespace Std;int a[MAXN],dp[MAXN],lenN;IntFind(int x){int L=1R=len, mid;While(l<=r){Mid= (l+r) >>1;If(x>dp[Mid]) L=mid+1;Elser=mid-1;}return L;}IntMain(){While(scanf("%d", &n)==1){len=0;For(int I=1; I<=n; I++)scanf("%d", &a[I]);For(int I=1; I<=n; I++){int k=find< Span class= "Sh-symbol" > (A[i[k]=a[i];len=max,k}printf ( "%d\n,len}return 0 "              





Lis-program E

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.