Codeforces Round #353 (Div. 2) D. Tree Construction (构造二叉搜索树)
来源:互联网 发布:网络歌曲磨剪子抢菜刀 编辑:程序博客网 时间:2024/06/08 09:58
During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help.
You are given a sequence a, consisting of n distinct integers, that is used to construct the binary search tree. Below is the formal description of the construction process.
- First element a1 becomes the root of the tree.
- Elements a2, a3, ..., an are added one by one. To add element ai one needs to traverse the tree starting from the root and using the following rules:
- The pointer to the current node is set to the root.
- If ai is greater than the value in the current node, then its right child becomes the current node. Otherwise, the left child of the current node becomes the new current node.
- If at some point there is no required child, the new node is created, it is assigned value ai and becomes the corresponding child of the current node.
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the length of the sequence a.
The second line contains n distinct integers ai (1 ≤ ai ≤ 109) — the sequence a itself.
Output n - 1 integers. For all i > 1 print the value written in the node that is the parent of the node with value ai in it.
31 2 3
1 2
54 2 3 1 6
4 2 2 4
Picture below represents the tree obtained in the first sample.
Picture below represents the tree obtained in the second sample.
题解:
给你一堆结点,按照题目规则叫你构造一棵二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲。
n有100000,如果直接建树可能会TLE。因为这是一棵二叉搜索树。所以我们可以利用二叉搜索树的性质。
因为对于当前输入的结点V,找到已经输入的最大的Left和最小的Right,其中Left< V < Right。其中,Left和Right必定是祖先和后代的关系,(不可能有公共祖先的)。对于当前的输入的结点V必定是Left的左子树,或者Right的右子树。这两个位置一定有一个位置是当前结点V的。
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")//#include<bits/stdc++.h>#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<cstring>#include<vector>#include<map>#include<cmath>#include<queue>#include<set>#include<stack>#include <utility> using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int,int> pii;typedef vector<int> vi;#define mst(a) memset(a, 0, sizeof(a))#define M_P(x,y) make_pair(x,y)#define in freopen("in.txt","r",stdin) #define rep(i,j,k) for (int i = j; i <= k; i++) #define per(i,j,k) for (int i = j; i >= k; i--) #define lson x << 1, l, mid #define rson x << 1 | 1, mid + 1, r const int lowbit(int x) { return x&-x; } const double eps = 1e-8; const int INF = 1e9+7; const ll inf =(1LL<<62) ;const int MOD = 1e6 + 3; const ll mod = (1LL<<32);const int N =1e6+6; const int M=100010;//const int maxn=1001; template <class T1, class T2>inline void getmax(T1 &a, T2 b) {if (b>a)a = b;} template <class T1, class T2>inline void getmin(T1 &a, T2 b) {if (b<a)a = b;}int read(){int v = 0, f = 1;char c =getchar();while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();return v*f;}set<int> num;map<int,int> Left,Right;int n;void init(){num.clear();Left.clear();Right.clear();}int main(){int v;cin>>n;init();cin>>v;num.insert(v); //树根vector<int> ans;for(int i=0;i<n-1;i++){cin>>v;set<int>::iterator it = num.upper_bound(v);if(it != num.end() && Left[*it] == 0) {ans.push_back(*it); Left[*it] = 1;}else{ --it;ans.push_back(*it);Right[*it] = 1 ;}num.insert(v); }for(int i=0;i<ans.size()-1;i++)cout<<ans[i]<<" ";cout<<ans[ans.size()-1]; return 0;}
- Codeforces Round #353 (Div. 2) D. Tree Construction (构造二叉搜索树)
- Codeforces Round #353 (Div. 2) D. Tree Construction (二叉搜索树+set)
- Codeforces Round #353 (Div. 2) D. Tree Construction
- Codeforces Round #353 (Div. 2)D. Tree Construction
- Codeforces Round #353 (Div. 2) D. Tree Construction (BST)
- Codeforces Round #353 (Div. 2) D. Tree Construction
- Codeforces Round #353 (Div. 2) D. Tree Construction __ Binary Search Tree
- Codeforces Round #353 (Div. 2) D. Tree Construction (BST询问父亲节点)
- Codeforces Round #353 (Div. 2) D. Tree Construction(每个点按照二叉树排序树的插入插入到树中,问最后每个点的父亲节点的值是多少)
- Codeforces Round #363 (Div. 2) Problem D - Fix a Tree(并查集 + 构造)
- Codeforces Round #158 (Div. 2)D. Black and White Tree(搜索 + 思维)
- Codeforces 675D Tree Construction【构造,BST】
- Codeforces Round #309 (Div. 2) D 构造
- 解题报告:Codeforces Round #432 (Div. 1) D. Tournament Construction (DP+构造)
- Codeforces Round #340 (Div. 2)(D)构造
- Codeforces Round #427 (Div. 2)-D(记忆化搜索)
- codefocres#353-D - Tree Construction-构造
- Codeforces Round #375 (Div. 2) -- F. st-Spanning Tree(并查集 构造生成树)
- 微信公众号开发---群发消息
- Windows驱动调试环境配置
- 十二.分支管理
- 《OpenGL ES 2.0 Programming Guide》第12章 “最简单的本地纹理+FBO+VBO”示例代码【C语言版】
- js创建蛇形(螺旋)N*N矩阵,顺时针增加
- Codeforces Round #353 (Div. 2) D. Tree Construction (构造二叉搜索树)
- Android消息机制
- Java基础-网络编程学习
- session机制
- DVB系统中PCR的生成和PCR校正
- 通过反编译深入理解Java String及intern
- 微信公众号开发---实现微信自定义菜单创建以及消息回复等
- 十三.创建与合并分支
- spring DelegatingFilterProxy,targetFilterLifecycle的作用