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重循环会超时。

     
  • 相关阅读:
    C++内存泄露的有效预防方法:谁使用,谁删除 (1.2)
    工作日志2014-08-28
    【2012.1.24更新】不要再在网上搜索eclipse的汉化包了!
    关于ActionContext.getContext()的使用方法心得
    Android开发(20)--RadioGroup的使用
    站点防止攻击
    小强的HTML5移动开发之路(50)——jquerymobile页面初始化过程
    我是怎样成长为系统架构师的
    辛星站点架构师笔记第四篇
    strcpy_s与strcpy的比較
  • 原文地址:https://www.cnblogs.com/huaxiangdehenji/p/4734829.html
Copyright © 2011-2022 走看看