HDU 5324 Boring Class 树状数组套平衡树

本文介绍了一种解决最长有序子序列问题的方法,通过离散化、树状数组结合平衡树的技术实现高效求解。该问题要求寻找满足特定条件的最长子序列,并在多个可能解中选择字典序最小的一个。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Boring Class

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Problem Description
Mr. Zstu and Mr. Hdu are taking a boring class , Mr. Zstu comes up with a problem to kill time, Mr. Hdu thinks it’s too easy, he solved it very quickly, what about you guys?
Here is the problem:
Give you two sequences L1,L2,...,Ln and R1,R2,...,Rn .
Your task is to find a longest subsequence v1,v2,...vm satisfies
v11 , vmn , vi<vi+1 .(for i from 1 to m - 1)
LviLvi+1 , RviRvi+1 (for i from 1 to m - 1)
If there are many longest subsequence satisfy the condition, output the sequence which has the smallest lexicographic order.


 

Input
There are several test cases, each test case begins with an integer n.
1n50000
Both of the following two lines contain n integers describe the two sequences.
1Li,Ri109
 

Output
For each test case ,output the an integer m indicates the length of the longest subsequence as described.
Output m integers in the next line.
 

Sample Input
  
5 5 4 3 2 1 6 7 8 9 10 2 1 2 3 4
 

Sample Output
  
5 1 2 3 4 5 1 1
 

Author
ZSTU
 

Source
 
