Stock Exchange POJ 3903/ TOJ 3120

来源:互联网 发布:淘宝上正品球鞋店 编辑:程序博客网 时间:2024/06/05 10:41
 1036K235MSC++1533B
Stock Exchange
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 2380
Accepted: 855

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.

一开始,用动态规划没有过,对于优化问题,就是删除动态规划的状态。(动态规划时间复杂度是O(N*N))
因为是升序列,所所以可以通过二分查找来降低时间复杂度O(NlgN)

对于一个序列来说
如果输入是
1 7 2 6 3 5 4 5
那么输出就是1 2 3 4 5
但是如何来的呢?

在动态规划里面每次找到下一个数字的时候和前面所有的数来比较。在动态规划中有下列状态
ARRAY:1 7 2 6 3 5 4 5                例子1
State:      1 2 2 3 3 4 4 5
对于更一般的序列来说
ARRAY: 1 7 6 3 4 5 2                 例子2
State:          1 2 2 2 3 4 2
这里我们发现State没有顺序,但是每次比较的只是ARRAY中的某个数字和相同State的数字的最小数字进行比较。
对于例子1来说,就是State = 1 --min(1)
                                    State = 2 --min(7 2)
                                    State = 3 --min(6 3)
                                    State = 4 --min(5 4)
                                    State = 5 --min(5)
这个例子有点特殊,更一般的例子2 来说,我们不知道何时数字来,就像例子2中的ARRAY最后一个2一样,那么当2 来到时候,可以对之前已经的
State       1     2       3     4  
Update    1  7/6/3    4     5
进行二分查找,来降低搜素时间。
#include <iostream>using namespace std;#define MAX 100100#define FIRST -1int BiSearchloc(int lng,int Ra,int comp[]){    int beg = 0;     int end = lng;    while(beg <= end){           int mid =(beg+end)/2;            if(Ra > comp[mid]){           beg = mid +1;                   }        else if( Ra < comp[mid])         {           end = mid -1;              }        else           return mid;         }    return beg; } int main(){    int a[MAX];    int b[MAX]={FIRST};    int i=0,j=0;    int N = 0;    while(cin >> N){       int lng = 0;       for(i=0;i<N;i++){           cin>> a[i];                          }             for(j=0;j<N;j++){               if(j==0)               {                  b[0] = a[0];                  lng = 0;                  continue;               }               //////               if(a[j]<b[0]){                  b[0] = a[j];                  continue;               }               if((a[j]==b[lng])||(a[j]==b[0]))                  continue;                              if(a[j]>b[lng]){               //如果数组A元素比B的最大值大的话                                       b[++lng]=a[j];                    continue;                                                                                             }               int r = BiSearchloc(lng-1,a[j],b);               if(a[j]<b[r])               b[r]=a[j];                         }                cout << lng+1<<endl;    }    system("pause");    return 0;}


原创粉丝点击