poj 3903(最长上升子序列 )
来源:互联网 发布:肿泡眼双眼皮手术知乎 编辑:程序博客网 时间:2024/05/18 02:01
题目链接:http://poj.org/problem?id=3903
Stock Exchange
Time Limit: 1000MS Memory Limit: 65536KTotal 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.
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.
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 <iostream>#include <string.h>#include <string>#include <cstdio>#include <cmath>#include <stdio.h>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;i<n;i++)scanf("%d",&a[i]); int len=0; d[0]=-INF; for(int i=0;i<n;i++) { if(a[i]>d[len])d[++len]=a[i]; else { int j=find(1,len,a[i]); d[j]=a[i]; } } printf("%d\n",len); } return 0;}
0 0
- poj 3903(最长上升子序列 )
- poj 3903 最长上升子序列
- POJ 3903-最长上升子序列
- POJ 3903 Stock Exchange 最长上升子序列
- poj 3903 最长上升子序列,nlog(n)的算法
- POJ 3903 && 1631 &&2533 最长上升子序列
- poj 3903 Stock Exchange (最长上升子序列)
- 证券交易所 Stock Exchange 最长上升子序列 poj 3903
- poj 3903 求最长上升子序列长度
- POJ 3903 Stock Exchange (LIS:最长上升子序列)
- poj 3903 Stock Exchange 最长上升子序列
- POJ 3903 —— Stock Exchange 最长上升子序列
- POJ 3903 Stock Exchange 最长上升子序列 模板题
- poj 2127(最长公共上升子序列)
- poj 1836 Alignment( 最长上升(下降)子序列 )
- poj 2533 Longest Ordered Subsequence(最长上升子序列)
- POJ 1836 Alignment 【最长上升(下降)子序列】
- POJ 2533 Longest Ordered Subsequence(最长上升子序列)
- 如何让你的bash 定时运行?可…
- linux 下一键移植 乐蛙/…
- PAT 1063. Set Similarity
- linux下文件遍历的批处理操作——签…
- LeetCode-Binary Tree Inorder Traversal
- poj 3903(最长上升子序列 )
- ia32-libs linux64位下无法安…
- 加快git 传输速度的方法——PS:小白…
- 解决相机移植后像素无法识别完全的…
- 使用Roptipng zipalign优化APK
- Android 系统初始值修改
- JDK 1.7 tar。gz下载和使用
- 解决 输入法弹出时,将底部按…
- failed to create the java virtual machine