POJ3903 5thweek.problem_E LIS

Description

The world financial crisis is quite a subject. Some people are more relaxed while others are quite anxious. John is one of 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 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 are 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

Hint

There are three data sets. In the first case, the length L of the sequence is 6. The sequence is 5, 2, 1, 4, 5, 3. The result for the data set is the length of the longest rising trend: 3.
 
题目分析:
依然是求最长上升子序列问题。方法同B题zoj1093.
提醒自己注意两个方面:1.子序列不要求连续。2、数组越界问题和小于等于的取舍问题。
 
代码如下:
 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int stock[100005],dp[100005];
 5 int main()
 6 {
 7     int n,i,k,left,right,mid;
 8   while(scanf("%d",&n)!=EOF)
 9   {
10       for(i=0;i<n;i++)
11         scanf("%d",&stock[i]);
12      dp[0]=-1;
13      k=0;
14         for(i=0;i<n;i++)
15         {
16             if(stock[i]>dp[k])
17                 dp[++k]=stock[i];
18             else
19             {
20                 left=1;
21                 right=k;
22                 while(left<=right)
23               {
24                  mid=(left+right)/2;
25                  if(stock[i]>dp[mid])
26                     left=mid+1;

27                  else
28                     right=mid-1;
29               }
30               dp[left]= stock[i];
31             }
32 
33         }
34         printf("%d
",k);
35   }
36   return 0;
37 }
原文地址:https://www.cnblogs.com/x512149882/p/4733180.html