Search code examples
queue

how to print values for x dependent on t in a queue for this question given below?


The question:

Queue is a container of elements that are inserted and deleted according to FIFO (First In First Out).

For n queues Qi ( i = 0, 1, ..., n-1 ), perform a sequence of the following operations.

I have three operations:

if q1=0, Insert an integer x to Qt

if q1=1, Report the value which should be deleted next from Qt . If Qt is empty, do nothing.

if q1=2,Delete an element from Qt . If Qt is empty, do nothing

In the initial state, all queues are empty.

my code is:

#include <bits/stdc++.h>
using namespace std;
#define fio                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define ll long long
int main()
{
    fio;
    ll n, q;
    cin >> n >> q;
    queue<pair<ll, ll>> qu;
    while (q--)
    {
        ll q1, t, x;
        cin >> q1 >> t;
        if (q1 == 0)
        {
            cin >> x;
            qu.push(make_pair(t, x));
        }
        else if (q1 == 1)
        {
            while (!qu.empty())
            {
                pair<ll, ll> f = qu.front();
                if (t == f.first)
                    cout << f.second << endl;
            }
            qu.pop();
        }
        else if (q1 == 2)
        {
            while (!qu.empty())
            {
                qu.pop();
            }
        }
    }
    return 0;
}


input:3 9
0 0 1
0 0 2
0 0 3
0 2 4
0 2 5
1 0
1 2
2 0
1 0

output:
1
4
2

but I am getting 1,2,3.  I had tried to use break but it was giving output: 1 4 but it was not working for last 1 0 value.
I have used a pair in queue that is storing values of t ,x .In condition q1==1 I have tried to match the values of first in pair with t. if it matches then I will print the second value of pair.
how can I fix it?

Solution

  • Data structure:

    • First, we could just use std::tuple<ll, ll, ll> with three elements for each operation. For q1 in (1, 2), we add -1 as the third element.

    • We exactly simulate the given ifs.

    • Then, it is just a for loop with three conditions.

    For each operation:

    • if q1 == 0, we append x to queue[t].
    • if q1 == 1, we first check if there is a non-empty queue[t], we print out its front()`.
    • if q1 == 2, and there is a non-empty a queue[t], we pop() from queue[t].
    #include <iostream>
    #include <queue>
    #include <tuple>
    #include <vector>
    
    #define fio                                \
        std::ios_base::sync_with_stdio(false); \
        std::cin.tie(NULL);
    #define ll long long
    
    int main()
    {
        fio;
    
        ll n = 3, q = 9;
    
        std::vector<std::tuple<ll, ll, ll>> operations = {
            {0, 0, 1},
            {0, 0, 2},
            {0, 0, 3},
            {0, 2, 4},
            {0, 2, 5},
            {1, 0, -1},
            {1, 2, -1},
            {2, 0, -1},
            {1, 0, -1}};
    
        std::vector<std::queue<ll>> queues(n);
    
        for (const auto &[q1, t, x] : operations)
        {
            if (q1 == 0)
            {
                queues[t].push(x);
            }
            else if (q1 == 1)
            {
                if (!queues[t].empty())
                {
                    std::cout << queues[t].front() << "\n";
                }
            }
            else if (q1 == 2)
            {
                if (!queues[t].empty())
                {
                    queues[t].pop();
                }
            }
        }
    
        return 0;
    }
    
    
    

    Prints

    1

    4

    2