codeforces 675D (STL set)
来源:互联网 发布:jvcs500知乎 编辑:程序博客网 时间:2024/05/22 07:57
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 aitself.
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.
一开始以为要构造二叉树,但是发现数据量太大了 1e5,如果是单链的话会退化成 1e5*1e5
其实只需要找到它前面最后一个比它小和最后一个比它大的 比较哪个出现的时间晚即可
#include <bits/stdc++.h>using namespace std;int fa[101010];set<int> s;map<int,int> mp;int main(){ int n; scanf("%d",&n); int d; scanf("%d",&d); s.insert(d); mp[d]=1; for(int i=2;i<=n;i++) { scanf("%d",&d); set<int>::iterator it=s.lower_bound(d);//s.lower_bound(d) 比lower_bound(s.begin(),s.end())快 if(it==s.begin()) fa[i]=*it; else if(it==s.end()) it--,fa[i]=*it; else { set<int>::iterator it2 = it; it--; if(mp[*it]>mp[*it2]) fa[i]=*it; else fa[i]=*it2; } mp[d]=i; s.insert(d); } for(int i=2;i<=n;i++) printf("%d ",fa[i] );}
- codeforces 675D (STL set)
- Codeforces--246D--Colorful Graph(STL set)
- CodeForces - 733D Kostya the Sculptor (STL SET)
- codeforces 675D Tree Construction(set)
- Codeforces 675D Tree Construction【思维+set】
- codeforces 675D Tree Construction(set)
- codeforces 631B (STL set)
- Codeforces Round #295 (Div. 2) D.Cubes(STL SET应用)
- Codeforces--246D--Colorful Graph(STL set)(好题)
- CodeForces - 749D set+二分
- Codeforces 620D STL+二分
- codeforces 660D (STL map)
- CodeForces - 675D Tree Construction (set&数据结构)
- 【CodeForces】567D - One-Dimensional Battle Ships & 【51Nod】1521 - 一维战舰(STL - set & 二分)
- Codeforces 19D Points 线段树+set
- Codeforces 460 D. Little Victor and Set
- 【CodeForces】19D Points 线段树+set
- codeforces 520d Cubes 贪心+set
- x86架构下,页面大小为什么是4K?
- Magic number (programming) from Wikipedia(0xCCCCCCCC、0xCDCDCDCD、0xFEEEFEEE)
- 透镜分离相位检测对焦的原理
- sql server 判断是否存在数据库,表,列,视图
- B-Code For 1 Codeforces 768【递归】 好题!
- codeforces 675D (STL set)
- 子网掩码计算
- 找到两个链表的第一个公共节点 37--剑指offer
- 【Owin 学习系列】1. 第一个 Owin 程序
- 树莓派2 配置
- cordova-plugin-camera及cordova-plugin-crop调用例程
- 代码重构小结
- springboot JPA Connection is read-only. Queries leading to data modification are not allowed
- pc端 移动端 分享