此题紫书上面有详细分析,关键是运用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. 函数式编程--使用lambda表达式

    前面一篇博客我们已经说到了,lambda表达式允许使用更简洁的代码来创建只有一个抽象方法的接口的实例.现在我们来写一段java的命令者模式来自己研究下lambda表达式的语法. 这里重复下命令者模式: ...

  2. linkin大话设计模式--命令模式

    linkin大话设计模式--命令模式 首先考虑一种应用情况,某个方法需要完成某一个功能,这个功能的大部分功能已经确定了,但是有可能少量的步骤没法确定,必须等到执行这个方法才可以确定. 也就是说,我们写 ...

  3. tcpdump 使用

    例子: 首先切换到root用户 tcpdump -w  aaa.cap   -i eth7   -nn -x  'port  9999'  -c  1 以例子说明参数: -w:输出到文件aaa.cap ...

  4. Node-debug方法

    本文使用配置node-inspector配合chorme完成debug(编辑器使用SublimeText3). 1.用命令行进入安装node的目录,使用npm install -g node-insp ...

  5. css正三角倒三角

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  6. ABP 多租户 对应多数据库 租户启动报错

    什么是多租户? “软件多租户是指一个软件体系结构,其中一个软件实例在一个服务器上运行,并为多个租户提供服务*租户是一组共享具有软件实例特定权限的公共访问权限的用户. 架构中,软件应用程序旨在为每个租户 ...

  7. es随想一

    一.安全问题 前段时间网上看到的,大量的MongoDB服务器和ElasticSearch服务器被黑客攻击,留下勒索信息. ElasticSearch的2种客户端连接方式,transport方式默认的9 ...

  8. java中的各种Queue

    java中的各种并发Queue可以归为以下的几种: ConcurrentLinkedQueue: 一个由链表结构组成的非阻塞队列 ArrayBlockingQueue :一个由数组结构组成的有界阻塞队 ...

  9. java web 获取客户端操作系统信息

    package com.java.basic.pattern; import java.util.regex.Matcher; import java.util.regex.Pattern; /** ...

  10. 测试SM图床