1. 程式人生 > >牛客多校3 C-Shuffle Cards(rope大法解決數組分塊)

牛客多校3 C-Shuffle Cards(rope大法解決數組分塊)

always href dict sil however cli ++ gic -s

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;
}

牛客多校3 C-Shuffle Cards(rope大法解決數組分塊)