admin管理员组文章数量:1602103
题意:输出2到n在一颗不平衡二叉排序树里面的父节点的编号
思路:显然边建树边输出是会TLE的,那么开个set乱搞一下就可以了
#include<iostream>
#include<cstdio>
#include<map>
#include<set>
using namespace std;
const int maxn = 1e5+6;
map<int,int>pos;
set<int> s;
int main()
{
int n;
scanf("%d",&n);
int temp;
scanf("%d",&temp);
s.insert(0);
s.insert(temp);
// s.insert(1e9+100);
pos[temp]=1;
for (int i = 2;i<=n;i++)
{
cin >> temp;
set<int>::iterator it = s.lower_bound(temp);
set<int>::iterator it1 = it;
set<int>::iterator it2 = it;
it1--;
if (pos[*it1]>pos[*it2])
printf("%d\n",*it1);
else
printf("%d\n",*it2);
s.insert(temp);
pos[temp]=i;
}
printf("\n");
}
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.
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.
OutputOutput 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 input3 1 2 3output
1 2input
5 4 2 3 1 6output
4 2 2 4Note
Picture below represents the tree obtained in the first sample.
Picture below represents the tree obtained in the second sample.
本文标签: codeforcestreesetconstruction
版权声明:本文标题:codeforces 675D Tree Construction(set) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1728397158a1157140.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论