此题紫书上面有详细分析,关键是运用Set优化实现O(nlgn)复杂度

AC代码:

#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;

const int maxn = 2e5+5;
int num[maxn], h[maxn], g[maxn];

//g[i] - num[i] is the Last
//h[i] - num[i] is the First

struct node{
    int val, lenth;
    node(){}
    node(int val, int lenth):val(val), lenth(lenth){}
    bool operator < (const node &p) const {
        return val < p.val;
    }
};

#define It set<node>::iterator

int solve(int n){
    set<node> Set;
    int ans = g[0];
    Set.insert(node(num[0], g[0]));
    for(int i = 1; i < n; ++i){
        node c(num[i], g[i]);
        It it = Set.lower_bound(c); //得到迭代器
        bool ok = 1; //keep
        if(it != Set.begin()){
            node pre = *(--it);
            ans = max(ans, pre.lenth + h[i]);
            if(pre.lenth >= c.lenth) ok = 0;
        }
        if(ok){ //intsert C
            Set.erase(c);
            Set.insert(c);
            it = Set.find(c);
            it++;
            while(it != Set.end() && it->lenth <= c.lenth) Set.erase(it++);
        }
    }
    return ans;
}

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; ++i) scanf("%d", &num[i]);
        // 处理g[i]
        g[0] = 1;
        for(int i = 1; i < n; ++i) {
            if(num[i] > num[i-1]) g[i] = g[i-1] + 1;
            else g[i] = 1;
        }
        // 处理h[i]
        h[n-1]=1;
        for(int i = n-2; i >= 0; --i) {
            if(num[i] < num[i+1]) h[i] = h[i+1] + 1;
            else h[i] = 1;
        }
        printf("%d\n",solve(n));
    }
    return 0;
}

如有不当之处欢迎指出!

uva1471 二叉搜索树的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  4. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  5. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  6. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  7. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  8. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  9. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. 用Python实现几种排序算法

    #coding=utf-8 # 1 快速排序算法 def qksort(list): if len(list)<=1: return list else: pivot = list[0] les ...

  2. Servlet--ServletConfig接口,GenericServlet类

    ServletConfig接口 定义:public interface ServletConfig 这个接口定义了一个对象, 通过这个对象, Servlet 引擎配置一个 Servlet 并且允许 S ...

  3. [Nginx]单机环境的多应用配置

    # 服务层 # https://github.com/farwish/alconservice # alconservice.conf server { listen 8090; root /home ...

  4. phantomjs集成到scrapy中,并禁用图片,切换UA

    phantomjs是一个没有界面的浏览器,支持各种web标准,提供DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG,对于爬取一些经过js渲染的页面非常有用.但是phantomj ...

  5. http目录显示时间与服务器相差8小时

    一直用nginx做http服务,代码里访问过文件地址,并未认真关注过访问http目录下的时间戳.今天浏览文件的时候发现一个问题.web上显示的文件时间戳与服务器时间相比差8个小时.具体表现看下图: w ...

  6. BZOJ 4269: 再见Xor [高斯消元 线性基]

    4269: 再见Xor Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. 我太愚蠢了连数组开小了以及$2^{ ...

  7. xcode7中使用cocos2d-x3.8的webview控件

    在XCode7中使用cocos2d-x 3.3以上版本的WebView控件时,碰到了编译错误 App Transport Security has blocked a cleartext HTTP ( ...

  8. Maven入门知识介绍

    1.1 Maven简介 Apache Maven 是一个软件项目管理工具.基于项目对象模型的概念,Maven可用来管理项目的依赖.编译.文档 等信息. 使用maven管理项目时,项目的依赖的jar包将 ...

  9. NIO下_使用示例

    一.分散与聚集 1.分散读取(Scattering Reads):将通道中的数据分散到多个缓冲区中 2.聚集写入(Gathering Writes):将多个缓冲区中的数据聚集到通道中 public v ...

  10. Linux下jdk环境配置

    1.下载jdk http://www.oracle.com/technetwork/java/javase/downloads/index.html 我选择64位的版本 jdk-8u121-linux ...