A. Valeriy and Deque Codeforces Solution | Codeforces Round 569 (Div. 1)


Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with 𝑛 elements. The 𝑖 -th element is 𝑎𝑖 (𝑖 = 1,2,…,𝑛 ). He gradually takes the first two leftmost elements from the deque (let's call them 𝐴 and 𝐵 , respectively), and then does the following:
Solution
void solve()
{

    int n, m;
    cin >> n >> m;

    vi v(n);
    deque<int> q;
    for (int i = 0; i < n; i++)
    {
        cin >> v[i];
        q.push_back(v[i]);
    }

    vector<pii> f;
    int big = 0, small;
    for (int i = 0; i < 2 * n; i++)
    {
        int first = q.front();
        q.pop_front();
        int second = q.front();
        q.pop_front();
        big = max(first, second);
        small = min(first, second);
        f.push_back({first, second});
        q.push_front(big);
        q.push_back(small);
    }

    // print1(f);

    for (int i = 0; i < m; i++)
    {
        int k;
        cin >> k;

        if (k <= n)
        {
            cout << f[k - 1].first << " " << f[k - 1].second << endl;
        }
        else
        {
            k %= (n - 1);
            k += n - 2;
            cout << f[k].first << " " << f[k].second << endl;
        }
    }

    // 1 2 3 4 5
    // 2 3 4 5 1
    // 3 4 5 1 2
    // 4 5 1 2 3

    // 5 1 2 3 4
    // 5 2 3 4 1
    // 5 3 4 1 2
    // 5 4 1 2 3

    // 5 1 2 3 4
    // 5 2 3 4 1
    // 5 3 4 1 2
    // 5 4 1 2 3

    // 5 1 2 3 4
    // 5 2 3 4 1
}
TagsA. Valeriy and DequeCodeforces Round 569 (Div. 1)data structuresimplementation*1500Codeforces
Raunit Verma-picture
Raunit Verma

Technical Writer at CodingKaro

Share this on
CodingKaro Poster
CodingKaro
4.7

3K+ Downloads

One of its unique features is an automated coding contest reminder, which sets alarms for upcoming coding contests on popular platforms like CodeChef, CodeForces, and LeetCode, without the need for user input. This feature ensures that users never miss a coding contest and can stay updated with the latest coding challenges.

Download CodingKaro
Other Blogs in Codeforces