• 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
  • 相关阅读:
    XML注释
    Mysql 查询不重复数据
    java 支持跨域
    蓝桥杯历届试题-打印十字图--Java
    以流的方式读取url中的参数
    写log日志,并读取log日志
    Java Memcached的使用
    PageInfo实现分页
    java swing 窗口在屏幕中默认显示的位置 居中位置显示
    简易计算器布局 BorderLayout JPanel GridLayout 混合
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4313513.html
Copyright © 2020-2023  润新知