codeforces 675D
来源:互联网 发布:财智软件 倒闭 编辑:程序博客网 时间:2024/06/06 09:03
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.
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.
31 2 3
1 2
54 2 3 1 6
4 2 2 4
Picture below represents the tree obtained in the first sample.
Picture below represents the tree obtained in the second sample.
说是说数据结构题,但是你要自己弄一颗二叉搜索树肯定超时,我都试过了。。。
后来还是看了题解才做出来的,罪过罪过,自己做不知道得做到什么时候去,看题解还tmd中途一路出bug。。。真是太弱。。
题目意思是这样, 从第二个数开始,输出它在二叉搜索树上的父节点。
某个插入的节点一定是比它小的最大数的节点的右儿子或者是比它大的最小数节点的左儿子。
首先: 开一个map,开一个set, mp[a[i]] = i; set.insert(a[i])
然后: 在set中用it1 = upper_bount(a[i])找出比它大的最小值, 然后it2 = lower_bount(a[i]) ,此时*it2 肯定等于a[i], it2--即是比它小的最大值。
现在get了两个值,答案就是其中之一, 如果it1或者it2有一方等于set.end()那还好说直接输出另一方即可,如果插入集合之后既有比它大的值又有比它小的值怎么办? 这时候就要使用map了,比较谁的下标大,输出即可。(下标大的数更接近插入的节点)。
另:
set
lower_bound(x) 返回集合中大于或者等于x的第一个值的位置,如果找不到返回s.end() (!!!记得这有等于!)
upper_bound(x) 返回集合中大于x的第一个位置,如果找不到返回s.end()
代码:
/* * main.cpp * * Created on: 2016年5月14日 * Author: Triose */#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<map>#include<set>using namespace std;//#define ONLINE_JUDGE#define rep(i,a) for(int (i)=0; i<(a);(i)++)#define mem(a,b) (memset((a),b,sizeof(a)))#define sf(a) scanf("%d",&a)#define sfI(a) scanf("%I64d",&a)#define sfd(a,b) scanf("%d%d",&a,&b)#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)#define sfs(a) scanf("%s",a)#define pf(a) printf("%d\n",a)#define pfd(a,b) printf("%d %d\n",a,b)#define pfs(a) printf("%s\n",a)#define pfI(a) printf("%I64d\n",a)#define enter putchar(10)#define LL __int64template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }template<class T> inline T Min(T a, T b) { return a<b ? a : b; }template<class T> inline T Max(T a, T b) { return a>b ? a : b; }int n, m;map<int, int > mp;set<int> s;set<int>::iterator it1, it2;int main() {#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);//freopen("out.txt", "w", stdout);#endifint tmp;while(~sf(n)) {mp.clear();s.clear();rep(i, n) {sf(tmp);mp[tmp] = i + 1;s.insert(tmp);if(i > 0) {it1 = s.lower_bound(tmp);it1--;it2 = s.upper_bound(tmp);if(it2 == s.end()) {printf("%d%c", *(it1), i == n - 1 ? '\n' : ' ');}else if(it1 == s.end()) {printf("%d%c", *it2, i == n - 1 ? '\n' : ' ');}else {printf("%d%c", (mp[*it1] > mp[*it2] ? *it1 : *it2), i == n - 1 ? '\n' : ' ');}}}} return 0;}
- codeforces 675D
- codeforces-675D Tree Construction
- codeforces 675D (STL set)
- Codeforces 675D Tree Construction (splay)
- codeforces 675D 模拟构建二叉排序树
- Codeforces 675D Tree Construction【构造,BST】
- codeforces 675D Tree Construction(set)
- CodeForces 675B D - Restoring Painting
- codeforces 675D Tree Construction (map)
- Codeforces 675D Tree Construction【思维+set】
- codeforces 675D Tree Construction(set)
- CodeForces 617D CodeForces 617D
- CodeForces 101D
- CodeForces 103D
- CodeForces 222D Olympiad
- codeforces 242d
- CodeForces 111D
- 【codeforces】3D
- iOS开发笔记--什么时候调用layoutSubviews
- 手机开发实战33——SIM卡功能
- 5分钟实现Android中更换头像功能
- 数据挖掘常用工具库介绍
- 手机开发实战34——SIM卡应用1
- codeforces 675D
- Ubuntu命令积累
- 手机开发实战35——SIM卡应用2
- mongodb基础-索引
- 666
- PHP程序员的能力水平层次
- nod-1639-绑鞋带
- android中的动画
- 玩转树莓派-1-配置篇