3279 - I2P(I)2025_Chou_Hu_Hw9_ClassB Scoreboard

Time

2025/12/02 20:30:00 2025/12/09 18:30:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
14853 WindowedQueue
14854 Min-Max Priority Queue

14853 - WindowedQueue   

Description

People are lining for shu cream. To make sure the customer won't be waiting too long, the shu cream store decided to serve multiple people at the same time. The number of people that the store can serve simultaneously is call the window size S. The lining queue will be divided into serveral windows accroding to the window size, where the people in the same window can be served at the same time.

For example, if the current queue is [1, 2, 3, 4, 5, 6, 7, 8] and S=2, then each window will be: [1, 2], [3, 4], [5, 6], [7, 8]. Similarly, if S=3, then each window will be [1, 2, 3], [4, 5, 6], [7, 8]

Now, you need to implement a data structure WindowedQueue to simulate the lining queue, which supports the following operations:

  • SET_WIN_SIZE S: Set the new window size to S 
  • INSERT ID: Add a new customer ID to the end of queue, where ID is an integer.
  • SERVE: Serve the customers in the first window and print out the customers that is served. After that, these customers will be removed from the queue, and the customers in the seoncd window will be promoted to the first window, and so on. For example, suppose that the current queue is [1, 2, 3, 4, 5, 6, 7, 8] and the window size is 3. After a SERVE operation, customers [1, 2, 3] is removed from the queue, and the second window [4, 5, 6] will become the first window. Similarly, the third window [7, 8] will become the second window.
  • LEN: Print the number of windows.
  • SHOW_QUEUE k: Print the first k windows

Initially, the queue is empty and the window size is set to 2.

Given the template:

class WindowedQueueIterator:
    def __init__(self, windowed_list):
        self.windowed_list = windowed_list
        self.index = 0
 
    def __next__(self):
        # TODO:
        # Return next window and increase self.index
        raise NotImplementedError
 
 
class WindowedQueue:
    def __init__(self, window_size=2):
        self.data = list()
        self.set_window_size(window_size)  
 
    def set_window_size(self, window_size):
        # TODO:
        # set self.window_size to window size
        raise NotImplementedError
 
    def __len__(self):
        # TODO:
        # return the number of windows
        raise NotImplementedError
 
    def __iter__(self):
        return WindowedQueueIterator(self)
 
    def insert(self, ID):
        # TODO:
        # insert ID to the end of queue
        raise NotImplementedError
 
    def serve(self):
        # TODO:
        # serve the customers in the first window, and remove these customers from the queue
        # if the queue is empty, return None
        # otherwise, return a list that contains the served customers 
        # (the order of customers should be same as the queue)
        raise NotImplementedError
 
# DO NOT modify the code below
 
num_ops = int(input())
wl = WindowedQueue()
 
for i in range(num_ops):
    op = input().split()
 
    if op[0] == "INSERT":
        ID = int(op[1])
        wl.insert(ID)
    elif op[0] == "SERVE":
        served = wl.serve()
        if served != None:
            print(f"Serve customers: {served}")
        else:
            print("No customer to be served")
    elif op[0] == "LEN":
        print(len(wl))
    elif op[0] == "SET_WIN_SIZE":
        S = int(op[1])
        wl.set_window_size(S)
    elif op[0] == "SHOW_QUEUE":
        k = int(op[1])
        it = iter(wl)
        for _ in range(k):
            if _ == k - 1:
                print(next(it))
            else:
                print(next(it), end=',')

Input

The first line contains an integer T (T <= 1000), representing the number of operations

For the next T lines, each line contain one of an operation above.

The constraints of each operations:

  • SET_WIN_SIZE S:  S <= 1000 
  • INSERT ID: ID <= 1000
  • SERVE: No extra contraint
  • LEN: No extra contraint
  • SHOW_QUEUE k: k is guaranteed to be less than or equal to the number of windows

Output

