zoukankan      html  css  js  c++  java
  • 单调递增子序列(二) ---- 数据特大 , 需要优化

    这个时间复杂度是  n*log n 有了很大的优化 , 并且用stl函数库中的 lower_bound 能简化代码 . 

    这个题用 平时的n^2 的代码的话 是铁定超时的  下面附上  140ms  的代码  

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<vector>
     8 #include<set>
     9 #include<stack>
    10 #include<string>
    11 #include<sstream>
    12 #include<map>
    13 #include<cctype>
    14 #include<limits.h>
    15 using namespace std;
    16 int main()
    17 {
    18     int n,b,dp[100005],location,num;
    19     while(scanf("%d",&n)!=EOF)
    20     {
    21         num=0;
    22         for(int i=0;i<n;i++)
    23         {
    24             scanf("%d",&b);
    25             location=lower_bound(dp,dp+num,b)-dp;    //在 已经有序的数组中二分查找
    26             dp[location]=b;
    27             num=location+1>num?location+1:num;
    28         }
    29         printf("%d
    ",num);
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    [BZOJ3257]树的难题
    [BZOJ4987]Tree
    [NOI2015][洛谷P2150]寿司晚宴
    P2221 [HAOI2012]高速公路
    BUG全集(我遇到的)
    NOIP2018游记
    BZOJ1103
    Google Chrome 优化
    特殊空格
    Ant Design Vue 使用
  • 原文地址:https://www.cnblogs.com/A-FM/p/5426987.html
Copyright © 2011-2022 走看看