• POJ 3903 Stock Exchange


    Stock Exchange
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2954   Accepted: 1082

    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
    题目大意:最长上升子序列。
    解题方法:这题由于数据较大,不能采用常规的DP方法来解答,那样时间复杂度为O(n^2),应采用二分,时间复杂度为n * logn。
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    {
        int n, low, mid, high, nLen;
        int num[100005];
        int Stack[100005];
        while(scanf("%d", &n) != EOF)
        {
            nLen = 0;
            Stack[0] = -1;
            for (int i = 1; i <= n; i++)
            {
                scanf("%d", &num[i]);
            }
            for (int i = 1; i <= n; i++)
            {
                if (num[i] > Stack[nLen])
                {
                    Stack[++nLen] = num[i];
                }
                else
                {
                    low = 1;
                    high = nLen;
                    while(low <= high)
                    {
                        mid = (low + high) / 2;
                        if (Stack[mid] < num[i])
                        {
                            low = mid + 1;
                        }
                        else
                        {
                            high = mid - 1;
                        }
                    }
                    Stack[low] = num[i];
                }
            }
            printf("%d
    ", nLen);
        }
        return 0;
    }
  • 相关阅读:
    web端常见兼容性问题整理
    浏览器初始化样式
    html5特效库
    csshack
    进程与线程,并发、并行、异步、多线程
    js复制粘贴事件
    dom range相关
    vue和react在使用上的对比
    ListView往TreView里面拖拽
    ListView添加项目带序列
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3265889.html
Copyright © 2020-2023  润新知