For each following operation, output the corresponding text:

  • SERVE: Output Serve customers: , followed by a list of ID of customers in the first window. If the queue is empty, output No customer to be served
  • LEN: Output an integer, representing the number of windows.
  • SHOW_QUEUE k: Output k lists, the i-th list indicates the IDs of customers in the i-th window.

Sample Input  Download

Sample Output  Download

Tags




Discuss




14854 - Min-Max Priority Queue   

Description

A min-priority-queue is a data structure that you can insert element to it and delete element from it. However, you can't delete arbitrary element in a min-priority-queue, you can only delete the element with minimum value in it. Similarly, you can only delete the element with maximum value in a max-priority-queue.

In this problem, you are going to maintain a min-priority-queue and a max-priority-queue, which support the following operations:

  • INSERT val: Insert a element val to both min-priority-queue and max-priority-queue
  • DELETE M: Delete an element from the max-priority-queue
  • DELETE m: Delete an element from the min-priority-queue
  • ITER M: Print out the elements in the max-priority-queue, in insertion order
  • ITER m: Print out the elements in the min-priority-queue, in insertion order
  • INTER: Print how many number are both in the min-priority-queue and the max-priority-queue 

Originally, both max-priority-queue and min-priority-queue are empty.

Given the template:

from abc import ABC, abstractmethod

class BasicPQ(ABC):
    def __init__(self):
        self.data = list()
    
    def insert(self, val):
        # TODO
        # insert an element with value val into the priority queue
        raise NotImplementedError

    @abstractmethod
    def delete(self):
        raise NotImplementedError

    def __iter__(self):
        self.index = 0
        return self
    def __next__(self):
        # TODO
        # if there is a next (in the insertion order) value in self.data, return it and increase self.index 
        # otherwise, raise StopIteration
        raise NotImplementedError

class MinPQ(BasicPQ):
    def delete(self):
        # TODO
        # delete the minimum value in the priority queue
        raise NotImplementedError

class MaxPQ(BasicPQ):
    def delete(self):
        # TODO
        # delete the maximum value in the priority queue
        raise NotImplementedError
        
def Inter(max_pq, min_pq):
    # TODO
    # return the number of elements that are in both min-priority-queue and max-priority-queue
    rase NotImplementedError
    

# DO NOT change the code below

num_ops = int(input())
min_pq = MinPQ()
max_pq = MaxPQ()

for i in range(num_ops):
    op = input().split()
    if op[0] == "INSERT":
        val = int(op[1])
        min_pq.insert(val)
        max_pq.insert(val)
    elif op[0] == "DELETE":
        if op[1] == "M":
            max_pq.delete()
        else:
            min_pq.delete()
    elif op[0] == "ITER":
        if op[1] == "M":
            it = iter(max_pq)    
            print("Elements in the max-priority-queue:", end=' ')
            for i in it:
                print(i, end=' ')
            print()
        else:
            it = iter(min_pq)    
            print("Elements in the min-priority-queue:", end=' ')
            for i in it:
                print(i, end=' ')
            print()
    elif op[0] == "INTER":
        print(Inter(max_pq, min_pq))

Input

The first line contains an integer T (T <= 1000), representing the number of operations

For the next T lines, each line contain one of an operation above.

The constraints of each operations:

  • INSERT val:  val <= 1000, all vals are distinct
  • DELETE M: It is guaranteed that the max-priority-queue must be non-empty
  • DELETE m: It is guaranteed that the min-priority-queue must be non-empty
  • ITER M: It is guaranteed that the max-priority-queue must be non-empty
  • ITER m: It is guaranteed that the min-priority-queue must be non-empty
  • INTER: No extra constraint

Output

For each following operation, output the corresponding text:

  • ITER M: Output Elements in the max-priority-queue, followed by the elements in the max-priority-queue. A space should be added right after each element.
  • ITER m: Output Elements in the min-priority-queue, followed by the elements in the min-priority-queue. A space should be added right after each element.
  • INTER: Output a number, representing the number of elements that are both in the min-priority-queue and the max-priority-queue  

Sample Input  Download

Sample Output  Download

Tags




Discuss