2019.8.15乘兴打Codeforces Round #569 (Div. 2)小记
C. Valeriy and Deque
time limit per test
6 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with nn elements. The ii-th element is aiai (ii = 1,2,…,n1,2,…,n). He gradually takes the first two leftmost elements from the deque (let's call them AA and BB, respectively), and then does the following: if A>BA>B, he writes AA to the beginning and writes BB to the end of the deque, otherwise, he writes to the beginning BB, and AA writes to the end of the deque. We call this sequence of actions an operation.
For example, if deque was [2,3,4,5,1][2,3,4,5,1], on the operation he will write B=3B=3 to the beginning and A=2A=2 to the end, so he will get [3,4,5,1,2][3,4,5,1,2].
The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him qq queries. Each query consists of the singular number mjmj (j=1,2,…,q)(j=1,2,…,q). It is required for each query to answer which two elements he will pull out on the mjmj-th operation.
Note that the queries are independent and for each query the numbers AA and BB should be printed in the order in which they will be pulled out of the deque.
Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides.
Input
The first line contains two integers nn and qq (2≤n≤1052≤n≤105, 0≤q≤3⋅1050≤q≤3⋅105) — the number of elements in the deque and the number of queries. The second line contains nn integers a1a1, a2a2, ..., anan, where aiai (0≤ai≤109)(0≤ai≤109) — the deque element in ii-th position. The next qqlines contain one number each, meaning mjmj (1≤mj≤10181≤mj≤1018).
Output
For each teacher's query, output two numbers AA and BB — the numbers that Valeriy pulls out of the deque for the mjmj-th operation.
Examples
input
Copy
5 3
1 2 3 4 5
1
2
10
output
Copy
1 2
2 3
5 2
input
Copy
2 0
0 0
output
Copy
Note
- Consider all 10 steps for the first test in detail:
- [1,2,3,4,5][1,2,3,4,5] — on the first operation, AA and BB are 11 and 22, respectively. So, 22 we write to the beginning of the deque, and 11 — to the end. We get the following status of the deque: [2,3,4,5,1][2,3,4,5,1].
- [2,3,4,5,1]⇒A=2,B=3[2,3,4,5,1]⇒A=2,B=3.
- [3,4,5,1,2][3,4,5,1,2]
- [4,5,1,2,3][4,5,1,2,3]
- [5,1,2,3,4][5,1,2,3,4]
- [5,2,3,4,1][5,2,3,4,1]
- [5,3,4,1,2][5,3,4,1,2]
- [5,4,1,2,3][5,4,1,2,3]
- [5,1,2,3,4][5,1,2,3,4]
- [5,2,3,4,1]⇒A=5,B=2[5,2,3,4,1]⇒A=5,B=2.
思路:模拟双端队列题目,比B题简单...
#include<bits/stdc++.h>
#define rg register long long
#define inf 21474899993647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 100005
#define endl "n"
#define N 6000
const double eps=1e-8;
using namespace std;
inline ll read()
{
char ch=getchar();
ll s=0,w=1;
while(ch<48||ch>57)
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(ch>=48&&ch<=57)
{
s=(s<<1)+(s<<3)+(ch^48);
ch=getchar();
}
return s*w;
}
inline void write(ll x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+48);
}
ll n=read(),t=read();
map<ll,pair<ll,ll>>p;
deque<ll>q;
int main()
{
for(rg i=1;i<=n;i++)
{
ll x=read();
q.push_back(x);
}
for(rg i=1;i<=300000;i++)
{
ll a=q.front();
q.pop_front();
ll b=q.front();
q.pop_front();
if(a>b)
{
q.push_front(a);
q.push_back(b);
}
else
{
q.push_front(b);
q.push_back(a);
}
p[i]=make_pair(a,b);
}
for(rg i=1;i<=t;i++)
{
ll x=read();
if(x>2*n-2)
{
ll k=x-(2*n-2);
x=(k-1)%(n-1)+1+n-1 ;
}
cout<<p[x].first<<" "<<p[x].second<<endl;
}
return 0;
}
- 【系统日志】log4j配置学习总结
- 【译】MySQL char、varchar的区别
- 【jfinal修仙系列】修改ShiroPlugin支持jfinal3.0
- MySQL二进制日志
- 【nginx启动】 97 Address family not supported by protocol
- jfinal 内置的handler功能
- JS 对指定iframe 全屏操作
- 【jfinal修仙系列】扩展CacheInterceptor支持Redis缓存
- 基于Redis的定时任务
- 【jfinal】扩展JFIANL 支持加载jar包中SQL模板
- 【jfinal修仙系列】扩展ShiroCacheManager 支持Redis缓存
- 【springboot】 springboot 整合mybatis-plus
- jfinal-swagger让你的应用接口更加简单
- 【springboot】 spring session 分布式会话共享
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Ubuntu E: 无法获得锁 /var/lib/dpkg/lock-frontend – open (11: 资源暂时不可用)
- Ubuntu 18.04 安装MySQL时未提示输入密码的问题及解决方法
- ubuntu中修改grub的启动时间生成
- linux7下虚拟主机的三种实现方式
- 详解CentOS7下PostgreSQL 11的安装和配置教程
- CentOS下安装Memcached和PHP Memcached扩展
- Mac中文件权限查看和设置详解
- linux下open-vswitch安装卸载操作
- CentOS7安装GUI界面及远程连接的实现
- Centos7.4环境安装lamp-php7.0教程
- iOS摄像头推流(2)
- ubuntu 16.04LTS 开机启动自动更换壁纸的实现方法
- linux中Centos7的LVM磁盘扩容问题
- 如何禁止网站内容被搜索引擎收录的几种方法讲解
- Apache由http自动跳转到https的多种方法