题意:给出两个长度为n(n< 5w)的数组A,B,数组元素小于10^9 。求一个序列Vi。要求1<=Vi<=n,Vi<Vi+1,且长度最长的条件下字典序最小,使得Avi>=Avi+1且Bvi<=Bvi+1。
思路:要求字典序最小,只能倒着来,用DP[i]表示从最后一位到第i的能构成序列的最长长度,用Pre[i]来表示在上述条件下的前驱的位置。如果暴力DP,是会TLE,需要进行优化。将两个数组进行离散化。建立一个树状数组,数组数组的每个节点建立一个平衡树。树状数组按A数组的值建树。平衡树按照B值建树(关键值),维护DP的最大值,DP最大值条件下的下标的最小值。当更新第i个位置的dp值的时候,选取数组数组中小于等于x的区间(废话),在这些平衡树中找关键值大于等于B[i]的DP的最大值及其ID最小值。得到后直接更新dp和pre即可。更新完成后在将i位置的信息插入树套树中。
代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define maxn 50005
inline int max(int a,int b)
{
    return a>b?a:b;
}
struct multi_treap
{
    struct node
    {
        node* ch[2];
        int v, r, s,id,maxx,dp,maxxid;
        inline int cmp(int x)
        {
            if (x == v) return -1;
            return(x < v ? 0 : 1);
        }
    };
    node *root;
    inline void updata(node* o)
    {
        if (!o) return;
        o->s = 1;
        o->maxx = o->dp;
        o->maxxid = o->id;
        if (o->ch[0])
        {
            o->s += o->ch[0]->s;
            if(o->ch[0]->maxx > o->maxx)
            {
                o->maxx = o->ch[0]->maxx;
                o->maxxid = o->ch[0]->maxxid;
            }
            else if(o->ch[0]->maxx == o->maxx)
            {
                if(o->maxxid > o->ch[0]->maxxid) o->maxxid = o->ch[0]->maxxid;
            }
        }
        if (o->ch[1])
        {
            o->s += o->ch[1]->s;
            if(o->ch[1]->maxx > o->maxx)
            {
                o->maxx = o->ch[1]->maxx;
                o->maxxid = o->ch[1]->maxxid;
            }
            else if(o->ch[1]->maxx == o->maxx)
            {
                if(o->maxxid > o->ch[1]->maxxid) o->maxxid = o->ch[1]->maxxid;
            }
        }
    }
    inline void rateto(node* &o, int d)
    {
        node* k;
        k = o->ch[d ^ 1];
        o->ch[d ^ 1] = k->ch[d];
        k->ch[d] = o;
        updata(o);
        updata(k);
        o = k;
    }
    void add(node* &o, int Y,int DP,int ID)
    {
        if (!o)
        {
            o = (node*)malloc(sizeof(node));
            o->ch[0] = o->ch[1] = 0;
            o->v = Y;
            o->r = rand()*rand();
            o->s = 1;
            o->dp = DP;
            o->id = ID;
            o->maxx  = DP;
            o->maxxid = ID;
            return;
        }
        int d = o->cmp(Y);
        if (d == -1)
        {
            if(DP < o->dp) return;
            if(DP==o->dp)
            {
                if(ID < o->id) o->id=ID;
            }
            else
            {
                o->dp=DP;
                o->id=ID;
            }
            updata(o);
        }
        else
        {
            add(o->ch[d], Y,DP,ID);
            updata(o);
            if (o->r < o->ch[d]->r) rateto(o, d ^ 1);
        }
    }
    void find(node *o,int Y,int &DP,int &ID)
    {
        if(o==NULL) return;
        if(o->v < Y)
        {
            if(o->ch[1]) find(o->ch[1],Y,DP,ID);
            return;
        }
        else
        {
            if(o->dp > DP)
            {
                DP=o->dp;
                ID=o->id;
            }
            else if(o->dp == DP)
            {
                if(o->id < ID) ID = o->id;
            }
            if(o->ch[1])
            {
                if(o->ch[1]->maxx > DP)
                {
                    DP=o->ch[1]->maxx;
                    ID=o->ch[1]->maxxid;
                }
                else if(o->ch[1]->maxx == DP)
                {
                    if(o->ch[1]->maxxid < ID) ID = o->ch[1]->maxxid;
                }
            }
            if(o->ch[0]) find(o->ch[0],Y,DP,ID);
        }
    }
    void mymemory(node * &o)
    {
        if(o)
        {
            mymemory(o->ch[0]);
            mymemory(o->ch[1]);
            free(o);
            o=NULL;
        }
    }
    inline void insert(int Y,int DP,int ID)
    {
        add(root,Y,DP,ID);
    }
    inline void clear()
    {
        mymemory(root);
    }
    inline void get(int Y,int &DP,int &ID)
    {
        find(root,Y,DP,ID);
    }
    multi_treap()
    {
        root = 0;
    }
};
multi_treap s[maxn];
int sx;
void inline init()
{
    for(int i=1; i<=sx; i++) s[i].clear();
}
inline int lowbit(int X)
{
    return X&-X;
}
inline void add(int pos,int Y,int DP,int ID)
{
    while(pos<=sx)
    {
        s[pos].insert(Y,DP,ID);
        pos+=lowbit(pos);
    }
}
inline void que(int pos,int Y,int &DP,int &ID)
{
    while(pos>0)
    {
        s[pos].get(Y,DP,ID);
        pos-=lowbit(pos);
    }
}
int dp[maxn],pre[maxn],a[maxn],b[maxn],x[maxn],y[maxn];
inline int getint()
{
    int c;
    while (c = getchar(), c<'0' || '9'<c);
    int res = c - 48;
    while (c = getchar(), '0' <= c&&c <= '9') res = (res << 3) + res + res + c - 48;
    return res;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++)
        {
            x[i]=a[i]=getint();
        }
        for(int i=1; i<=n; i++)
        {
            y[i]=b[i]=getint();
        }
        std::sort(a+1,a+1+n);
        sx=std::unique(a+1,a+1+n)-a-1;
        std::sort(b+1,b+1+n);
        int sy=std::unique(b+1,b+1+n)-b-1;
        for(int i=1; i<=n; i++) x[i]=std::lower_bound(a+1,a+1+sx,x[i])-a;
        for(int i=1; i<=n; i++) y[i]=std::lower_bound(b+1,b+1+sy,y[i])-b;
        memset(dp,0,sizeof(dp));
        memset(pre,0x7F,sizeof(pre));
        init();
        int ans=0,id;
        for(int i=n; i>=1; i--)
        {
            int Y=y[i],ID=0x7F7F7F7F,DP=0;
            que(x[i],Y,DP,ID);
            pre[i]=ID;
            dp[i]=DP+1;
            add(x[i],Y,dp[i],i);
            if(dp[i]>=ans)
            {
                ans=dp[i];
                id=i;
            }
        }
        printf("%d\n",ans);
        while(id!=0x7F7F7F7F)
        {
            printf("%d",id);
            id=pre[id];
            if(id!=0x7F7F7F7F) putchar(' ');
        }
        putchar('\n');
    }
    return 0;
}
附上CDQ分治代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 50005
using namespace std;
struct tree_node
{
	int dp, id;
	inline bool operator < (const tree_node &b) const
	{
		if (dp == b.dp) return id > b.id;
		return dp < b.dp;
	}
};
int x[MAXN], y[MAXN];
int dp[MAXN], pre[MAXN];
struct sort_node
{
	int y, id;
	inline bool operator < (const sort_node &b) const
	{
		if (y == b.y) return id < b.id;
		return y > b.y;
	}
};
sort_node left[MAXN], right[MAXN];
int sx, sy;
tree_node c[MAXN];
inline int lowbit(int x)
{
	return x&-x;
}
void Modify(int pos, tree_node val)
{
	while (pos <= sx)
	{
		c[pos] = max(c[pos], val);
		pos += lowbit(pos);
	}
}
void clear(int pos)
{
	while (pos <= sx)
	{
		c[pos].dp = 0;
		c[pos].id = 0x7F7F7F7F;
		pos += lowbit(pos);
	}
}
tree_node getmax(int pos)
{
	tree_node res;
	res.id = 0x7F7F7F7F;
	res.dp = 0;
	while (pos > 0)
	{
		res = max(c[pos], res);
		pos -= lowbit(pos);
	}
	return res;
}
void update(int id, tree_node &tmp)
{
	if (dp[id] < tmp.dp)
	{
		dp[id] = tmp.dp;
		pre[id] = tmp.id;
	}
	else if (dp[id] == tmp.dp)
	{
		if (pre[id] > tmp.id) pre[id] = tmp.id;
	}
}
void CDQ(int L, int R)
{
	int MID = (L + R) >> 1;
	for (int i = L; i <= MID; i++)
	{
		left[i - L + 1].y = y[i];
		left[i - L + 1].id = i;
	}
	for (int i = MID + 1; i <= R; i++)
	{
		right[i - MID].y = y[i];
		right[i - MID].id = i;
	}
	int left_size = MID - L + 1;
	int right_size = R - L + 1 - left_size;
	sort(left + 1, left + left_size + 1);
	sort(right + 1, right + right_size + 1);
	int left_point = 1, right_point = 1;
	for (left_point; left_point <= left_size; left_point++)
	{
		while (right[right_point].y >= left[left_point].y&&right_point <= right_size)
		{
			tree_node tmp;
			tmp.dp = dp[right[right_point].id];
			tmp.id = right[right_point].id;
			Modify(x[right[right_point].id], tmp);
			right_point++;
		}
		tree_node tmp;
		tmp = getmax(x[left[left_point].id]);
		update(left[left_point].id, tmp);
	}
	for (right_point = 1; right_point <= right_size; right_point++) clear(x[right[right_point].id]);
}
void dfs(int L, int R)
{
	if (L == R)
	{
		dp[L]++;
		return ;
	}
	int mid = (L + R) >> 1;
	dfs(mid + 1, R);
	CDQ(L, R);
	dfs(L, mid);
}
int a[MAXN], b[MAXN];
int main()
{
	int n;
	for (int i = 0; i < MAXN; i++)
	{
		c[i].dp = 0;
		c[i].id = 0x7F7F7F7F;
	}
	while (~scanf("%d", &n))
	{
		memset(dp, 0, sizeof(dp));
		memset(pre, 0x7F, sizeof(pre));
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			x[i] = a[i];
		}
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &b[i]);
			y[i] = b[i];
		}
		sort(a + 1, a + 1 + n);
		sx = unique(a + 1, a + 1 + n) - a - 1;
		for (int i = 1; i <= n; i++) x[i] = lower_bound(a + 1, a + 1 + sx, x[i]) - a;
		sort(b + 1, b + 1 + n);
		sy = unique(b + 1, b + 1 + n) - b - 1;
		for (int i = 1; i <= n; i++) y[i] = lower_bound(b + 1, b + 1 + sy, y[i]) - b;
		dfs(1, n);
		int ans = 0, id = 0x7F7F7F7F;
		for (int i = 1; i <= n;i++) 
			if (dp[i] > ans)
			{
				ans = dp[i];
				id = i;
			}
		printf("%d\n", ans);
		for (; id != 0x7F7F7F7F; id = pre[id])
		{
			printf("%d", id);
			if (pre[id] != 0x7F7F7F7F) printf(" ");
			else printf("\n");
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值