POJ 3903 Stock Exchange (E

POJ 3903    Stock Exchange  (E - LIS 最长上升子序列)

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/E

题目:

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.
 
题意:
给出一个序列,求序列中的(严格)最长上升子序列。LIS
 
分析:
1.LIS。求最长上升子序列
2. 要考虑时间复杂度。因为n为1000000,如果用n^2的算法肯定超时,所以要选择nlogn的算法。(百度的),用二分找最优值
3.用cnt保存最长上升子序列的值。b[cnt++]=a[i]
 
代码:
 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 
 5 const int maxn=100005;
 6 
 7 int i;
 8 int n,cnt;
 9 int a[maxn],b[maxn];
10 
11 int LIS()
12 {
13     cnt=0;
14     b[0]=0;
15     for(i=0;i<n;i++)
16     {
17         if(cnt==0||a[i]>b[cnt])
18         {
19             cnt++;
20             b[cnt]=a[i];  //保存最长上升子序列的个数
21         }
22         else
23         {
24             int l=1,r=cnt;
25             while(l<=r)  //二分比较a[]和b[]中值的大小关系
26             {
27                 int mid=(l+r)/2;
28                 if(a[i]>b[mid])
29                     l=mid+1;
30                 else
31                     r=mid-1;
32             }
33             b[l]=a[i];
34             
35         }
36     }
37     return cnt;
38 }
39 
40 int main()
41 {
42     while(scanf("%d",&n)!=EOF)
43     {
44         for(i=0;i<n;i++)
45             scanf("%d",&a[i]);
46         LIS();
47         printf("%d
",cnt);
48     }
49     return 0;
50 }

开始调试的时候,只要输入一个数就输出1,一直没有找出来错误,找别人帮忙来看也没有找出,后来才发现是因为写的时候把scanf中的%d写成了d%,真是好坑啊。。。。。

下次一定要好好注意这些小地方

 
 
原文地址:https://www.cnblogs.com/ttmj865/p/4728551.html