Stock Exchange
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3887 | Accepted: 1402 |
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 1AC代码:
#include <stdio.h>
#include <string.h>
//author:YangSir
int a[100005];
int main(){
int n,i,max,b,num,x;
while(~scanf("%d",&n)){
num=1;
a[0]=-5456456;
scanf("%d",&b);
a[1]=max=b;
for(i=1;i<n;i++){
scanf("%d",&b);//数组的每个值变化的过程就表示子串一个个代入
if(max<b){
max=b;
num++;
a[num]=max;//子串在增长
}
else{
x=num-1;
while(a[x]>=b)
x--;
a[x+1]=b;
}
max=a[num];//max可能会变
}
printf("%d\n",num);
}
return 0;
}
本文介绍了如何解决POJ 3903 Stock Exchange问题,通过动态规划(DP)算法求解最长上升子序列(LIS)的长度,详细解析输入输出格式及样例。
3199

被折叠的 条评论
为什么被折叠?



