zoukankan      html  css  js  c++  java
  • codeforces675D Tree Construction

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

    本文作者:ljh2000
    作者博客:http://www.cnblogs.com/ljh2000-jump/
    转载请注明出处,侵权必究,保留最终解释权!

    Description

    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.

    正解:set维护平衡树

    解题报告:

      以前做过的题,只需维护前驱后继即可。

      ps:特判两边相等的情况。

    //It is made by ljh2000
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <ctime>
    #include <vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <string>
    #include <complex>
    using namespace std;
    typedef long long LL;
    const int inf = (1<<30);
    const int MAXN = 200011;
    int n,deep[MAXN],father[MAXN],ql,qr,nowl,nowr,val[MAXN];
    set<int>bst;
    map<int,int>mp;
    
    inline int getint(){
        int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
        if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
    }
    
    inline void work(){
    	n=getint(); int x; bst.insert(inf); bst.insert(-inf);
    	val[1]=x=getint(); bst.insert(x); mp[x]=1;
    	for(int i=2;i<=n;i++) {
    		x=getint(); val[i]=x;
    		ql=*--bst.lower_bound(x);
    		qr=*bst.lower_bound(x);
    		if(ql==(-inf)) father[i]=mp[qr];
    		else if(qr==inf) father[i]=mp[ql];
    		else{
    			if(nowl<nowr) father[i]=mp[ql];
    			else if(nowl>nowr) father[i]=mp[qr];
    			else{
    				int ll=mp[ql],rr=mp[qr];
    				if(ll>rr) father[i]=ll;
    				else father[i]=rr; 
    			}
    		}
    		deep[i]=deep[father[i]]+1;
    		bst.insert(x); mp[x]=i;
    		//printf("%d %d
    ",father[i],deep[i]);
    		printf("%d ",val[father[i]]);
    	}
    }
    
    int main()
    {
        work();
        return 0;
    }
    

      

  • 相关阅读:
    LeetCode:12. Roman to Integer (Easy)
    Python:正则表达式—— re 模块
    RAID(冗余硬盘阵列)
    Mac OS下搭建Hadoop + Spark集群
    LeetCode:12. Integer to Roman(Medium)
    js事件中绑定另一事件导致事件多次执行
    ie8以上及非ie8情况下的写法
    javascript闭包
    bootstrap-datetimepicker年视图中endDate设置之后比正常时间提前两个月
    javascript的阻塞机制
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/6375132.html
Copyright © 2011-2022 走看看