(第三场) C Shuffle Cards 【STL_rope || splay】

题目链接:https://www.nowcoder.com/acm/contest/141/C

题目描述

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 ≤ 1051 ≤ pi ≤ N1 ≤ si ≤ N-pi+1

输出描述:

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

case_1

Input:

5 1

2 3

Output:

2 3 4 1 5

case_2

Input:

5 2
2 3
2 3

Output:

3 4 1 2 5

题目大意:

给一串长度为N的连续上升序列, K次操作,每次把开头为 st, 长度为 len 的子串与前面的调换。

求经过K次调换,最后的这串东西是什么。

官方题解:二叉平衡树

Main Idea: Data structure, Implementation
We need to keep cutting out some consecutive number and put them in front of the rest list.
Since we need to quick find (p_i)-th element, it would be too slow with linked-list.

The solution is just maintaining the list with treap(or any other balanced binary tree).
Overall Time complexity: O((N+M) \lg N) Overall Space complexity: O(N)

大概思路:

①STL神器 —— rope 时间上勉强解,实现简单粗暴。

②splay (未完待续)

①AC code(4824K 859MS):

 1 #include<bits/stdc++.h>
 2 #include<ext/rope>
 3 using namespace std;
 4 using namespace __gnu_cxx;
 5 rope<int>A;
 6 int main (void){
 7     int n,m;
 8     scanf("%d %d",&n,&m);
 9     for(int i=1;i<=n;i++){
10         A.push_back(i);
11     }
12     while(m--){
13         int l,r;
14         scanf("%d %d",&l,&r);
15         l--;
16         A=A.substr(l,r)+A.substr(0,l)+A.substr(l+r,n-l-r);
17     }
18     for(int i=0;i<n;i++)
19         printf("%d ",A[i]);
20 }

原文地址:https://www.cnblogs.com/ymzjj/p/9393752.html

时间: 2024-10-29 19:11:31

(第三场) C Shuffle Cards 【STL_rope || splay】的相关文章

最近打的三场比赛的总结

10.25 上午 省常中模拟赛 比赛题目刚发下来,看到出题人之后我就变得紧张起来,因为暑假的时候也做过一份他出的题,题目难到连全场最高分也不过 100 多分,所以又一次做到他出的题难免有些心理阴影. 这种心态直接导致了我在第一题上的失误.由于在心里认为这场模拟赛的难度应该较高,导致我对于第一题几乎不假思索就认为是动规,而根本没有往更简单的方向去想.我一开始想到的是区间动规,但是发现只能拿 50 分,想了一会儿还是没什么思路,于是先把区间动规打好然后打第二题. 第二题是一道比较不错的题,但是由于强

计蒜之道2015程序设计大赛初赛第三场——腾讯手机地图

计蒜之道2015程序设计大赛初赛第三场——腾讯手机地图 (一)题面 腾讯手机地图的定位功能用到了用户手机的多种信号,这其中有的信号的作用范围近,有的信号作用的范围则远一些.有的信号相对于用户在不同的方位强度是不同的,有的则是在任何一个方向上信号强度都一致的. 已知用户面向北方拿着自己的手机,在不同方位的各种信号覆盖区域可以被抽象成以用户为圆心的一系列扇形.已知每个扇形的半径 r,和每个扇形的两条边相对于正东方向的夹角度数.每个信号覆盖区域抽象出的扇形都可以通过从第一条边逆时针旋转到第二条边画出.

2014多校第三场1005 || HDU 4891 The Great Pan(模拟)

题目链接 题意 : 给你n行字符串,问你有多少种理解方式.有两大类的理解 (1){A|B|C|D|...}代表着理解方式可以是A,可以是B或C或者D. (2)$blah blah$,在$$这两个符号中间,如果是不连续的空格的那个位置就有2种理解方式,可以理解为没有空格也可以理解为有空格.如果有连续N个空格的位置,那里就有N+1种理解方式. 最后所有的理解方式相乘,数据保证$一定与$匹配,{一定与匹配},不会有任何嵌套,类似{$$}或者{{}}或者${}$这种情况都不会出现,也不会有{$}这种情况

[CareerCup] 18.2 Shuffle Cards 洗牌

18.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle—in other words, each of the 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect. 这道题让我们实现一个洗牌的算法,实际上洗

hdu-4893-Wow! Such Sequence!-线段树【2014多校第三场-J】

题意:一个初始为0的数组,支持三种操作:1.向第k个数添加d,(|d| < 2^31);2.把[l, r]区间内的数字都换成与它最相近的Fibonacci数;3.询问[l, r]区间的和. 思路:初始化Fibonacci数组,longlong 类型内90个就够用了. 线段树区间查询,用lazy标记, sgt[]记录线段树各个节点的区间和, fib_num_sum[]记录与各个叶子节点当前值最接近的Fibonacci数,传递到区间fib_num_sum[]就是区间Fibonacci数的和. 操作1

Wow! Such Sequence! HDU多校联合赛第三场1007

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger from St. Beeze College. No, not cactuses. It's

【多校赛第三场】Redraw Beautiful Drawings【网络流】【谜のWA】

参考题解:http://blog.csdn.net/qian99/article/details/38276887 #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <queue> #include <vector> #include <algorithm>

HDU - 4544 湫湫系列故事――消灭兔子 2013腾讯编程马拉松复赛第三场

Description 湫湫减肥 越减越肥! 最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏. 游戏规则很简单,用箭杀死免子即可. 箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di(1 <= i <= M),每种箭需要一定的QQ币购买. 假设每种箭只能使用一次,每只免子也只能被射一次,请计算要消灭地图上的所有兔子最少需要的QQ币. Input 输入数据有多组,每组数据有四行: 第一行有两个整数N,M(1 <= N, M &l

CSDN学院 免费技术答疑公开课,本周三场即将开播~~~

为了答谢广大学员,CSDN学院特推出免费技术答疑公开课,让您开启一段充实的学习之旅~ 本周三场即将开播! 公开课一:测试神器QuickTest参数的应用[7月30日 晚7:30-9:30(周四)] ----------------------------------------------------------------------------------------------------------------------------------- 李晓鹏(项目经理) 个人简介:河北师大