zoukankan      html  css  js  c++  java
  • 2015 HUAS Summer Trainning #5 E

    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.
    题目大意:求最大的上升子序列。
     
    代码:
     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 const int maxn=100000+10;
     5 int n,a[maxn],b[maxn],t;
     6 int erfen(int i)
     7 {
     8     int l,r,m;
     9     l=0,r=t-1;
    10     while(l<=r)
    11     {
    12         m=l+(r-l)/2;
    13         if(a[i]>b[m])
    14             l=m+1;
    15         else r=m-1;
    16     }
    17     return l;    
    18 }
    19 int main()
    20 {
    21     int i,j;
    22     while(cin>>n)
    23     {
    24         for(i=0;i<n;i++)
    25         {
    26             cin>>a[i];
    27         }
    28         t=0;
    29         b[0]=a[0];
    30         for(i=1;i<n;i++)
    31         {
    32             if(a[i]>b[t])
    33             {
    34                 b[++t]=a[i];
    35             }
    36             else 
    37             {
    38                 int d=erfen(i);
    39                 b[d]=a[i];
    40             }
    41             
    42         }
    43         cout<<t+1<<endl;
    44         
    45     }
    46     return 0;
    47 }
    View Code

    这里使用的二分查找。
    用2重循环会超时。

     
  • 相关阅读:
    WebBrowser,挖坑,跳坑,填坑
    Random类取随机数小测试
    request.path 值危险
    文件上传
    gitbook serve后报错::Error: ENOENT: no such file or directory……/_book/gitbook/gitbook
    iOS 报错 Undefined symbol: _BZ2_bzDecompressEnd
    iOS operator delete(void*) 和 operator new(unsigned long)的报错解决办法
    [!] CDN: trunk Repo update failed
    iphone手机系统回退、降级
    iphone手机型号获取
  • 原文地址:https://www.cnblogs.com/huaxiangdehenji/p/4734829.html
Copyright © 2011-2022 走看看