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 ...
随机推荐
- mybatis分页+springmvc+jsp+maven使用步骤
作者注:本文主要用于个人学习.复习.同时欢迎指导讨论 1,添加maven依赖<dependency> <groupId>com.github.miemiedev ...
- ClearCase创建视图与基本命令
1.创建和设置view cleartool mkview -tag King_dev /home/King/King_dev.vws cleartool setview King_dev 2.删除V ...
- angular4在prod模式下的JIT编译问题
最近利用angular4开发一个项目,由于画面中的显示都是从数据表中读取,通过设置显示FLAG和显示顺序对画面布局按既定规则控制的, 所以必须利用动态编译实现. 方法如下, 1,获取JitCompil ...
- oracle的分组查询和连接查询
分组函数: 六个常用的分组函数: AVG,SUM,MIN,MAX,COUNT,WM_CONCAT: 行转列 PS:分组函数默认会自动过滤控制,可以使用NVL函数使分组函数无法忽略空值: 未使用NVL函 ...
- python selenium 鼠标悬停
#鼠标悬停 chain = ActionChains(driver) implement = driver.find_element_by_link_text() chain.move_to_elem ...
- [js] 如何 在 jQuery 中的 $.each 循环中使用 break 和 continue
jQuery中each类似于javascript的for循环 但不同于for循环的是在each里面不能使用break结束循环,也不能使用continue来结束本次循环,想要实现类似的功能就只能用ret ...
- iOS-xcode代码统计
作为开发者,想不想知道自己写了多少行代码吗,打开终端,进入项目文件夹,然后进入想统计的某个文件夹,也可以直接在当前项目文件夹,然后终端输入下面的代码就可以了 find . "(" ...
- NOIP 2017 Day 0. 游记
刚从曲师大试机回来... 不巧,我抽到了和去年一样的考场,还是那么难用的XP,还是那么难用的键盘. 似乎在考场上有一股奇怪的力量,我本来在自己电脑上打板子打的没那么快,但是试机的那段时间..说出来你们 ...
- chrome_options
用法 from selenium.webdriver.chrome.options import Options chorme_option=Options() chorme_option.add ...
- 应用负载均衡之LVS(二):VS_TUN和VS_DR的arp问题
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...