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_如何在列表、字典中筛选数据?
实际问题有哪些? 过滤掉列表[3,9,-1,10.-2......] 中负数 筛选出字典{'li_ming':90,'xiao_hong':60,'li_kang':95,'bei_men':98} ...
- sed的替换命令
例1: [root@nhserver2 ~]# cat nagios.txt<TD ALIGN=LEFT valign=center CLASS='statusBGCRITICAL'>&l ...
- 封装的应用【example_Array工具】
定义一个数组工具[ArrayTool]封装其方法,ArrayDemo调用数组工具ArrayTool package new_Object; //封装多个个功能 class ArrayTool{ //1 ...
- Akamai CDN
Akamai CDN中的几个重要组件 mapping system 调度系统(映射client到edge cluster,进而到edge server) edge server platform 边缘 ...
- python交互模式下tab键自动补全
import rlcompleter,readline readline.parse_and_bind('tab:complete')
- Delphi json解析相关
身为一个资深的Delphi 开发者, 最近在做一个小工具的时候,开始捡起来pascal语言. 主要是开发一个内部用的小工具, 主要功能: 1.解析json格式 2.格式化json文件 3.校验json ...
- tensorflow Image 解码函数
觉得有用的话,欢迎一起讨论相互学习~Follow Me tf.image.decode_png(contents, channels=None, name=None) Decode a PNG-enc ...
- oracle学习(一)
作为一个入门选手,怕忘记,所以所有东西都尽量写下来.(省略oracle11g的安装过程) 一.sqlpuls用sys账户登录 (sqlplus是客户端连上服务器的一个工具) 1.使用cmd控制台登录 ...
- ipython的用法详解
ipython是一个升级版的交互式python命令行工具. ipython安装 pip install ipython 等到命令执行完成后显示successfully表示完装成功 在命令提示符下输入i ...
- Nginx负载均衡搭建(Window与Linux)
windows上搭建nginx负载均衡 1.准备几台http服务器软件,这里选用一台apache一台tomcat apache(windows)下载链接:https://www.apachehaus. ...