传送门:E.Magic Master


John is not only a magic master but also a shuffling master. Famous though he is, he likes interacting with his fans by playing a game with his fantastic shuffling skills. The game shows as follows: He first shows a deck of pokers contains N cards indexed 1,2,…,N and all the cards are the same. He notes that the side contains numbers as the front side and the other side as the back. Later he selects one of his fans to choose a positive integer M that is not greater than 10. After that, he will shuffle the cards purposefully and put it on the desktop. Note that at this moment the front sides of the cards are facing to the desk. Then, the selected fans should perform the following steps until there is no card on the desk.

  1. Place the card on the top of the piles to John’s hand. If there are already some cards in his hand, put it on the top of them.
  2. If there remain cards on the desk:
    (a) Put a card from the top of the piles to the bottom of it.
    (b) Execute the step (a) of M times.
    © Go to step 1.

Next, it is time to witness the miracle. John will continually overturn the cards on his hand top to bottom, and we will find that the cards are always in decreasing order. One day, one of John’s fans, Tom, a smart JBer, understands the key to this magic. He turns to you and comments that the key to that magic is the shuffling John did previously. For any number M, he will turn the cards in a specific order after the shuffling. As you are not as smart as Tom, you should find the index of the Kth cards from the top of the piles after the shuffling.

Input
The first line contain a positive integer T (1 ≤ T ≤ 10) – the number of test cases. For each test cases, the first line contain a positive integer N (1 ≤ N ≤ 40000000) , indicating the number of cards. The second line contain a positive integer M (1 ≤ M ≤ 10) – the number the fans selects. The third line contain an integer Q (1 ≤ Q ≤ 100) – indicating that there are Q questions. Next, there are Q lines, each with an integer K (1 ≤ K ≤ N) – representing a query.

Output
For each query, you should output the index of the Kth cards from the top of the piles after the shuffling

样例输入:
1
5
1
2
2
3

样例输出:
5
2


官方题解:
单独用一个数组模拟指针,实现未翻开的牌跳过后面所有翻开的牌,直接指向下一 张未翻开的牌。然后按规则模拟即可。

思路:
我用的双向队列逆向模拟(因为最终的结果是已知的,我们由其逆推最初的状态),利用双向队列的首尾均可操作的性质,时间复杂度应该为 O(N*M) 。

最后顺便整理一下双向队列的一些操作。


AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<deque>
#include<queue>
#define ll long long
using namespace std;
const int N=5e7+10;
int t,n,m,q,k;
deque<int> qq;
int a[N];
int main()
{scanf("%d",&t);while(t--){scanf("%d",&n);scanf("%d",&m);qq.push_back(n);for(int i=n-1;i>=2;i--){qq.push_front(i);int len=qq.size();for(int j=0;j<m%len;j++)//防止重复移动,控制时间复杂度{int temp=qq.back();qq.pop_back();qq.push_front(temp);}}qq.push_front(1);int id=0;while(!qq.empty()){a[++id]=qq.front();qq.pop_front();}scanf("%d",&q);while(q--){scanf("%d",&k);printf("%d\n",a[k]);}}return 0;
}

Elem:数据类型

构造队列:
deque< Elem > c :构造一个空的双向队列
deque< Elem > c1(c2) :复制 deque

数据访问:
c.front() :返回第一个数据
c.back() :返回最后一个数据
c.begin() :返回指向第一个数据的迭代器
c.end() :返回指向最后一个数据的下一个位置的迭代器
c.rbegin() :返回逆向队列的第一个数据
c.rend() :返回指向逆向队列的最后一个数据的下一个位置的迭代器

加入数据:
c.push_back(Elem) :在尾部加入一个数据
c.push_front(Elem) :在头部插入一个数据
c.insert(pos,Elem) :在 pos 位置插入一个 elem ,返回新数据位置
c.insert(pos,n,Elem) :在 pos 位置插入 n 个 elem 数据,无返回值
c.insert(pos,beg,end) :在 pos 位置插入在[beg,end)区间的数据,无返回值

删除数据:
c.pop_back() :删除最后一个数据
c.pop_front() :删除头部数据
c.erase(pos) :删除 pos 位置的数据,返回下一个数据的位置
c.erase(beg,end) :删除[beg,end) 区间的数据,返回下一个数据的位置

其他操作:
c.empty() :判断容器是否为空
c.size() :返回容器中实际数据的个数
c.max_size() :返回容器中最大数据的数量
c.resize(num) :重新指定队列的长度

2019 ICPC南昌网络赛 E题 Magic Master 【双向队列】相关推荐

  1. 2019 ICPC 南昌网络赛 H. The Nth Item

    2019 ICPC 南昌网络赛 H. The Nth Item 题目大意:已知一个数列F(n): F(0)=0,F(1)=1 F(n)=3∗F(n−1)+2∗F(n−2),(n≥2) ​ 给你一个操作 ...

  2. 2019 ICPC 徐州网络赛 J.Random Access Iterator

    2019 ICPC 徐州网络赛 J.Random Access Iterator 题目大意:给你n个点和n-1条边(树形结构),保证1为根节点,通过以下方式dfs遍历: 询问dfs到最深节点的概率(有 ...

  3. 2013 ACM/ICPC 长沙网络赛J题

    题意:一个数列,给出这个数列中的某些位置的数,给出所有相邻的三个数字的和,数列头和尾处给出相邻两个数字的和.有若干次询问,每次问某一位置的数字的最大值. 分析:设数列为a1-an.首先通过相邻三个数字 ...

  4. 2019 ICPC 南京网络赛 F Greedy Sequence

    You're given a permutation aa of length nn (1 \le n \le 10^51≤n≤105). For each i \in [1,n]i∈[1,n], c ...

  5. Peekaboo(2019年上海网络赛K题+圆上整点)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 你的位置在\(O(0,0)\),\(A\)的位置为\((x_1,y_1)\),\(B\)的位置为\((x_2,y_2)\),现在已知\(a=O ...

  6. HDU - 5875 2016 ACM/ICPC 大连网络赛 H题 暴力

    题目链接 题意:给你一个区间l,r一直将val[l]模上val[l+1],val[l+2]...val[r],因为一个模上比前一个数小数是没有意义的,所以需要将每一个点找到右边第一个小于他的点就行. ...

  7. HDU - 5876 Sparse Graph 2016 ACM/ICPC 大连网络赛 I题 bfs+set+补图最短路

    题目链接 题意:给的补图,让你求一个源点到其他点的最短距离,因为图太稠密了, 用dij以及spfa根本不得行,这里只能用一种我不会方法来进行,这里用了bfs的方法以及set来维护,分别set维护一个未 ...

  8. HDU - 5877 Weak Pair 2016 ACM/ICPC 大连网络赛 J题 dfs+树状数组+离散化

    题目链接 You are given a rootedrooted tree of NN nodes, labeled from 1 to NN. To the iith node a non-neg ...

  9. 2019 ICPC 上海网络赛 K. Peekaboo

    题目连接:https://nanti.jisuanke.com/t/41421 题意:给定三个整数a, b, c,求半径为a.b,圆心坐标为原点的两个同心圆上的整点间的距离为c的点对 题解:推一下圆上 ...

最新文章

  1. 【转载】css3 content 生成内容
  2. 前端性能优化:Add Expires headers
  3. 九章云极DataCanvas完成C轮融资:定义标准化AI基础架构未来
  4. 乐观锁-version的使用
  5. 开源GIS(六)——openlayers中overlay强大功能
  6. 全国各地车牌代码整理出数据库表,直接生成表
  7. css实现页面标签的跳转
  8. 使用Java将图片转成Base64编码,并压缩至40k
  9. PHP爱好者:十天学会php之第一天
  10. pyboard使用心得记录-基于对sk6812的控制(欢迎补充)
  11. hive执行insert overwrite失败,报错 could notbe cleaned up错误
  12. Transformer BEV perception
  13. wincc项目 CS结构 ES工程师站下装到OS服务器失败或者特别慢的原因
  14. b站电脑测试用什么软件,使用BiliBili访问诊断工具检测哔哩哔哩网络的方法
  15. 微软project服务器搭建,Project Server 2013 安装和部署概述
  16. 在英语课堂中培养学生音素觉知的初步探讨(Phonemic Awareness)
  17. 初学狄克斯特拉算法~(待提高)
  18. blueprint 实例
  19. Top01-0010、img标签的属性
  20. 南加大计算机专业本科sat要求,南加州大学本科申请条件有哪些?

热门文章

  1. 在单点登录中,如果cookie被禁用了怎么办?
  2. ds12c887c语言程序,时钟芯片ds12c887的C51驱动程序
  3. spark性能优化(二)数据倾斜问题
  4. RecyclerView之利用ItemDecoration实现万能分割线
  5. 《软件技术学研会-技术培训》第0章 操作系统安装
  6. 【考研英语】作文只是背模板?正确复习姿势了解下
  7. hash函数的简单介绍
  8. 方正证券:新一代认证核心系统换代升级,坚持实践金融科技全栈自主可控
  9. 【软件工程】软件维护
  10. 数学模型转化为计算机语言,程序设计语言类课程教学选题方法探讨