uva1471 二叉搜索树
来源:互联网 发布:2016淘宝女装销量前十 编辑:程序博客网 时间:2024/06/06 13:15
此题紫书上面有详细分析,关键是运用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 Firststruct 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>::iteratorint 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;}
如有不当之处欢迎指出!
0 0
- uva1471 二叉搜索树
- uva1471
- uva1471
- 【二叉搜索数】HDU3791二叉搜索树
- 二叉树--二叉搜索树
- 【二叉树】二叉搜索树
- 二叉树- 二叉搜索树
- 【搜索树】二叉搜索树
- 二叉搜索树BSTree
- 二叉搜索树
- 二叉搜索树
- 二叉搜索树
- 最优二叉搜索树
- 二叉搜索树
- 二叉搜索树
- HDOJ3791 二叉搜索树
- 二叉查找树搜索
- 二叉搜索树
- 自定义浏览器滚动条的样式,打造属于你的滚动条风格
- Codeforces Round #395 (Div. 2) A+B
- GZIP概念
- 第一次来写博客
- IntelliJ IDEA单元测试入门
- uva1471 二叉搜索树
- 网管的自我修养-外设相关
- 无需刷新整个Web页面显示服务器响应的当前时间(原始ajax未引入jar包)
- memoEdit 界面卡
- Openwrt netifd解析
- LeetCode twosum C
- ql server 2008评估期已到 不能打开数据库 解决办法
- React/React Native 的ES5 ES6写法对照表
- jQuery--事件案例