Codeforces Round #569 (Div. 2) C. Valeriy and Deque
链接:
https://codeforces.com/contest/1180/problem/C
题意:
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is ai (i = 1,2,…,n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A>B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A 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], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3,4,5,1,2].
The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number mj (j=1,2,…,q). It is required for each query to answer which two elements he will pull out on the mj-th operation.
Note that the queries are independent and for each query the numbers A and B 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.
思路:
考虑当第一个值为最大值时,后面的顺序就是数组排列的顺序。
先记录第一个不是最大值,然后队列进出队,当第一个值最大时,将剩余元素出队到数组。找的时候取模计算即可。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;
int a[MAXN];
int n, q;
int main()
{
int v, mmax = -1;
cin >> n >> q;
deque<int> que;
for (int i = 1;i <= n;i++)
{
cin >> v;
mmax = max(mmax, v);
que.push_back(v);
}
vector<pair<int, int> > par;
while (que.front() != mmax)
{
int fi = que.front();
que.pop_front();
int se = que.front();
que.pop_front();
par.emplace_back(fi, se);
if (fi < se)
swap(fi, se);
que.push_front(fi);
que.push_back(se);
}
que.pop_front();
int cnt = -1;
while (!que.empty())
{
a[++cnt] = que.front();
que.pop_front();
}
LL w;
while (q--)
{
cin >> w;
if (w <= par.size())
cout << par[w-1].first << ' ' << par[w-1].second << endl;
else
{
w -= par.size()+1;
cout << mmax << ' ' << a[w%(cnt+1)] << endl;
}
}
return 0;
}
Codeforces Round #569 (Div. 2) C. Valeriy and Deque的更多相关文章
- Codeforces Round #569 (Div. 2) 题解A - Alex and a Rhombus+B - Nick and Array+C - Valeriy and Dequ+D - Tolik and His Uncle
A. Alex and a Rhombus time limit per test1 second memory limit per test256 megabytes inputstandard i ...
- Codeforces Round #569 (Div. 2)A. Alex and a Rhombus
A. Alex and a Rhombus 题目链接:http://codeforces.com/contest/1180/problem/A 题目: While playing with geome ...
- Codeforces Round #569 (Div. 2) B. Nick and Array
链接: https://codeforces.com/contest/1180/problem/B 题意: Nick had received an awesome array of integers ...
- Codeforces Round #569 Div. 1
A:n-1次操作后最大值会被放到第一个,于是暴力模拟前n-1次,之后显然是循环的. #include<bits/stdc++.h> using namespace std; #define ...
- Codeforces Round #569 题解
Codeforces Round #569 题解 CF1179A Valeriy and Deque 有一个双端队列,每次取队首两个值,将较小值移动到队尾,较大值位置不变.多组询问求第\(m\)次操作 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
随机推荐
- django 数据库操作详解
Django配置使用mysql数据库 修改 settings.py 中的 DATABASES 注意:django框架不会自动帮我们生成mysql数据库,所以我们需要自己去创建. DATABASES ...
- windows vs2015 编译openssl 1.1.0c
1,到openssl官网下载源码. 2,安装activePerl,我放在网盘:https://pan.baidu.com/s/1ZHe24yRcPtIuSiEa-3oqxw 3.安装完毕后,使用 VS ...
- 使用SpringWebFlow
使用SpringWebFlow 本章主要内容: · 创建会话式的Web应用程序 · 定义流程状态和行为 Spring Web Flow 是Spring MVC 的扩展,它支持开发基于流程的应用程序.它 ...
- 【Python开发】【神经网络与深度学习】网络爬虫之图片自动下载器
python爬虫实战--图片自动下载器 之前介绍了那么多基本知识[Python爬虫]入门知识(没看的赶紧去看)大家也估计手痒了.想要实际做个小东西来看看,毕竟: talk is cheap show ...
- 快速乘+快速幂(用于模数超过int范围)
一般的快速幂并不适合模数大于int范围的情况,因为在乘法运算的过程可能会出现超出long long的情况出现.这个时候可以利用快速幂的思想使用快速乘,原理就是模拟乘法运算,将乘法运算分解成加法运算,再 ...
- [转帖]yum 下载rpm包 之前不知道具体的存放路径.
使用Yum下载RPM包-进击的二狗子-51CTO博客 https://www.2cto.com/os/201905/807225.html yumdownloader 命令 yum install y ...
- vue-cli3.0本地代理cookie跨域请求Nginx配置
由于后端需要通过请求取前端中的cookie信息,在本地开发模式中,直接请求接口,后端无法拿到前端cookie数据, 经测试需在 vue-cli 中使用代理,如果使用Nginx做反向代理需同时修改Ngi ...
- Linux等操作系统杂谈
这部分基本上都是感性认识,介绍一下发展历史什么的.所以基本上都不是我原创的,转载来源都标记在文中了,如果侵权的话请联系删除 操作系统发展历史吃瓜 <Unix.Windows.Mac OS.Lin ...
- django admin站点美化
使用django-simpleui 安装 pip install django-simpleui 源码地址 https://github.com/newpanjing/simpleui django ...
- 转载MySQL面试题
1.MySQL的复制原理以及流程 (1).复制基本原理流程 主:binlog线程--记录下所有改变了数据库数据的语句,放进master上的binlog中: 从:io线程--在使用start slave ...