代码之家  ›  专栏  ›  技术社区  ›  Keya Rani Mondal

如何在下面给出的问题的队列中打印依赖于t的x的值?

  •  1
  • Keya Rani Mondal  · 技术社区  · 1 年前

    问题是:

    队列是根据FIFO(先进先出)插入和删除的元素的容器。

    对于n个队列Q (i=0,1,…,n-1),执行以下操作的序列。

    我有三个操作:

    如果q1=0,则在Q中插入一个整数x t

    如果q1=1,则报告下一个应该从Q中删除的值 t .如果Q t 是空的,什么也不做。

    如果q1=2,则从Q中删除元素 t .如果Q t 是空的,什么也不做

    在初始状态下,所有队列都是空的。

    我的代码是:

    #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?
    
    1 回复  |  直到 1 年前
        1
  •  0
  •   user24714692    1 年前

    数据结构:

    • 首先,我们可以使用 std::tuple<ll, ll, ll> 每个操作有三个元件。对于 q1 在里面 (1, 2) ,我们添加-1作为第三个元素。

    • 我们完全模拟给定的 if s

    • 那么,它只是一个有三个条件的for循环。

    对于每个操作:

    • 如果 q1 == 0 ,我们将x附加到 queue[t] .
    • 如果 q1 == 1. , we first check if there is a non-empty 队列[t] , we print out its 正面
    • 如果 q1 == 2 ,并且有一个非空的 队列[t] 我们 pop() 从…起 队列[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;
    }
    
    
    

    打印

    1.

    4.

    2.