【链接】 我是链接,点我呀:)

【题意】

依次序将数字插入到排序二叉树当中
问你每个数字它的父亲节点上的数字是啥

【题解】

按次序处理每一个数字
对于数字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的更多相关文章

  1. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【codeforces 765E】Tree Folding

    [题目链接]:http://codeforces.com/problemset/problem/765/E [题意] 给你一棵树; 可以把一个节点的两条相同长度的链合并成一条链; 且这两条相同长度的链 ...

  3. codeforces 675D D. Tree Construction(线段树+BTS)

    题目链接: D. Tree Construction D. Tree Construction time limit per test 2 seconds memory limit per test ...

  4. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  5. Kruskal算法及其类似原理的应用——【BZOJ 3654】tree&&【BZOJ 3624】[Apio2008]免费道路

    首先让我们来介绍Krukal算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...

  6. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【30.36%】【codeforces 740D】Alyona and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【13.91%】【codeforces 593D】Happy Tree Party

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 764C】Timofey and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. jquery中$()的使用

    在jquery中最常使用的就是$这个符号了,在我没有系统的学习jquery之前,我用到的$都是用于对元素的选择,而这只是$的很简单的用法.在jquery$()函数一共有三种用法: $(selector ...

  2. Linux 进程间通讯方式 pipe()函数 (转载)

    转自:http://blog.csdn.net/ta893115871/article/details/7478779 Linux 进程间通讯方式有以下几种: 1->管道(pipe)和有名管道( ...

  3. NestedPreb

    屌丝手动版 One of the things we’re sorely missing from Unity is nested prefabs. So we rolled this little ...

  4. P3256 [JLOI2013]赛车

    传送门 如果把速度看成斜率,起始位置看成截距,这就是一个水平可见直线了-- 不过这题里我实现方法借鉴了CQzhangyu大佬的,先按速度排序,然后维护一个单调栈,如果当前的人速度比栈顶大距离又比它远直 ...

  5. Notepad++安装插件

    https://sourceforge.net/projects/npp-plugins/files/ 下载完插件后,通过notepad++的设置>>>>导入>>& ...

  6. java 键盘输入多种方法

    转! 分类: java学习2012-11-04 09:58 8427人阅读 评论(1) 收藏 举报 一.java不像C中拥有scanf这样功能强大的函数,大多是通过定义输入输出流对象.常用的类有Buf ...

  7. md5加密、Des加密对称可逆加密、RSA非对称可逆加密、https单边验证、银行U盾双边认证

    1.md5不可逆的加密方式,加密成一个32位的字符串.算法是公开的,任何语言的加密结果都是一样的.总有可能是重复的.     用途:             (1)防止明文存储:可以用作密码加密    ...

  8. poj2240 Arbitrage

    思路: 有向图判负环. 实现: (1)spfa #include <iostream> #include <map> #include <string> #incl ...

  9. Android基础TOP7_1:ListView制作列表

    结构: Activity: activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/r ...

  10. JPEG图像压缩出现资源不足问题的解决

    1,问题的提出 公司开发了一个图像压缩上传程序.采用Delphi语言实现.大致步骤如下: 1,上传前将文件打开装载到TJpegImage, 2,创建一个TBitmap组件,设置其大小,采用Stretc ...