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

     
  • 相关阅读:
    好用的开源产品搜集;开源软件,开源系统,开源项目;
    windows10 双系统安装后,grub2 引导修复(亲自实验);grub2 命令行 手动加载内核;fedora 29 系统grub2引导修复;
    C 实战练习题目40
    C 实战练习题目39
    C 实战练习题目38
    C 实战练习题目37 – 排序
    C 实战练习题目36 – 求100之内的素数
    C 实战练习题目35 -字符串反转
    C 实战练习题目34
    C 实战练习题目33 – 质数(素数)判断
  • 原文地址:https://www.cnblogs.com/huaxiangdehenji/p/4734829.html
Copyright © 2011-2022 走看看