Codeforces Round #353 (Div. 2) D. Tree Construction (构造二叉搜索树)

来源:互联网 发布:网络歌曲磨剪子抢菜刀 编辑:程序博客网 时间:2024/06/08 09:58
D. Tree Construction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

  1. First element a1 becomes the root of the tree.
  2. 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:
    1. The pointer to the current node is set to the root.
    2. 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.
    3. 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.
Input

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

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.

Examples
input
31 2 3
output
1 2
input
54 2 3 1 6
output
4 2 2 4
Note

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;}



2 0
原创粉丝点击