【Codeforces 675D】Tree Construction
【链接】  我是链接,点我呀:) 
 【题意】
依次序将数字插入到排序二叉树当中
问你每个数字它的父亲节点上的数字是啥
【题解】
按次序处理每一个数字
对于数字x
找到最小的大于x的数字所在的位置i
显然,x要放在这个x的左子树里面
所以如果x的左子树为空那么直接放上去就好
否则,左子树不为空的话,对于i的左儿子,从这个左儿子开始,一直往右儿子方向往下走
即未插入x之前,最大的且比i对应的数字小的数字 less
我们的x显然是要放在less的右子树上才行。
这个less和位置i对应的数字都能用java里面的lower和higher函数得到
那么根据上面的分析
如果higher对应的数字的左子树为空,那么x应该放在higher所在位置的左儿子上,因此答案就是higher
否则,如果higher对应的数字的左子树不为空,那么就应该放在less(最大的小于x的数字)的右儿子上,因此答案是less
【代码】
import java.io.*;
import java.util.*;
public class Main {
    static InputReader in;
    static PrintWriter out;
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    static int N = 50000;
    static class Task{
        int n;
        TreeSet<Integer> dic = new TreeSet<Integer>();
        HashMap<Integer,Integer> mymap[]= new HashMap[2];
        public void solve(InputReader in,PrintWriter out) {
        	for (int i = 0;i < 2;i++) mymap[i] = new HashMap<Integer,Integer>();
        	n = in.nextInt();
        	int x;
        	x = in.nextInt();
        	dic.add(x);
        	for (int i = 2;i <= n;i++) {
        		x = in.nextInt();
        		Integer upper = dic.higher(x);
        		Integer less = dic.lower(x);
        		if (upper==null) {
        			out.print(less+" ");
        			mymap[1].put(less, 1);
        		}else {
        			if (mymap[0].containsKey(upper)) {
        				out.print(less+" ");
        				mymap[1].put(less, 1);
        			}else {
        				out.print(upper+" ");
        				mymap[0].put(upper, 1);
        			}
        		}
        		dic.add(x);
        	}
        }
    }
    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}
												
											【Codeforces 675D】Tree Construction的更多相关文章
- 【19.77%】【codeforces 570D】Tree Requests
		
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
 - 【codeforces 765E】Tree Folding
		
[题目链接]:http://codeforces.com/problemset/problem/765/E [题意] 给你一棵树; 可以把一个节点的两条相同长度的链合并成一条链; 且这两条相同长度的链 ...
 - codeforces 675D D. Tree Construction(线段树+BTS)
		
题目链接: D. Tree Construction D. Tree Construction time limit per test 2 seconds memory limit per test ...
 - 【codeforces 415D】Mashmokh and ACM(普通dp)
		
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
 - Kruskal算法及其类似原理的应用——【BZOJ 3654】tree&&【BZOJ 3624】[Apio2008]免费道路
		
首先让我们来介绍Krukal算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...
 - 【27.91%】【codeforces 734E】Anton and Tree
		
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
 - 【30.36%】【codeforces 740D】Alyona and a tree
		
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
 - 【13.91%】【codeforces 593D】Happy Tree Party
		
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
 - 【codeforces 764C】Timofey and a tree
		
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
 
随机推荐
- Ruby on Rails5 直接的路径无效问题
			
比如设置个背景 background = "../../assets/images/test1.jpg" 会发现无效 网上一翻,Rails里面直接指定无效. 解决方法就是把 ...
 - [Swift通天遁地]一、超级工具-(6)通过JavaScript(脚本)代码调用设备的源生程序
			
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
 - VBNET AUTOCAD NETAPI 让插件随autocad启动
			
定义一个函数,随AutoCAD 启动加载当前程序集到autocad,涉及到写入注册表,注意这是在autocad内部加载dll之后处理的方法.... 写入HKLM表示所有登录的用户都会受影响(autoc ...
 - $CF1153A\ Serval\ and\ Bus$
			
看大佬的代码都好复杂(不愧是大佬\(orz\) 蒟蒻提供一种思路 因为求的是最近的车对吧\(qwq\) 所以我们可以用一个\(while\)循环所以没必要去用什么 \(for...\) 至于这是\(d ...
 - GIT学习之路最终日 标签管理+总结
			
本文参考廖雪峰老师的博客进行总结,完整学习请转廖雪峰博客 6.1 创建标签 命令git tag (name)用于新建一个标签,默认为HEAD,也可以指定一个commit id: git tag -a ...
 - 贪心 Codeforces Round #191 (Div. 2) A. Flipping Game
			
题目传送门 /* 贪心:暴力贪心水水 */ #include <cstdio> #include <algorithm> #include <cstring> us ...
 - {Python}安装第三方包(setup.py)
			
在github上下载了records文件到本地. 解压文件 cmd切换到文件setup.py的目录下 先执行 python setup.py build 再执行python setup.py inst ...
 - 联想 S5【K520】免解锁BL 免rec 保留数据 Magisk Xposed 救砖 ROOT ZUI 3.7.490
			
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
 - UVM基础之------uvm_port_base
			
Port Base Classes uvm_port_component_base This class defines an interface for obtaining a port ...
 - 用PHP开发自己的独立博客(一)——概述
			
开篇废话:因为重新回归朝九晚五的生活,于是就想开始写技术博客,当是做技术文档了.于是试用了各类博客,CSDN.cnblogs都还不错.简单试用了一下,说说各自的特点. CSDN的界面不能定制,使用默认 ...