CSU 1555 splay模拟

本文介绍了一种使用伸展树(Splay Tree)解决逆序数问题的方法,即已知每个位置上的逆序数,求解原始序列。通过从大到小模拟的方式构建伸展树,最终通过中序遍历获取原始序列。

题意:给你每个位置对应的逆序数, 求其原序列。
思路: splay从大到小模拟;

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
#define Key_value ch[ch[root][1]][0]
const int MAXN = 500010;
const int INF = 0x3f3f3f3f;
int pre[MAXN],ch[MAXN][2],key[MAXN],size[MAXN];
int root,tot1;
int sum[MAXN],rev[MAXN],same[MAXN];
int lx[MAXN],rx[MAXN],mx[MAXN];
int s[MAXN],tot2;//内存池和容量
int n,q;
void NewNode(int &r,int father,int k)
{
    if(tot2) r = s[tot2--];//取的时候是tot2--,存的时候就是++tot2
    else r = ++tot1;
    pre[r] = father;
    ch[r][0] = ch[r][1] = 0;
    key[r] = k;
    size[r] = 1;
}
void push_up(int r)
{
    int lson = ch[r][0], rson = ch[r][1];
    size[r] = size[lson] + size[rson] + 1;
}
void push_down(int r)
{
}
void Init()
{
    root = tot1 = tot2 = 0;
    ch[root][0] = ch[root][1] = size[root] = pre[root] = 0;
    NewNode(root,0,-1);
    NewNode(ch[root][1],root,-1);
    push_up(ch[root][1]);
    push_up(root);
}
//旋转,0为左旋,1为右旋
void Rotate(int x,int kind)
{
    int y = pre[x];
    push_down(y);
    push_down(x);
    ch[y][!kind] = ch[x][kind];
    pre[ch[x][kind]] = y;
    if(pre[y])
    ch[pre[y]][ch[pre[y]][1]==y] = x;
    pre[x] = pre[y];
    ch[x][kind] = y;
    pre[y] = x;
    push_up(y);
}
//Splay调整,将r结点调整到goal下面
void Splay(int r,int goal)
{
    push_down(r);
    while(pre[r] != goal)
    {
        if(pre[pre[r]] == goal)
        {
            push_down(pre[r]);
            push_down(r);
            Rotate(r,ch[pre[r]][0] == r);
        }
        else
        {
            push_down(pre[pre[r]]);
            push_down(pre[r]);
            push_down(r);
            int y = pre[r];
            int kind = ch[pre[y]][0]==y;
            if(ch[y][kind] == r)
            {
                Rotate(r,!kind);
                Rotate(r,kind);
            }
            else
            {
                Rotate(y,kind);
                Rotate(r,kind);
            }
        }
    }
    push_up(r);
    if(goal == 0) root = r;
}
int Get_kth(int r,int k)
{
    push_down(r);
    int t = size[ch[r][0]] + 1;
    if(t == k)return r;
    if(t > k)return Get_kth(ch[r][0],k);
    else return Get_kth(ch[r][1],k-t);
}
//在第pos个数后面插入tot个数
void Insert(int pos, int val)
{
    Splay(Get_kth(root,pos+1),0);
    Splay(Get_kth(root,pos+2),root);
    NewNode(Key_value, ch[root][1], val);
    push_up(ch[root][1]);
    push_up(root);
}
vector<int> ans;
void InOrder(int r)
{
    if(!r)return;
    push_down(r);
    InOrder(ch[r][0]);
    if(key[r] != -1)
        ans.push_back(key[r]);
    InOrder(ch[r][1]);
}
int a[10010];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
    while(scanf("%d",&n)!=EOF)
    {
        ans.clear();
        Init();
        for(int i=1; i<=n; i++)
            scanf("%d", a+i);
        bool res = true;
        for(int i=n; i>=1; i--)
        {
            if(a[i] > n-i) {res = false;break;};
            Insert(a[i], i);
        }
        if(!res) puts("No solution");
        else{
            InOrder(root);
            int size = ans.size();
            for(int i=0; i<size; i++)
                printf("%d%s", ans[i], i==size-1?"\n":" ");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值