[题目传送门]
题意:给n个数的数组,进行操作(每次操作取数组前两个,将大数放在数组前,小数放在数组尾),进行q次询问。每次询问第x(long long )次操作时取出的两个数。
分析:这道题是对stl deque的运用,具体用法在大佬这里。deque跟vector有点像,不过deque可以在队列两端进行操作。
这道题的思路时将最大值之前的取值用vector记录下来,模拟操作。最大值之后,每次第一个值都是最大值mx,第二个值是循环的,取模后便可得到。
q.front():返回第一个元素的引用。
q.back():返回最后一个元素的引用。
q.push_front(x):把元素x插入到双向队列的头部。
q.pop_front():弹出双向队列的第一个元素。
q.push_back(x):把元素x插入到双向队列的尾部。
q.pop_back():弹出双向队列的最后一个元素。
代码如下:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<deque>
using namespace std;
typedef long long ll;
const int maxn=1e5+100;
int main()
{
int n,k;
int x;
int mx=-1;
int ans;
int ant;
vector<int>a,b;
scanf("%d%d",&n,&k);
deque<int>q;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
q.push_back(x);
mx=max(mx,x);//获取最大值
}
while(1)
{
int x,y;
x=q.front();
q.pop_front();
if(x==mx)
break;
y=q.front();
q.pop_front();
a.push_back(x);//记录下到达最大之前的取值
b.push_back(y);
if(x>y)//模拟操作
{
q.push_front(x);
q.push_back(y);
}
else
{
q.push_front(y);
q.push_back(x);
}
}
ant=a.size();
while(k--)
{
ll x;
scanf("%lld",&x);
// printf("ant %d\n",ant);
if(x<=(ll)ant)
printf("%d %d",a[x-1],b[x-1]);
else
{
ans=(x-ant+(n-1))%(n-1);
if(ans==0)//ans=n-1时
ans=n-1;
printf("%d %d",mx,q[ans-1]);
}
if(k)
printf("\n");
}
return 0;
}
/*
5 12
1 2 3 4 5
*/