牛客多校3 C-Shuffle Cards(rope大法解决数组分块)

Shuffle Cards

链接:https://www.nowcoder.com/acm/contest/141/C
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld

题目描述

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 p
i
, s
i
 indicating that Eddy takes p
i
-th card from top to (p
i
+s
i
-1)-th card from top(indexed from 1) and put them on the top of rest cards.


1 ≤ N, M ≤ 10
5

1 ≤ p
i
 ≤ N
1 ≤ s
i
 ≤ N-p
i
+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



看成字符串处理。

STL中的rope准确的中文翻译是可持久化平衡树,超好用!

第一次用,因为内部实现是平衡树,所以时间效率较高(空间比较玄学)

貌似不是标准的STL容器,在名称空间__gnu_cxx中,用起来和string差不多

s.insert(a,b) 在s的第a位插入b(b可为字符串)

s.erase(a,b)在s的第a位删除b

输出时直接将s[c]表示s的第c位数

 

ps:本题与C++的string作了对比,同样的实现string43%超时,rope600ms过



#include<bits/stdc++.h>
#include<ext/rope>     //固定写法
using namespace std;
using namespace __gnu_cxx;     //固定写法
rope<int> s;     //实质是可持久化平衡树
 
int main()
{
    int n,m,l,e,i;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++){
        s.push_back(i);    //放元素
    }
    while(m--){
        scanf("%d%d",&l,&e);
        s=s.substr(l-1,e)+s.substr(0,l-1)+s.substr(l+e-1,n-e-(l-1));    //将区间放置首位,重新组合数组,substr(起始字符,元素个数)
    }
    for(i=0;i<s.size();i++){
        if(i>0) printf(" ");
        printf("%d",s[i]);   //元素按下标输出
    }
    return 0;
}

 

posted @ 2018-07-26 21:26  yzm10  阅读(337)  评论(0编辑  收藏  举报