题目链接:http://poj.org/problem?id=3903
Stock Exchange
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 4263 |
|
Accepted: 1523 |
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.
Source
Southeastern European Regional Programming Contest 2008
思路:(1)如果n的规模比较小那么可以用朴素的O(n*n)的dp算法求解;
(2)但是这里n的规模比较大,朴素算法会超时,得用另一种O(n*logn)的算法;参考自:http://blog.sina.com.cn/s/blog_76344aef0100scyq.html
附上代码:
#include
#include
#include
#include
#include
#include
const int maxn=100100;
const int INF=99999999;
using namespace std;
int a[maxn];
int d[maxn];
int find(int l,int r,int key)
{
if(l==r)return l;
int mid=(l+r)/2;
if(key>d[mid])return find(mid+1,r,key);//这里就把a[]当作d[]wrong了好多次
else return find(1,mid,key);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;id[len])d[++len]=a[i];
else
{
int j=find(1,len,a[i]);
d[j]=a[i];
}
}
printf("%d\n",len);
}
return 0;
}