Shuffle Cards
链接:https://www.nowcoder.com/acm/contest/141/C
来源:牛客网
The first line contains two space-separated integer N, M indicating the number of cards and the number of shuffling Eddy has done.i
Each of following M lines contains two space-separated integer p
, 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.5
1 ≤ N, M ≤ 10
i
1 ≤ p
≤ Ni
1 ≤ s
≤ N-pi
+1
Output one line contains N space-separated integers indicating the final order of the cards from top to bottom.
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大法解决数组分块)
原文:https://www.cnblogs.com/yzm10/p/9374568.html