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;
}
- “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp
- 虎嗅主站盲打成功(已进后台)
- 我的第五个网页制作:pre、html转义、abbr标签的使用
- UVALive 3882 - And Then There Was One【约瑟夫问题】
- 超级账本项目:链码示例
- 我的第六个网页制作:table标签
- POJ 1163 The Triangle【dp+杨辉三角加强版(递归)】
- UVA 11039-Building designing【贪心+绝对值排序】 UVA11039-Building designing
- UVA 11636-Hello World!(水题,猜结论) UVA11636-Hello World!
- 百度某SDK设计缺陷导致手机敏感信息泄露(IMEI号和地理位置信息等)
- HDU 1004 Let the Balloon Rise【STL<map>】
- UVA 10881 - Piotr's Ants【模拟+思维】
- DFS中的奇偶剪枝学习笔记
- POJ 3154 Graveyard【多解,数论,贪心】
- 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 数组属性和方法