codeforces 675D Tree Construction (map)

来源:互联网 发布:c语言读取bin文件内容 编辑:程序博客网 时间:2024/06/03 13:46

题目链接:http://codeforces.com/contest/675/problem/D


题意:建立一棵二叉搜索树,比当前节点小的放左边,否则放右边。问编号2~n的节点的父节点数值是多少。


思路:如果用普通的建树方法来做,在遇到深度较深的树时会超时。map的lower_bound(t)返回第一个key大于等于t的位置it,所以t必定在(it.second,it.first)之间。

#include<bits/stdc++.h>using namespace std;typedef long long ll;const int N=1e5+5;map <int ,int> q;int a[N];int main(){    int n;    scanf("%d %d",&n ,&a[1]);    q[a[1]] = q[1e9] = a[1];    for(int i=2;i<=n;i++)    {        scanf("%d",&a[i]);        map<int,int>::iterator it = q.lower_bound(a[i]);        printf("%d " ,(*it).second);q[a[i]] = (*it).second = a[i];    }    return 0;}


0 0