Codeforces 675D Tree Construction【思维+set】
2017-03-14 19:01
253 查看
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.
First element a1 becomes the root of the tree.
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:
The pointer to the current node is set to the root.
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.
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
Output
Input
Output
Note
Picture below represents the tree obtained in the first sample.
![](http://codeforces.com/predownloaded/f2/f3/f2f3bf8a19408d1f82699a41b787e56a6452dd81.png)
Picture below represents the tree obtained in the second sample.
![](http://codeforces.com/predownloaded/f5/bd/f5bdbb65dc9bd752e030a47d4808c15b1b19aae1.png)
题目大意:
让你构造出来一颗二叉搜索树,问你每个点的父亲节点的数值是多少。
思路:
新加进来的点,其父亲要么是比他大的值中最小的,或者是比他小的值中的最大的。
那么这里维护一个set乱搞就行了。
set.lower_bound(val)查询的是比val值大于等于的第一个值。
Ac代码(参考自:http://blog.csdn.net/Yukizzz/article/details/51438061)(不会用set党表示慢慢接触慢慢了解):
#include<stdio.h>
#include<set>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
map<int ,int>lson,rson;
set<int >s;
set<int >::iterator pos;
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
if(i==0)
{
s.insert(x);
continue;
}
pos=s.lower_bound(x);
if(pos!=s.end()&&lson[*pos]==0)
{
lson[*pos]=x;
}
else
{
pos--;
rson[*pos]=x;
}
printf("%d ",*pos);
s.insert(x);
}
printf("\n");
}
}
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.
First element a1 becomes the root of the tree.
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:
The pointer to the current node is set to the root.
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.
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.
![](http://codeforces.com/predownloaded/f2/f3/f2f3bf8a19408d1f82699a41b787e56a6452dd81.png)
Picture below represents the tree obtained in the second sample.
![](http://codeforces.com/predownloaded/f5/bd/f5bdbb65dc9bd752e030a47d4808c15b1b19aae1.png)
题目大意:
让你构造出来一颗二叉搜索树,问你每个点的父亲节点的数值是多少。
思路:
新加进来的点,其父亲要么是比他大的值中最小的,或者是比他小的值中的最大的。
那么这里维护一个set乱搞就行了。
set.lower_bound(val)查询的是比val值大于等于的第一个值。
Ac代码(参考自:http://blog.csdn.net/Yukizzz/article/details/51438061)(不会用set党表示慢慢接触慢慢了解):
#include<stdio.h>
#include<set>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
map<int ,int>lson,rson;
set<int >s;
set<int >::iterator pos;
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
if(i==0)
{
s.insert(x);
continue;
}
pos=s.lower_bound(x);
if(pos!=s.end()&&lson[*pos]==0)
{
lson[*pos]=x;
}
else
{
pos--;
rson[*pos]=x;
}
printf("%d ",*pos);
s.insert(x);
}
printf("\n");
}
}
相关文章推荐
- codeforces 675D Tree Construction 数据结构
- codeforces 675D Tree Construction set
- Codeforces 675D Tree Construction【构造,BST】
- CodeForces - 675D Tree Construction (set&数据结构)
- 【CF 675D Tree Construction】BST
- CodeForces 732D Exams (思维+set乱搞)
- CodeForces - 330B Road Construction 思维
- codeforces 675 C ——Money Transfers——————【思维题】
- Codeforces 507C - Guess Your Way Out! (思维)
- Codeforces 611F 思维
- codeforces 154A (对 思维)
- codeforces 714C. Sonya and Queries (思维题)
- CodeForces 305A Strange Addition(思维题)
- Codeforces 513B2 - Permutations (思维)
- Codeforces 303A Lucky Permutation Triple【思维】
- Codeforces 899D - Shovel Sale 【思维】
- codeforces-233【B思维】
- Codeforces 233B Non-square Equation (数学+思维)
- codeforces 903D. Almost Difference(数学+思维)
- Codeforces 443C Borya and Hanabi【暴力+思维】