C Shuffle Cards

 牛客网暑期ACM多校训练营(第三场)  C   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 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.
示例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

思路:

  rope可当做可持久化平衡树,适用于大量、冗长的串操作

 基本操作:

       1)运算符:rope支持operator += -= + - < ==

  2)输入输出:可以用<<运算符由输入输出流读入或输出。

  3)长度/大小:调用length(),size()都可以哦

  4)插入/添加等:

  push_back(x);//在末尾添加x

  insert(pos,x);//在pos插入x,自然支持整个char数组的一次插入

  erase(pos,x);//从pos开始删除x个

  copy(pos,len,x);//从pos开始到pos+len为止用x代替

  replace(pos,x);//从pos开始换成x

  substr(pos,x);//提取pos开始x个

  at(x)/[x];//访问第x个元素

代码:

#include<cstdio>
#include<ext/rope>  //固定写法
using namespace std;
using namespace __gnu_cxx;  //固定写法
rope<int> ss;  //实质是可持久化平衡树int n,m;
int main()
{scanf("%d%d",&n,&m);for(int i=1; i<=n; i++) ss.push_back(i);  //放入元素(1~n)while(m--){int p,s;scanf("%d%d",&p,&s);ss = ss.substr(p-1,s)+ss.substr(0,p-1)+ss.substr(p+s-1,n-p-s+1); //重新组合三个区间 substr(起始位置,区间长度)
    }for(int i=0; i<n; i++){printf("%d",ss[i]);if(i==n-1)puts("");else printf(" ");}return 0;
}

 

转载于:https://www.cnblogs.com/longl/p/9381126.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/570602.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用windows api 计算文件的md5值

我自己封装了一个函数GetMd5(LPCWSTR FileDirectory)。其中调用了windows api&#xff1a;CryptAcquireContext&#xff0c;CryptCreateHash,CryptHashData等。要计算不同文件的md5值&#xff0c;只需在调用此函数时传入不同文件的全路径即可&#xff0c;不需要额外的dll。示例…

C#调用带结构体指针的C Dll的方法

C#调用带结构体指针的C Dll的方法 原文:C#调用带结构体指针的C Dll的方法在C#中调用C&#xff08;C&#xff09;类的DLL的时候&#xff0c;有时候C的接口函数包含很多参数&#xff0c;而且有的时候这些参数有可能是个结构体&#xff0c;而且有可能是结构体指针&#xff0c;那么…

【MFC】遍历文件夹及其子文件夹

