zoukankan      html  css  js  c++  java
  • 51Nod 1134 最长递增子序列(动态规划O(nlogn))

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <stdio.h>
     4 #define MAXN 50010
     5 using namespace std;
     6 
     7 const int MIN = -1e9;
     8 
     9 int main(void){
    10     int n, a[MAXN], vis[MAXN], len = 1; 
    11     scanf("%d", &n);
    12     for (int i = 0; i<n; i++){
    13         scanf("%d", &a[i]);
    14     }
    15     for (int i = 0; i <= n; i++){
    16         vis[i] = MIN;
    17     }
    18 
    19     //vis[i]表示满足递增数量i的最小值 
    20     vis[1] = a[0];
    21     for (int i = 1; i<n; i++){
    22         //upper_bound(vis + 1, vis + len + 1, a[i]) 返回被查序列中第一个大于查找值的指针
    23         int pos = upper_bound(vis + 1, vis + len + 1, a[i]) - vis;
    24         vis[pos] = a[i];
    25         if (len<pos){ //维护最大长度
    26             len = pos;
    27         }
    28     }
    29     printf("%d
    ", len);
    30     return 0;
    31 }
  • 相关阅读:
    2020年12月18日
    2020年12月17日
    2020年12月16日
    2020年12月15日
    2020年12月14日
    排序算法总结
    2020微软笔试题
    子串、子序列问题
    剑指offer题解(完结)
    ONOS 2.2安装
  • 原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/8726917.html
Copyright © 2011-2022 走看看