Codeforces Round #353 (Div. 2) D. Tree Construction (BST)

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
3
1 2 3
output
1 2
input
5
4 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個值,由輸入順序按照二叉排序樹(bst)的方法構造樹,構造完畢後問:除了根節點以外每個節點的父節點是值爲多少?

分析:

由於n的取值較大,在極端情況下直接構造BST的複雜度爲O(n^2),不可取。

參考了題解http://www.cnblogs.com/helenawang/p/5501857.html,裏面解釋的非常好。

由於BST存在極端不平衡情況可能超時,插入過程中需對BST進行維護,構造成AVL可以保證複雜度爲O(nlogn)。

在看題解的時候數據結構學的具體構造與旋轉已經忘的差不多了,看代碼發現並不需要構造出AVL,利用set的lower_bound可以很簡單的實現平衡性。

通過分析一個待插入節點v的大於v的第一個元素與小於v的第一個元素之間關係,可以找到具體插入方法。具體可以參考題解。

代碼十分優美:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
const int N=100005;
const int mod=1e9+7;

int a[N];

struct Node {
    int d;
    int lc,rc;
    Node(){}
    Node(int d):d(d),lc(-1),rc(-1){}
}nodes[N];

int main() {
    int n;
    while (cin>>n) {
        for (int i=0; i<n; i++) {
            scanf("%d",&a[i]);
        }
        set<int> s;
        map<int, int> left;
        map<int, int> right;
        int res;
        s.insert(a[0]);
        for (int i=1; i<n; i++) {
            set<int>::iterator pos=s.lower_bound(a[i]);
            if (pos!=s.end()&&left.count(*pos)==0) {
                res=*pos;
                left[res]=a[i];
            } else {
                pos--;
                res=*pos;
                right[res]=a[i];
            }
            printf("%d ",res);
            s.insert(a[i]);
        }
        cout<<endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章