#K92327. Queue Operations with Additional Functions

    ID: 38173 Type: Default 1000ms 256MiB

Queue Operations with Additional Functions

Queue Operations with Additional Functions

You are required to implement a queue data structure that supports two additional operations:

  • reverseFirstK(k): Reverse the order of the first \( k \) elements of the queue (if \( k \) is greater than the current size of the queue, reverse all elements).
  • removeEveryNth(n): Remove every \( n \)-th element from the queue. Elements are counted starting at 1. For example, if \( n = 2 \), remove the 2nd, 4th, ... elements. Note that if \( n = 1 \), all elements are removed.

The queue should also support the traditional operations:

  • enqueue x: Add the integer \( x \) to the end of the queue.
  • dequeue: Remove and return the front element of the queue. If the queue is empty, output \( -1 \).

Your program will process a series of operations read from standard input. For every dequeue operation, print the returned result to standard output on a separate line.

Make sure that your solution handles all operations efficiently so that it can pass all the test cases.

inputFormat

The input is given via standard input. The first line contains a single integer \( N \) representing the number of operations to perform. Each of the following \( N \) lines contains a command. The command will be one of the following:

  • enqueue x - Enqueue the integer \( x \) into the queue.
  • dequeue - Dequeue an element from the front of the queue and print its value. If the queue is empty, print \( -1 \).
  • reverseFirstK k - Reverse the order of the first \( k \) elements of the queue.
  • removeEveryNth n - Remove every \( n \)-th element from the queue.

It is guaranteed that \( n \) and \( k \) are positive integers.

outputFormat

For each dequeue operation, print the returned integer to standard output on a new line.

## sample
11
enqueue 10
enqueue 20
enqueue 30
reverseFirstK 2
dequeue
enqueue 40
removeEveryNth 2
dequeue
dequeue
removeEveryNth 5
dequeue
20

10 40 -1

</p>