POJ 3903 Stock Exchange Problem Solving experience

Source: Internet
Author: User
Tags stock prices

Original question:

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

Hint

There is 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. Analysis: This problem can not be used n^2 algorithm, to use the LIS Nlogn algorithm described below NLOGN algorithm this is a bit greedy , define an array of variables D[k]: The last element of the ascending subsequence with a length of k, and if there are multiple ascending sub-sequences with a length of K, the smallest of the least elements is recorded. Note that the elements in D are monotonically increasing, and this property is used below.
First len = 1,d[1] = a[1], then to A[i]: if A[i]>d[len], then len++,d[len] = A[i];
Otherwise, we will find a J in d[1] to d[len-1], according to the definition of D, we need to update the last element of the ascending sub-sequence of length J (to make it the smallest), i.e. d[j] = A[i];
The final answer is Len.
Using the monotonicity of D, when looking for J, we can find two points, thus the time complexity is NLOGN.
  still do not know can go here: http://blog.csdn.net/shuangde800/article/details/7474903 Code:
#include <iostream>#include<cstdio>using namespacestd;Const intN =100000+Ten;intA[n], dp[n];//While Loop implementationintBinsearch (int*d,intLeftintRightintkey) {    intmid;  while(Left <=Right ) {Mid= left + ((right-left) >>1); if(D[mid] < Key&&d[mid +1] >=key)returnmid; if(D[mid] < Key&&d[mid +1] <key) left= Mid +1; if(D[mid] = =key) right= Mid-1; if(d[mid]>key) right= Mid-1; }    return-1;}//Recursive Implementation//int binsearch (int *d,int left, int. right, int key)//{//    //search interval [left, right]//    //if (left > right)//return-1;////int mid = left+ (right-left)/2;//if (D[mid] < Key&&d[mid + 1] >= key)//return mid;//if (d[mid] = = key)//Right = mid-1;////else if (D[mid] < key) {//can be optimized? //Left = mid+1; //    }//else if (d[mid]>key) {//Right = mid-1;//    }//return Binsearch (d, left, right, key);//}intMain () {//int Aa[n] = {1, 1, 1, 1, 1}; //cout << binsearch (aa, 0, 5, 1);    intN;  while(Cin >>N) { for(inti =0; I < n; i++) {scanf ("%d", A +i); } dp[1] = a[0]; intLen =1, Protected_len;  for(inti =1; I < n; i++)        {            if(i = =3)                intoiu=3; if(a[i]>Dp[len]) {Len++; Dp[len]=A[i]; }            Else{Protected_len= Binsearch (DP,1, Len,a[i]); //cout << "protencetd=" << protected_len << ' << a[i] << Endl;                if(Protected_len! =-1) {Protected_len++; Dp[protected_len]=A[i]; }                Else{dp[1] =A[i]; } }} cout<< Len <<Endl; }    return 0;}

POJ 3903 Stock Exchange Problem Solving experience

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.