codeforces 675D 模拟构建二叉排序树

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.

  1. First element a1 becomes the root of the tree.
  2. 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:
    1. The pointer to the current node is set to the root.
    2. 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.
    3. 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.

Picture below represents the tree obtained in the second sample.


题意:把每个数放进二叉排序树中,输出从第二个数到第n个数的直接父节点。

分析:刚开始写了一个正规的排序二叉树,然后不断插入数据,插入数据的过程中同时记录这个数的父节点,但是没考虑到数据量太大,而且排序二叉树可能会退化成链表的模样,使得插入的时间复杂度变成O(n^2)级别,很明显会超时。后来看题解,用一个set和两个map来模拟二叉树,查找的时候用二分查找, 这样使得复杂度就在O(nlogn)级别了。


先贴一下超时的代码:

#include<bits/stdc++.h>

#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

inline int in()
{
    int res=0;char c;int f=1;
    while((c=getchar())<'0' || c>'9')if(c=='-')f=-1;
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res*f;
}
const int N=100010,MOD=1e9+7;
int a[N];

struct node
{
    int val;
    node *l,*r;
};
int ret;
node *insert(node *p,int x)
{
    if(p==NULL)
    {
        node *q = new node;
        q->val=x;
        q->l=q->r=NULL;
        return q;
    }
    else
    {
        if(x <= p->val)
        {
            ret = p->val;
            p->l=insert(p->l,x);
        }
        else {
                ret = p->val;
                p->r=insert(p->r,x);
        }
        return p;
    }
}

int find(node *p,int x,int fa)
{
    if(x==p->val) return fa;
    else if(x <= p->val)
    {
        return find(p->l,x,p->val);
    }
    else
    {
        return find(p->r,x,p->val);
    }
}

int main()
{
    int n=in();
    node * root = new node;
    root->l = root->r = NULL;
    root->val = in();
    for(int i=1;i<n;i++)
    {
        a[i]=in();
        ret = a[0];
        root=insert(root,a[i]);
        printf("%d ",ret);
    }
//    for(int i=1;i<n;i++){
//        printf("%d ",find(root,a[i],a[0]));
//    }
    return 0;
}

AC代码:

#include<bits/stdc++.h>

#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

inline int in()
{
    int res=0;char c;int f=1;
    while((c=getchar())<'0' || c>'9')if(c=='-')f=-1;
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res*f;
}
const int N=100010,MOD=1e9+7;
map<int,int> Left,Right;
set<int> numbers;
set<int>::iterator it;

int main()
{
    int n=in(),x=in(),ret;
    numbers.insert(x);

    for(int i=1;i<n;i++){
        x=in(); //数都是不同的
        it = numbers.upper_bound(x);
        //如果找到了第一个比x大的,并且这个点的左子树不存在
        if(it != numbers.end() && Left.count(*it) == 0){
            Left[*it] = x; //插入左边
            ret = *it;
        }
        //没找到或者找到了, 但是左子树存在,那么找到小于x的最大的点
        //(这个点应该在存在的左子树中),x应该是他的右孩子(排序二叉树的特性)
        else{
            it--;
            Right[*it] = x;
            ret = *it;
        }
        numbers.insert(x);
        printf("%d ",ret);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值