Reprint Please specify source: http://blog.csdn.net/u012860063
Topic Links:
id=3903 ">http://poj.org/problem?id=3903
Description
The world financial crisis is quite a subject. Some people is more relaxed while others is quite anxious. John is one of the 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 the 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 is 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
The first type: http://blog.csdn.net/u012860063/article/details/34086819
The code is as follows:
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int a[100047],dp[ 100047],n;int bin (int size,int k) { int l = 1,r = size; while (l<=r) { int mid = (l+r)/2; if (K>dp[mid]) L = mid+1; else r = mid-1; } return l;} int LIS (int *a) { int i,j,ans=1; DP[1] = a[1]; for (i = 2; i<=n; i++) { if (a[i]<=dp[1]) j = 1; else if (A[i]>dp[ans]) j = ++ans; else j = Bin (Ans,a[i]); DP[J] = A[i]; } return ans;} int main () {int i;while (~scanf ("%d", &n)) {for (i = 1; I <= n; i++) {scanf ("%d", &a[i]);} int ans = LIS (a);p rintf ("%d\n", ans); return 0;}
Another type of use: Lower_bound
The code is as follows:
#include <cstdio> #include <algorithm>using namespace std; #define INF 0x3fffffffint a[100047],dp[100047]; int main () {int t,i;while (scanf ("%d", &t)!=eof) {for (i = 0; I <= t; i++) dp[i]=inf;for (i = 0; i < T; i++) {scanf ("%d" , &a[i]);} for (i = 0; i < T; i++) {*lower_bound (Dp,dp+t,a[i]) =a[i];} printf ("%d\n", Lower_bound (Dp,dp+t,inf)-DP);} return 0;}
Attach a picture of the explanation:
poj3903 Stock Exchange (Longest ascending subsequence)