此题紫书上面有详细分析,关键是运用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. Git知识总览(四) git分支管理之rebase 以及 cherry-pick相关操作

    上篇博客聊了<Git知识总览(三) 分支的创建.删除.切换.合并以及冲突解决>,本篇博客我们主要来看一下 rebase 变基相关的操作.rebase 操作和 merge 操作最终都可以达到 ...

  2. maven-assembly-plugin插件的使用方法

    一. Assembly 是什么意思? 二. maven-assembly-plugin是什么? 它是maven中针对打包任务而提供的标准插件. 三. maven-assembly-plugin插件的作 ...

  3. JFreeChart绘制折线图实例

    JFreeChart是JAVA平台上的一个开放的第三方图表绘制类库.只要下载JFreeChart的类库,导入项目即可使用.下面是一个绘制折线图的实例.各处注释都已经写的比较清晰了. package c ...

  4. Matlab 多项式拟合、稳健滤波等实用函数

    Function summary http://www.biomecardio.com/matlab/index.html clinspace Curvilinearly spaced points ...

  5. Git分支高级管理[四]

    标签(linux): git 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 切换分支 git checkout 撤销对文件的修改 git checkout -- ...

  6. 主函数特别之处:public static void main(String[] args)

    public static void main(String[] args) public class Test_java {//主函数特殊之处 public static void main(Str ...

  7. JDK8 HashMap 源码解析

    HashMap中数据结构 在jdk1.7中,HashMap采用数组+链表(拉链法).因为数组是一组连续的内存空间,易查询,不易增删,而链表是不连续的内存空间,通过节点相互连接,易删除,不易查询.Has ...

  8. 2018Pycharm激活方法

    1.将"0.0.0.0 account.jetbrains.com"添加到hosts文件中 2.打开http://idea.lanyus.com/ 3.获取激活码,粘贴到第二个选项 ...

  9. 基于flash的web视频对讲直播测试

    由于项目的需求,要在web上实现视频通话的需求.现成的方案有WebRTC,基于浏览器的成熟方案,但是这个方案和公司项目需求有几个点冲突.后来考虑到基于flash的方案.参考雷神的博客 simplest ...

  10. hive权威指南<一>

    一.ETL介绍: 数据抽取:把不同的数据源数据抓取过来,存到某个地方 数据清洗:过滤那些不符合要求的数据或者修正数据之后再进行抽取 不完整的数据:比如数据里一些应该有的信息缺失,需要补全后再写入数据仓 ...