zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    网页布局
    Block Formatting Context
    SEO初识
    新的一个月,就这么不知不觉的来临了
    Js结束,项目进行中
    JS学习中....
    ws快捷键
    Html的学习以及webstorm的使用
    从事前端开发应该了解的CSS原理
    jQuery动画
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11155479.html
Copyright © 2011-2022 走看看