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

【题意】

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

【题解】

按次序处理每一个数字
对于数字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. C++ 值初始化和默认初始化

    对于初始化的问题,我之前一直傻傻分不清.有关初始化以及赋值的区别也是一问题,这次回过头来看,配合<<CSAPP>>的内容,对初始化有了一些新的认识. 声明: 在环境/上下文中指 ...

  2. bzoj 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典【dp】

    预处理出g[i][j]表示原串第i个匹配第j个单词需要去掉几个字母(匹配不上为-1) 设f[i]为i及之后满足条件要去掉的最少字母 倒着dp! f[i]初始为f[i+1]+1,转移方程为f[i]=mi ...

  3. 10.17NOIP模拟赛

    #include<iostream> #include<cstdio> #include<cstring> #define N 1001 using namespa ...

  4. 17年day2

    /* 嗯,明天就出发了. 嗯,终于快要结束了. 考试日常挂T1 今天晚上老师们请我们吃水饺,还有一个大蛋糕. 虽然没怎么吃蛋糕23333 还好我的水饺是白菜馅的~~~ 晚上学哥学姐们发视频送祝福,谢谢 ...

  5. webapp填坑记录

    网上也有许多的 webapp 填坑记录了,这几个月,我在公司正好也做了2个,碰到了一些问题,所以我在这里记录一下我所碰到的问题: meta 头部声明在开发的时候,刚刚创建 HTML 文件,再使用浏览器 ...

  6. 洛谷P5055 【模板】可持久化文艺平衡树(FHQ Treap)

    题面 传送门 题解 日常敲板子.jpg //minamoto #include<bits/stdc++.h> #define R register #define inline __inl ...

  7. set && muliset

    #include <set> #include <iostream> #include <cstdio> #include <cctype> using ...

  8. 用 jQuery 实现简单倒计时功能

    问题场景:假设某个活动截止时间给定了,现在需要开发一个页面可以自动刷新距离活动截止时间还剩多少天? <!DOCTYPE html> <html xmlns="http:// ...

  9. 数据库恢复挂起解决办法【MSSQL】

    新建查询输入如下代码运行 - -把test改成你需要修复的数据库名 USE master GO ALTER DATABASE test SET SINGLE_USER GO ALTER DATABAS ...

  10. Java 8 (11) 新的日期和时间API

    在Java 1.0中,对日期和时间的支持只能依赖java.util.Date类.这个类只能以毫秒的精度表示时间.这个类还有很多糟糕的问题,比如年份的起始选择是1900年,月份的起始从0开始.这意味着你 ...