uva1471 二叉搜索树
此题紫书上面有详细分析,关键是运用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 二叉搜索树的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [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 ...
- [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 ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [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 ...
- [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. 这道 ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [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 ...
随机推荐
- Python程序的执行方式
Python代码有两种执行方式: 一.文件执行 二.交互器执行(推荐) 一.文件执行 1.用 notepad++ 或 Sublime Text,甚至 写字本创建一个文件. 2.比如:print('He ...
- myeclipse取消js校验
最近玩一个新的项目,项目里面集成了别的项目,在从SVN上第一次荡下来的时候编译的时候老是校验jq文件,老是被卡主,设置myeclipse环境的时候我已经取消了所有的js校验了,但是还是不行.恼火之余, ...
- android 软键盘的使用
AndroidManifest.xml文件中界面对应的<activity>里加入android:windowSoftInputMode="adjustPan" 键盘 ...
- 常用Nagios配置命令
cd /usr/local/nagios/etc vim nagios.cfg /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios ...
- SQL语句-delete语句
delete语句 delete语句用于删除表中已经存在的整行数据 Tbl_name关键词代表删除数据的目标表 Where子句代表被删除数据的满足条件,如果没有where子句则代 表所有表数据都删除 O ...
- 101490E Charles in Charge
题目连接 http://codeforces.com/gym/101490 题目大意 你有一张图,每两点之间有一定距离,计算出比最短路大x%之内的路径中最长边的最小值 分析 先跑一遍最短路,然后二分答 ...
- office2013使用空格符
空格符:所有的空格都以点表示出来. 作用:挑出空格符后,可以很方便的看到所有的空格,分页符等,使页面更容易调整的工整(在写正规的word文档时,需要这样做). 用法:很简单,根据图片上操作的顺序即可调 ...
- *C语言有关指针的变量声明中的几个易错点
转至:http://my.oschina.net/ypimgt/blog/108265 Technorati 标签: 指针, typedef, const, define 我们都知道,至少听说过 ...
- 使用facebook和twitter进行分享经验总结
凤凰涅槃,浴火重生. 在传说当中,凤凰是人世间幸福的使者,每五百年,它就要背负着积累于在人间的所有痛苦和恩怨情仇,投身于熊熊烈火中自焚,以生命和美丽的终结换取人世的祥和与幸福.同样在肉体经受了巨大的痛 ...
- [DeeplearningAI笔记]02_3.1-3.2超参数搜索技巧与对数标尺
Hyperparameter search 超参数搜索 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.1 调试处理 需要调节的参数 级别一:\(\alpha\)学习率是最重要的需要调节的 ...