zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Linux日常之命令sort
    Linux日常之命令sed
    Linux日常之命令grep
    Linux日常之命令awk
    Linux日常之命令tee
    Linux日常之数据重定向
    Hibernate打印SQL及附加参数
    使用D3 Geo模块画澳大利亚地图
    基于Spring-WS的Restful API的集成测试
    做项目时需要考虑的安全性问题
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3265889.html
Copyright © 2011-2022 走看看