zoukankan      html  css  js  c++  java
  • 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.
    大意:最长上升趋势(最长上升子序列)
    用二分法logn防止爆
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    int a[100005], num[100005];
    
    int dichotomy(int len ,int n){
    int left = 1,right = len;
    int mid;
    while(left <= right){
        mid = (right + left)/2;
        if(num[mid] == n ) return mid;
        else if(num[mid] > n) right = mid - 1;
        else if(num[mid] < n) left = mid + 1;//如果后面的数小于前面的书就把原来的给覆盖掉,如果后面数大于前面的数,就加一个,大多数情况是超出了while的条件出去的,跟mid关系不大.
          }
    return left;
    }
    int main(){
    int n;
    while(~scanf("%d",&n)){
          for(int i = 1; i <= n; i++){
            scanf("%d", &a[i]);
          }
          num[1] = a[1];
          int len = 1;
          for(int i = 1; i <=   n; i++){
        int     j = dichotomy(len,a[i]);
            num[j] = a[i];
            if(j > len)
             len = j;//记录上升的个数
          }
        printf("%d
    ", len);
            }
        return 0;
    
    }
    View Code
  • 相关阅读:
    linux升级node版本
    mysql视图
    mysql全文本搜索
    mysql数据处理函数
    mysql数据分组
    mysql组合查询
    Django添加生产环境配置
    费用保险单,如何失焦时自动补零
    div+css滚动条
    phpadmin导入数据提示文件最大限制的修改方法
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4313513.html
Copyright © 2011-2022 走看看