void ScanFile(CString Dir) {CFileFind finder;CString AddL"\\*";CString DirSpecDirAdd; //补全要遍历的文件夹的目录BOOL bWorking finder.FindFile(DirSpec);while (bWorking){bWorking finder.FindNextFile();if(!finder.IsDots(…

HihoCoder - 1483 区间最值

给定n个数A1...An&#xff0c;小Ho想了解AL..AR中有多少对元素值相同。小Ho把这个数目定义为区间[L,R]的价值&#xff0c;用v[L,R]表示。 例如1 1 1 2 2这五个数所组成的区间的价值为4。 现在小Ho想知道在所有的的v[L,R](1 < L < R < n)中&#xff0c;第k小的值是多少…

用EnumProcesses()枚举进程

参照msdn的例子&#xff0c;用EnumProcesses&#xff08;&#xff09;枚举进程并输入进程名和句柄。以下代码在vs2008中测试通过&#xff1a; #include "stdafx.h" #include <windows.h> #include "psapi.h" #pragma comment (lib, "psapi.…

利用Tomcat运行一个JSP页面 详细步骤

1.启动Tomcat: 在Tomcat安装目录下的bin文件夹中&#xff0c;运行startup.bat批处理文件。截图如下&#xff1a; 此时会自动弹出窗口&#xff1a; 不要关闭这个窗口。 2.测试Tomcat是否启动成功。 打开浏览器&#xff0c;在地址栏中输入http://localhost:8080&#xff08;Tom…

springboot 找不到mapper问题

<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 --><resources> <!--新版本IDEA 开发工具maven编译不支持xml文件的编译&#xff0c;eclipse不影响--> <resource> <directory>src/main/java</directory> <in…

JSP的三个编译指令-page,include详解

JSP的编译指令是通知JSP引擎的消息。 常见的编译指令有三个&#xff1a; page&#xff1a;该指令是针对当前页面的指令。 include&#xff1a;用于指定如何包含另一个页面。 tablib&#xff1a;用于定义和访问自定义标签。 编译指令格式如下&#xff1a; <% 编译指令…

计算机基础:计算机网络-socket编程

来源&#xff1a;mooc大学华南理工大学计算机网络课程 chapter6 代码&#xff1a;https://github.com/NeilKeats/SocketDemo/commit/5f3a795250a95339106ed741b4355a9c6c3d39e9 1.设计原理&#xff1a;CS系统 2.如何使用&#xff1a; 3.TCP通信的socket例子&#xff1a;文件传输…

centos 静态拨号

本人系统centos6.5&#xff1b;虚拟机太丑&#xff0c;固ssh。 centos的与联网相关的配置文件在 $ /etc/sysconfig/network-scripts DHCP方式-联网 打开文件 $ vim /etc/sysconfig/network-scripts/ifcfg-eth0 常见参数说明 DEVICEeth0 # 网卡名称&#xff0c;一般不会改动 HWA…

JSP的9个内置对象-application

JSP页面中包含9个内置对象&#xff0c;这9个内置对象都是Servlet API的类或者接口的实例&#xff0c;只是JSP规范将它们完成了默认初始化&#xff0c;即它们已经是对象&#xff0c;可以直接使用。 application&#xff1a;javax.servlet.ServletContext的实例&#xff0c;该实…

jsp中获取list长度

<% taglib uri"http://java.sun.com/jsp/jstl/core" prefix"c"%> <% taglib prefix"fn" uri"http://java.sun.com/jsp/jstl/functions"%> list的长度是&#xff1a;${fn:length(list)}转载于:https://www.cnblogs.com/ca…

JSP的7个动作指令-forward指令

forward指令用于将页面响应控制转发给另外的页面。既可以转发给静态的HTML页面&#xff0c;也可以转发到动态的JSP页面&#xff0c;或者转发到容器的Servlet。 JSP的forward指令格式如下&#xff1a; 对于JSP1.0&#xff0c;使用如下语法&#xff1a; <jsp:forward page&q…

JSP的9个内置对象-request

request&#xff1a;javax.servlet.http:HttpServletRequest的实例&#xff0c;该对象封装了一次请求&#xff0c;客户端的请求参数都被封装在该对象里。获取客户端请求参数必须使用该对象。常用的方法有getParameter(String paramName), getParameterValues(String paramName)…

算法图解学习笔记02:递归和栈

计算机内存原理 要说递归和栈的问题&#xff0c;首先就要说下计算机内存的基本原理。简单理解计算机内存原理可以将一台电脑看作超市的存包柜&#xff0c;每个柜子都有柜号&#xff08;即计算机中的地址&#xff0c;如0x000000f&#xff09;。当需要将数据存储到计算机中时&…

JSP的9个内置对象-response

response代表服务器对客户端的响应。大部分时候&#xff0c;程序无须使用response来响应客户端请求&#xff0c;因为有个更简单的响应对象-out。它是页面输出流&#xff0c;是JstWriter的实例。JspWriter是Writer的子类&#xff0c;Writer是字符流&#xff0c;无法输出非字符内…

一个权重的物体拷贝权重给多个(oneWeightToMany)

你是否在做项目的时候经常会遇见一个物体带权重需要拷贝给其他物体&#xff0c;这时候其他物体多的数不胜数 你怎么办呢? 举例&#xff1a;假如一头狮子 身体你已经做好了&#xff0c;但是模型师把半个身体都做满了垂落的实体模型毛发&#xff0c;你是否感到头疼&#xff1f;&…

JSP的9个内置对象-session

session代表一次用户会话&#xff0c;其含义是&#xff1a;从客户端浏览器连接服务器开始&#xff0c;到客户端浏览器与服务器断开为止&#xff0c;这个过程就是一次会话。 session通常用于跟踪用户的会话信息&#xff0c;如判断用户是否登录系统&#xff0c;或者在购物车应用中…

JavaScript-Tool:jquery.qrcode.js

ylbtech-JavaScript-Tool&#xff1a;jquery.qrcode.js1.返回顶部 1、插件描述&#xff1a;jquery.qrcode.js 是一个能够在客户端生成矩阵二维码QRCode 的jquery插件 &#xff0c;使用它可以很方便的在页面上生成二维条码。如何使用它 将jquery.qrcode.min.js和jquery添加到您的…

lunix下的redis数据库操作——list列表

首先&#xff0c;需要先了解栈和队列的概念&#xff1a; 栈 先进后出&#xff1a;类比弹夹上的子弹&#xff0c;最后上进弹夹的子弹第一个使用&#xff0c;砌墙的板砖&#xff0c;后来居上 队列 先进先出&#xff1a;排队打饭&#xff0c;先到先得 创建列表&#xff1a; 左添…