📌  相关文章
📜  根据给定的规则对 Queue 执行给定的查询

📅  最后修改于: 2022-05-13 01:56:05.440000             🧑  作者: Mango

根据给定的规则对 Queue 执行给定的查询

给定一个由前N个自然数组成的队列和类型为 {E, X} 的查询Query[][] ,任务是根据以下规则对给定队列执行给定查询:

  • 如果E的值为1 ,则从队列中弹出最前面的元素。
  • 如果E的值为2 ,则将值X从队列中删除(如果存在)。
  • 如果E的值为3 ,则在队列中找到X的位置(如果存在)。否则,打印“-1”

例子:

方法:给定的问题可以通过使用贪婪方法和二分搜索来解决。请按照以下步骤解决给定的问题。

  • 将变量countE1初始化为0以计算事件E1的发生次数。
  • 当查询类型为E1时,将countE1的值增加1
  • 维护一个集合数据结构,该结构存储在执行查询操作时删除的元素。
  • 对于类型 3的查询,请执行以下步骤:
    • 初始化一个变量,比如position来存储X的初始位置。
    • 在集合中查找X的值以检查X是否已被删除,如果X存在于集合中,则打印-1 。否则,求X的位置。
    • 用迭代器遍历集合,如果的值> X ,则跳出循环。否则,将位置减少1
    • 在位置变量中打印X存储的最终位置。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to perform all the queries
// operations on the given queue
void solve(int n, int m, int** queries)
{
    // Stores the count query of type 1
    int countE1 = 0;
    set removed;
 
    for (int i = 0; i < m; i++) {
 
        // Event E1: increase countE1
        if (queries[i][0] == 1)
            ++countE1;
 
        // Event E2: add the X in set
        else if (queries[i][0] == 2)
            removed.insert(queries[i][1]);
 
        // Event E3: Find position of X
        else {
 
            // Initial position is
            // (position - countE1)
            int position = queries[i][1]
                           - countE1;
 
            // If X is already removed or
            // popped out
            if (removed.find(queries[i][1])
                    != removed.end()
                || position <= 0)
                cout << "-1\n";
 
            // Finding the position of
            // X in queue
            else {
 
                // Traverse set to decrease
                // position of X for all the
                // number removed in front
                for (int it : removed) {
 
                    if (it > queries[i][1])
                        break;
 
                    position--;
                }
 
                // Print the position of X
                cout << position << "\n";
            }
        }
    }
}
 
// Driver Code
int main()
{
    int N = 5, Q = 3;
    int** queries = new int*[Q];
    for (int i = 0; i < Q; i++) {
        queries[i] = new int[2];
    }
 
    queries[0][0] = 1;
    queries[0][1] = 0;
    queries[1][0] = 3;
    queries[1][1] = 3;
    queries[2][0] = 2;
    queries[2][1] = 2;
 
    solve(N, Q, queries);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to perform all the queries
// operations on the given queue
static void solve(int n, int m, int [][]queries)
{
   
    // Stores the count query of type 1
    int countE1 = 0;
    HashSet removed = new HashSet();
 
    for (int i = 0; i < m; i++) {
 
        // Event E1: increase countE1
        if (queries[i][0] == 1)
            ++countE1;
 
        // Event E2: add the X in set
        else if (queries[i][0] == 2)
            removed.add(queries[i][1]);
 
        // Event E3: Find position of X
        else {
 
            // Initial position is
            // (position - countE1)
            int position = queries[i][1]
                           - countE1;
 
            // If X is already removed or
            // popped out
            if (removed.contains(queries[i][1])
                || position <= 0)
                System.out.print("-1\n");
 
            // Finding the position of
            // X in queue
            else {
 
                // Traverse set to decrease
                // position of X for all the
                // number removed in front
                for (int it : removed) {
 
                    if (it > queries[i][1])
                        break;
 
                    position--;
                }
 
                // Print the position of X
                System.out.print(position+ "\n");
            }
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 5, Q = 3;
    int [][]queries = new int[Q][2];
 
    queries[0][0] = 1;
    queries[0][1] = 0;
    queries[1][0] = 3;
    queries[1][1] = 3;
    queries[2][0] = 2;
    queries[2][1] = 2;
 
    solve(N, Q, queries);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# python program for the above approach
 
# Function to perform all the queries
# operations on the given queue
 
 
def solve(n, m, queries):
 
        # Stores the count query of type 1
    countE1 = 0
    removed = set()
 
    for i in range(0, m):
 
                # Event E1: increase countE1
        if (queries[i][0] == 1):
            countE1 += 1
 
            # Event E2: add the X in set
        elif(queries[i][0] == 2):
            removed.add(queries[i][1])
 
            # Event E3: Find position of X
        else:
 
                        # Initial position is
                        # (position - countE1)
            position = queries[i][1] - countE1
 
            # If X is already removed or
            # popped out
 
            if (queries[i][1] in removed or position <= 0):
                print("-1")
 
                # Finding the position of
                # X in queue
            else:
 
                                # Traverse set to decrease
                                # position of X for all the
                                # number removed in front
                for it in removed:
                    if (it > queries[i][1]):
                        break
 
                    position -= 1
 
                    # Print the position of X
                print(position)
 
 
# Driver Code
if __name__ == "__main__":
 
    N = 5
    Q = 3
    queries = [[0 for _ in range(2)] for _ in range(Q)]
    queries[0][0] = 1
    queries[0][1] = 0
    queries[1][0] = 3
    queries[1][1] = 3
    queries[2][0] = 2
    queries[2][1] = 2
 
    solve(N, Q, queries)
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
public class GFG {
     
// Function to perform all the queries
// operations on the given queue
static void solve(int n, int m, int [,]queries)
{
   
    // Stores the count query of type 1
    int countE1 = 0;
    HashSet removed = new HashSet();
 
    for (int i = 0; i < m; i++) {
 
        // Event E1: increase countE1
        if (queries[i, 0] == 1)
            ++countE1;
 
        // Event E2: add the X in set
        else if (queries[i, 0] == 2)
            removed.Add(queries[i, 1]);
 
        // Event E3: Find position of X
        else {
 
            // Initial position is
            // (position - countE1)
            int position = queries[i, 1]
                           - countE1;
 
            // If X is already removed or
            // popped out
            if (removed.Contains(queries[i, 1])
                || position <= 0)
                Console.WriteLine("-1\n");
 
            // Finding the position of
            // X in queue
            else {
 
                // Traverse set to decrease
                // position of X for all the
                // number removed in front
                foreach (int it in removed) {
 
                    if (it > queries[i, 1])
                        break;
 
                    position--;
                }
 
                // Print the position of X
                Console.WriteLine(position+ "\n");
            }
        }
    }
}
 
// Driver Code
public static void Main (string[] args) {
     
    int N = 5, Q = 3;
    int [,]queries = new int[Q, 2];
 
    queries[0, 0] = 1;
    queries[0, 1] = 0;
    queries[1, 0] = 3;
    queries[1, 1] = 3;
    queries[2, 0] = 2;
    queries[2, 1] = 2;
 
    solve(N, Q, queries);
}
}
 
// This code is contributed by code_hunt.


Javascript


输出:
2

时间复杂度:

  • 对于类型 1 的查询: O(1)
  • 对于类型 2 的查询: O(log N)
  • 对于类型 3 的查询: O(N 2 log N)

辅助空间: O(N)