(第三场) C Shuffle Cards 【STL_rope || splay】
题目链接:https://www.nowcoder.com/acm/contest/141/C
题目描述
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.
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):
#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
rope<int>A;
int main (void){
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++){
A.push_back(i);
}
while(m--){
int l,r;
scanf("%d %d",&l,&r);
l--;
A=A.substr(l,r)+A.substr(,l)+A.substr(l+r,n-l-r);
}
for(int i=;i<n;i++)
printf("%d ",A[i]);
}
(第三场) C Shuffle Cards 【STL_rope || splay】的更多相关文章
- 牛客网多校训练第三场 C - Shuffle Cards(Splay / rope)
链接: https://www.nowcoder.com/acm/contest/141/C 题意: 给出一个n个元素的序列(1,2,...,n)和m个操作(1≤n,m≤1e5),每个操作给出两个数p ...
- 2018牛客多校第三场 C.Shuffle Cards
题意: 给出一段序列,每次将从第p个数开始的s个数移到最前面.求最终的序列是什么. 题解: Splay翻转模板题.存下板子. #include <bits/stdc++.h> using ...
- 2018牛客网暑期ACM多校训练营(第三场) H - Shuffle Cards - [splay伸展树][区间移动][区间反转]
题目链接:https://www.nowcoder.com/acm/contest/141/C 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 牛客网暑期ACM多校训练营(第三场) C Shuffle Cards 平衡树 rope的运用
链接:https://www.nowcoder.com/acm/contest/141/C来源:牛客网 Eddy likes to play cards game since there are al ...
- [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 ...
- 2018 HDU多校第三场赛后补题
2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...
- 2018牛客网暑假ACM多校训练赛(第三场)I Expected Size of Random Convex Hull 计算几何,凸包,其他
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-I.html 题目传送门 - 2018牛客多校赛第三场 I ...
- 2018牛客网暑假ACM多校训练赛(第三场)G Coloring Tree 计数,bfs
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-G.html 题目传送门 - 2018牛客多校赛第三场 G ...
- 2018牛客网暑假ACM多校训练赛(第三场)D Encrypted String Matching 多项式 FFT
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-D.html 题目传送门 - 2018牛客多校赛第三场 D ...
随机推荐
- spark ALS 推荐算法参数说明
- oracle 操作实例(一)----redolog 损坏恢复
一,实验前的准备 数据库全备保证自己没成功还能补救一下 vim full.sh export ORACLE_BASE=/u01/app/oracle export ORACLE_HOME=$ORACL ...
- PHP unlink删除本地中文名称的文件
由于编码不一样,用unlink()方法删除本地中文名称的材料之前,必须先转码,才能删除成功. 核心代码如下: //删除本地的议题材料(本地上传的材料) if($local_ma ...
- lua输入函数名字符串执行函数
str = "testA()"loadstring(str)() function testA() ------end 使用loadstring即可执行后面在xlua用了下发现不能 ...
- Nginx启动关闭和重启、文档直接下载不阅览
nginx启动相关 启动:sbin/nginx -c conf/nginx.conf 关闭:sbin/nginx -s stop 重启(重新加载配置文件):sbin/nginx -s reload 检 ...
- Linux内存管理机制简析
Linux内存管理机制简析 本文对Linux内存管理机制做一个简单的分析,试图让你快速理解Linux一些内存管理的概念并有效的利用一些管理方法. NUMA Linux 2.6开始支持NUMA( Non ...
- 从零开始写C# MVC框架之--- 项目结构
框架总分2个项目:Web开发项目.帮助类项目 (ZyCommon.Zy.Utilities) 1.ZyCommon,是Web开发项目结构.新建一个空解决方案,再建Data.Service.ZyWeb解 ...
- (0!=0)==true? 记一个匪夷所思的问题
最近换了份工作,公司的开发框架是基于SSH自己搭建的.这个问题是我在解决一个需求的时候遇到的,其实解决这个疑惑的过程也就是读框架源码的过程,特此记录一下. 问题:ba.getState()!=CbBa ...
- 探寻hashmap
Hashmap源码 1. 构造器: a)获得默认数组大小:1>>4 :16 b) 获得负载因子:0.75:衡量hashmap的空间使用程度 i.过大:使用空间更加充分,但是查找效率变低, ...
- POI Excel解析
Maven 引入POI <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi&l ...