POJ 3903 Stock Exchange 解题心得

原题:

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.
 
 
分析:
这题不能用n^2的算法过,要用LIS的nlogn算法过
下面介绍nlogn算法
这个有点贪心的意思了 ,
定义一个变量数组d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则记录最小的那个最末元素。
注意d中元素是单调递增的,下面要用到这个性质。
首先len = 1,d[1] = a[1],然后对a[i]:若a[i]>d[len],那么len++,d[len] = a[i];
否则,我们要从d[1]到d[len-1]中找到一个j,满足d[j-1]<a[i]<d[j],则根据D的定义,我们需要更新长度为j的上升子序列的最末元素(使之为最小的)即 d[j] = a[i];
最终答案就是len
利用d的单调性,在查找j的时候可以二分查找,从而时间复杂度为nlogn。
 
 
 
还不懂可以去这里:http://blog.csdn.net/shuangde800/article/details/7474903
 
 
 
代码:
#include<iostream>
#include<cstdio>
using namespace std;
const int N = 100000 + 10;

int a[N], dp[N];

//                                                                    while循环实现
int binsearch(int *d, int left, int right, int key)
{
    int  mid;
    while (left <= right)
    {
        mid = left + ((right - left) >>1);
        if (d[mid] < key&&d[mid + 1] >= key)
            return mid;
        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;
}
//                                                                                递归实现
//int binsearch(int *d,int left, int right, int key)
//{
//    //     搜索区间   [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){                    //可以优化?
//        left = mid+1;                
//    }
//    else if(d[mid]>key){
//        right = mid - 1;
//    }
//    return binsearch(d, left, right, key);
//}

int main()
{
    //int aa[N] = { 1, 1, 1, 1, 1 };
    //cout << binsearch(aa, 0, 5, 1);
    int n;
    while (cin >> n)
    {
        for (int i = 0; i < n; i++)
        {
            scanf("%d", a + i);
        }
        dp[1] = a[0];
        int len = 1, protected_len;
        for (int i = 1; i < n; i++)
        {
            if (i == 3)
                int oiu=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;
}

 

posted @ 2015-08-15 17:32  Shawn_Ji  阅读(299)  评论(0编辑  收藏  举报