bzoj4919: [Lydsy1706月赛]大根堆

本文探讨了启发式合并策略在处理树形结构数据时的应用,特别是在求解最长递增子序列(LIS)问题上的创新算法。通过实例解析,文章详细介绍了如何在树中递归地合并子树的LIS,以及如何通过lowerbound技巧优化选择过程,确保算法效率。

怎么说呢,启发式合并真的是一个很玄学的东西啊

假如是一条链,那么答案就是LIS

对于一棵树,需要考虑当前点取不取。

假如当前点比下面所有LIS都要大,那么对于当前必选

假如只比一个LIS的最大值小,可以把这个最大值换成当前点权。

实际上,可以先把这个点权拿去替换他的lowerbound,前面的最大值没有被更新就没有影响,当前面最大值被更新,对于当前取这个点必然是成立的。

 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;

int c[210000];
struct node
{
    int x,y,next;
}a[210000];int len,last[210000];
void ins(int x,int y)
{
    len++;
    a[len].x=x;a[len].y=y;
    a[len].next=last[x];last[x]=len;
}

multiset<int>s[210000];
multiset<int> :: iterator it;
void dfs(int x)
{
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        dfs(y);
        for(it=s[y].begin();it!=s[y].end();it++)
            s[x].insert(*it);
        s[y].clear();
    }
    it=s[x].lower_bound(c[x]);
    if(it!=s[x].end())s[x].erase(it);
    s[x].insert(c[x]);
}

int main()
{
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    int n,F,rt;
    scanf("%d",&n);
    len=0;memset(last,0,sizeof(last));
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&c[i],&F);
        if(F!=0)ins(F,i);
        else rt=i;
    }
    dfs(rt);
    printf("%d\n",s[rt].size());
    return 0;
}

 

转载于:https://www.cnblogs.com/AKCqhzdy/p/9605998.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值