NOWCODER141C:Shuffle Cards(FHQ Treap)

本文介绍了一种通过特定洗牌方式改变卡片顺序的算法,并使用C++中的Treap数据结构实现该算法。通过一系列操作,将指定区间的卡片移至顶部,最终得到新的卡片顺序。

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

题目描述 

Eddy likes to play cards game since there are always lots of randomness in the game. For most of the cards game, the very first step in the game is shuffling the cards. And, mostly the randomness in the game is from this step. However, Eddy doubts that if the shuffling is not done well, the order of the cards is predictable! 

To prove that, Eddy wants to shuffle cards and tries to predict the final order of the cards. Actually, Eddy knows only one way to shuffle cards that is taking some middle consecutive cards and put them on the top of rest. When shuffling cards, Eddy just keeps repeating this procedure. After several rounds, Eddy has lost the track of the order of cards and believes that the assumption he made is wrong. As Eddy's friend, you are watching him doing such foolish thing and easily memorizes all the moves he done. Now, you are going to tell Eddy the final order of cards as a magic to surprise him.

Eddy has showed you at first that the cards are number from 1 to N from top to bottom.

For example, there are 5 cards and Eddy has done 1 shuffling. He takes out 2-nd card from top to 4-th card from top(indexed from 1) and put them on the top of rest cards. Then, the final order of cards from top will be [2,3,4,1,5].

输入描述:

The first line contains two space-separated integer N, M indicating the number of cards and the number of shuffling Eddy has done.
Each of following M lines contains two space-separated integer pi, si indicating that Eddy takes pi-th card from top to (pi+si-1)-th card from top(indexed from 1) and put them on the top of rest cards.


1 ≤ N, M ≤ 105
1 ≤ pi ≤ N
1 ≤ si ≤ N-pi+1

输出描述:

Output one line contains N space-separated integers indicating the final order of the cards from top to bottom.

示例1

输入

复制

5 1
2 3

输出

复制

2 3 4 1 5

示例2

输入

复制

5 2
2 3
2 3

输出

复制

3 4 1 2 5

示例3

输入

复制

5 3
2 3
1 4
2 4

输出

复制

3 4 1 5 2

题意:N个数的数组初始分别是1~N,有M个操作,每个操作将一段区间移到数组开头,问最终数组是怎样的。

思路:这题最简单就是用C++封装好的容器rope,也可以写平衡树,比如无旋treap,treap就是堆和二叉搜索树的结合体,无旋treap就是没有旋转操作的treap,功能更强大。本题属于该数据结构的基本操作,将p下标的s个数移到开头,那么把树砍成三部分再连接起来就行。

# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline int Rand()
{
    static int seed=233;
    return seed=int(seed*48271LL%2147483647);
}
struct Treap
{
    Treap* son[2];
    int weight,sze,data;
    Treap(int v)
    {
        sze=1; data=v; weight=Rand();
        son[1] = son[0] = NULL;
        return;
    }
    inline void update()
    {
        sze = 1+(son[0]!=NULL?son[0]->sze:0)+(son[1]!=NULL?son[1]->sze:0);
        return;
    }
}*root;
typedef pair<Treap*,Treap*>D;
inline int sze(Treap* pos){return pos?pos->sze:0;}
Treap* merge(Treap* a,Treap* b)
{
    if(!a) return b;
    if(!b) return a;
    if(a->weight < b->weight)
    {
        a->son[1] = merge(a->son[1],b);
        a->update();
        return a;
    }else
    {
        b->son[0] = merge(a,b->son[0]);
        b->update();
        return b;
    }
}
D split(Treap* pos,int k)
{
    if(pos == NULL) return D(NULL,NULL);
    D y;
    if(sze(pos->son[0]) >= k)
    {
        y = split(pos->son[0],k);
        pos->son[0] = y.second;
        pos->update();
        y.second = pos;
    }
    else
    {
        y=split(pos->son[1],k-1-sze(pos->son[0]));
        pos->son[1] = y.first;
        pos->update();
        y.first = pos;
    }
    return y;
}
vector<int>ans;
void output(Treap* a)
{
    if(a == NULL) return;
    output(a->son[0]);
    ans.push_back(a->data);
    output(a->son[1]);
}
int main()
{
    int n, m, l, r;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; ++i)
        root = merge(root, new Treap(i));
    while(m--)
    {
        scanf("%d%d",&l,&r);
        D x = split(root, l+r-1);
        D y = split(x.first, l-1);
        merge(y.second, merge(y.first, x.second));
    }
    output(root);
    for(int i=0; i<ans.size(); ++i) printf("%d%c",ans[i],i==ans.size()-1?'\n':' ');
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值