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 ...
随机推荐
- linkin大话面向对象--初始化块
java使用构造器来对单个对象进行初始化操作,使用构造器先完成整个java对象的状态初始化,然后将java对象返回给程序,从而让整个java对象的信息更加完整.与构造器作用非常类似的是初始化块,它也可 ...
- MS SQL 监控磁盘空间告警
这几天突然有个想法:希望能够自动监控.收集数据库服务器的磁盘容量信息,当达到一个阀值后,自动发送告警邮件给DBA,将数据库磁盘详细信息告知DBA,提醒DBA做好存储规划计划,初步的想法是通过作业调用存 ...
- Tomcat xxx unbound
从别的地方import的代码 .出现apache unbound 第一步:选中项目右键Build Path->Configure Build Path-->选中Tomcat 7.0 unb ...
- JDBC (五)
1 使用dbutils进行一对多.多对多的开发 1.1 准备 mysql驱动的pom.xml <!-- https://mvnrepository.com/artifact/mysql/mysq ...
- jdk源码->多线程->Thread
线程的创建 java提供了三种创建线程的方法: 通过继承 Thread 类本身: 通过实现 Runnable 接口: 通过 Callable 和 Future 创建线程. 继承Thread类 步骤: ...
- http_build_query()函数使用方法
http_build_query()函数的作用是使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串. 写法格式:http_build_query ( mixed $query ...
- cat写入数据
1.cat可以利用两个>>把内容追加到文件中 cat >>oldboy.txt<<EOF >1 >2 >EOF 会在文件中加入EOF中间的数据.E ...
- pwd 的“P”选项
1.目录是链接目录时,pwd -P 显示出实际路径,而非使用连接(link)路径:pwd显示的是连接路径 例: [root@localhost soft]# cd /etc/init.d [root ...
- MUI 图片上传实现
HTML代码 <!doctype html> <html> <head> <meta charset="UTF-8"> <ti ...
- 安装puppeteer
Puppeteer是一个node库,他提供了一组用来操纵Chrome的API,默认headless也就是无UI的chrome,也可以配置为有UI. 其实有点类似于PhantomJS,但Puppetee ...