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连试试水的深浅..... ...
随机推荐
- 1 Configuring SAP ERP Sales and Distribution -introduction to SAP
SAP is one of the most popular enterprise resource planning (ERP) solutions inthe world. It offers a ...
- getApplication()和getApplicationContext()区别
二者使用结果相同,我们写个代码分别打印二者返回结果,发现两个方法获取的是同一个对象. public class MainActivity extends Activity { @Override pr ...
- Day06:方法 / 猜字母游戏
JAVA方法 方法就是处理一个业务所需要编写的代码的代码段 方法特性 一个方法处理一个业务 方法代码编写,不和其他方法冲突 方法定义后可以随意调用 将main方法中的所有代码分散到各个普通方法中 减少 ...
- Kaggle试水之泰坦尼克灾难
比赛地址:https://www.kaggle.com/c/titanic 再次想吐槽CSDN,编辑界面经常卡死,各种按钮不能点,注释的颜色不能改,很难看清.写了很多卡死要崩溃. 我也是第一次参加这个 ...
- go的变量定义
package main //理解包的概念 import "fmt" var ( aa = 1 bb = "kkk" ss = true) func varia ...
- @OneToMany 一对多 通过表之间的链接
https://blog.csdn.net/qq_38157516/article/details/80146547 一对多 一个人对多张卡,但是一张卡只能对应一个人,典型的一对多关系,下面就用One ...
- HDU 1159 Common Subsequence 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- css overflow规定元素溢出框将会发生什么
- springBoot2.0配置profile
1. 使用yaml来配置,直接配置application.yml文件 server: port: 8888 spring: profiles: active: dev # 激活生产环境 --- # 测 ...
- D - 卿学姐与魔法
卿学姐与魔法 Time Limit: 1200/800MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...