• Codeforces Round #569 (Div. 2) C. Valeriy and Deque


    Codeforces Round #569 (Div. 2)

    C. Valeriy and Deque

    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.

    Input

    The first line contains two integers n and q (2≤n≤105, 0≤q≤3⋅105) — the number of elements in the deque and the number of queries. The second line contains n integers a1, a2, ..., an, where ai (0≤ai≤109) — the deque element in i-th position. The next qlines contain one number each, meaning mj (1≤mj≤1018).

    Output

    For each teacher's query, output two numbers A and B — the numbers that Valeriy pulls out of the deque for the mj-th operation.

    Examples

    input

    5 3

    1 2 3 4 5

    1

    2

    10

    output

    1 2

    2 3

    5 2

    input

    2 0

    0 0

    output

     

    Note

    Consider all 10 steps for the first test in detail:

    1. [1,2,3,4,5] — on the first operation, A and B are 1 and 2, respectively.

    So, 2 we write to the beginning of the deque, and 1 — to the end.

    We get the following status of the deque: [2,3,4,5,1].

       2.[2,3,4,5,1]⇒A=2,B=3.

       3.[3,4,5,1,2]

           4.[4,5,1,2,3]

           5.[5,1,2,3,4]

       6.[5,2,3,4,1]

       7.[5,3,4,1,2]

       8.[5,4,1,2,3]

       9.[5,1,2,3,4]

       10.[5,2,3,4,1]⇒A=5,B=2.

    题意:题目意思是给你n个数放双端队列里面,有一个操作:取出前两个数,大的数放前面,小的或者一样大的放后面,问你经过m次操作后的前两个数是什么。

    思路:现学了一下deque,感觉还行(我比较菜,以前还不会orzorz),通过观察样例一的解释我们可以发现,

    当双端队列的队首经过几次操作后变成最大值时,后面再操作n-1次后会循环出现相同的结果,

    因为最大值在队首时进行操作一定是把第二个数放到队尾,了解了这种情况之后,

    我们只需要预处理出不循环的前一部分解和循环的后一部分解就可以直接输出了......


     

      1 #include<iostream>
      2 #include<cstring>
      3 #include<cstdio>
      4 #include<cmath>
      5 #include<algorithm>
      6 #include<map>
      7 #include<set>
      8 #include<vector>
      9 #include<queue>
     10 using namespace std;
     11 #define ll long long 
     12 const int mod=1e9+7;
     13 const int inf=1e9+7;
     14 
     15 deque<int>d;//双端队列 
     16 
     17 int main()
     18 {
     19     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     20     
     21     d.clear();
     22     
     23     int n,op;
     24     cin>>n>>op;//n个数,op个操作 
     25     
     26     int maxx=-1;//找到最大值 
     27     int num;
     28     for(int i=0;i<n;i++)
     29     {
     30         cin>>num;//输入 
     31         if(num>maxx)
     32             maxx=num;//刷新最大值 
     33         d.push_back(num);//尾部导入双端队列 
     34     }
     35     
     36     if(op==0)//特判一波 
     37         return 0;
     38     
     39     vector<pair<int,int> >v1;//这个是存无循环的前一部分解 
     40     vector<pair<int,int> >v2;//这个是存后面循环的后一部分解 
     41     
     42     v1.clear();
     43     v2.clear();
     44     
     45     int a,b;
     46     
     47     while(d[0]!=maxx)//先预处理找出前一部分解 
     48     {
     49         a=d[0];//a是第一个数 
     50         b=d[1];//b是第二个数 
     51         
     52         d.pop_front(); 
     53         d.pop_front();
     54         //按题意对双端队列操作一波
     55         if(a>b)
     56         {
     57             d.push_front(a);
     58             d.push_back(b);
     59         }
     60         else
     61         {
     62             d.push_front(b);
     63             d.push_back(a);
     64         }
     65         
     66         v1.push_back({a,b});//将解导入v1数组 
     67         
     68     }
     69     
     70     //for(int i=0;i<v1.size();i++)//可以看一看前一部分解 
     71     //    cout<<v1[i].first<<" "<<v1[i].second<<endl;
     72     
     73     ll int xunhuan=n-1;//后一部分的循环节长度 
     74     
     75     ll int m=v1.size();//前一部分解的长度 
     76     
     77     for(int i=0;i<xunhuan;i++)//处理出循环节 
     78     {
     79         a=d[0];
     80         b=d[1];
     81         d.pop_front();
     82         d.pop_front();
     83         
     84         if(a>b)
     85         {
     86             d.push_front(a);
     87             d.push_back(b);
     88         }
     89         else
     90         {
     91             d.push_front(b);
     92             d.push_back(a);
     93         }
     94         
     95         v2.push_back({a,b});
     96     }
     97     
     98     //for(int i=0;i<v2.size();i++)//可以看一看后一部分循环解的部分 
     99     //    cout<<v2[i].first<<" "<<v2[i].second<<endl;
    100     
    101     ll int caozuo;
    102     
    103     for(int i=0;i<op;i++)
    104     {
    105         cin>>caozuo;
    106         
    107         if(caozuo>=1&&caozuo<=v1.size())//是前一部分的解 
    108         {
    109             cout<<v1[caozuo-1].first<<" "<<v1[caozuo-1].second<<endl;
    110         }
    111         else
    112         {
    113             caozuo-=v1.size();//这里要减掉再膜循环节,%%% 
    114             caozuo%=xunhuan;
    115             if(caozuo==0)//0-1越界了,特判一下orzorz 
    116                 cout<<v2[v2.size()-1].first<<" "<<v2[v2.size()-1].second<<endl;
    117             else
    118                 cout<<v2[caozuo-1].first<<" "<<v2[caozuo-1].second<<endl;
    119         }
    120         
    121     }
    122     
    123     return 0;
    124 }
    大佬见笑,,
  • 相关阅读:
    WINCE串口驱动MDD层代码简单分析
    WinCE下,快速编译驱动及BSP
    如何使用ulink2烧写二进制文件
    PB6.0 快速编译单个驱动技巧
    WinCE5.0和WinCE6.0下,编译选项介绍
    WINCE串口驱动PDD层代码简单分析
    浅谈WinCE平台USB摄像头驱动开发流程
    WinCE中,环境变量的添加,删除和查询
    WinCE API
    WINCE 6.0安装顺序说明
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/11070195.html
Copyright © 2020-2023  润新知