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 ...
随机推荐
- GPU 实现 RGB -- YUV 转换 (OpenGL)
GPU 实现 RGB -- YUV 转换 前言 RGB --> YUV 转换的公式是现成的,直接在 CPU 端转换的话,只需要遍历每个像素,得到新的 YUV 值,根据其内存分布规律,合理安排分布 ...
- linkin大话面向对象--类和对象
我们每天在撸码,那么我们在敲什么东西呢?明显的我们在写类,写一个类,写一个接口,写某个接口里面写一些属性,在某个类里面写一个方法,然后以一个对象调用方法,对于j2ee来讲的话,可能还会写一些jsp,静 ...
- jQuery的标签选择器$('p')、类选择器$('.myClass')、id选择器$('#myId')
$()可以是$(expresion),即css选择器 $("a")构造的这个对象,是用CSS选择器构建了一个jQuery对象——它选择了所有的<a/>这个标签 $(&q ...
- Android ButterKnife注解式开发
在Android开发中findViewById和setOnClickListener解脱写法. 在任意的一个类中 @Bind(R.id.et) EditText editText; @OnClick( ...
- ajax实现异步校验
1.ajax介绍 见过百度的搜索框吗?当你输入一个关键词,下面立马会出现一些相关的热词,这就是用ajax做到的. 2.环境设想: 有一个注册页面.jsp <span id="mess& ...
- wxpython发布还自己图标的程序
在py2exe安装脚本文件中,修改代码: setup( windows=[ { 'script': 'myapp.py', 'icon_resources': [(1, 'myicon.ico')] ...
- web.xml 文件中一般包括 servlet, spring, filter, listenr的配置的加载顺序
首先可以肯定 加载顺序与他们在web.xml 文件中的先后顺序无关. web.xml 中 listener 和 serverlet 的加载顺序为 先 listener 后serverlet最终得出结果 ...
- Spring整合JDBC及事务处理
1.Spring整合JDBC DAO是数据访问对象(data access object)的简写.接口是实现松耦合的关键,Spring也鼓励使用接口,但不是强制的. 捕获异常时希望能尝试从异常状态中恢 ...
- 给你的jQuery项目赋予Router技能吧
现在你不会React/Vue都不好意思说自己是前端,不过我相信很多前端项目还是基于jquery类库的传统模式的,假如你有追求的态度使用过requireJs这个库,你一定思考过一个问题,或者说一种组件化 ...
- 洛谷 [P1403] 约数研究
本题的思想很好,正难则反 首先如果暴力枚举每个数的约数个数,一定会超时,那么我们就从约数的角度考虑,题目中问的是1~n的约数个数和,那么我们就枚举约数,看每个约数在1~n中出现过几次. #includ ...