Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed to implement a stack with an extra operation: PeekMedian – return the median value of all the elements in the stack. With N elements, the median value is defined to be the (N/2)-th smallest element if N is even, or ((N+1)/2)-th if N is odd.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N(≤105). Then N lines follow, each contains a command in one of the following 3 formats:
1 2 3
Push key Pop PeekMedian
where key is a positive integer no more than 105.
Output Specification:
For each Push command, insert key into the stack and output nothing. For each Pop or PeekMedian command, print in a line the corresponding returned value. If the command is invalid, print Invalid instead.
Sample Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
17 Pop PeekMedian Push 3 PeekMedian Push 2 PeekMedian Push 1 PeekMedian Pop Pop Push 5 Push 4 PeekMedian Pop Pop Pop Pop
stk = [] # 栈 commands = int(input()) for _ in range(commands): info = input() if info == "Pop": if len(stk) == 0: print("Invalid") else: print(stk.pop()) elif info == "PeekMedian": if len(stk) != 0: print(sorted(stk)[(len(stk) - 1)//2]) else: print("Invalid") else: num = int(info.split()[1]) stk.append(num)
bucket = [0for _ in range(100001)] stk = [] commands = int(input()) for _ in range(commands): info = input() if info == "Pop": if len(stk) == 0: print("Invalid") else: bucket[stk[-1]] -= 1 print(stk.pop()) elif info == "PeekMedian": if len(stk) != 0: target = (len(stk) + 1) // 2 index = 0 for num, count in enumerate(bucket): index += count if index >= target: print(num) break else: print("Invalid") else: num = int(info.split()[1]) bucket[num] += 1 stk.append(num)
bucket_count = [0for _ in range(100)] bucket = [[0for _ in range(1000)] for _ in range(100)] stk = [] commands = int(input()) for _ in range(commands): info = input() if info == "Pop": if len(stk) == 0: print("Invalid") else: num = stk[-1] bucket[num // 1000][num % 1000] -= 1 bucket_count[num // 1000] -= 1 print(stk.pop()) elif info == "PeekMedian": if len(stk) != 0: target = (len(stk) + 1) // 2 index = 0 for num, count in enumerate(bucket_count): index += count if index >= target: index -= count for num_2, count_2 in enumerate(bucket[num]): index += count_2 if index >= target: print(num * 100 + num_2) break break else: print("Invalid") else: num = int(info.split()[1]) bucket_count[num // 1000] += 1 bucket[num // 1000][num % 1000] += 1 stk.append(num)
bucket_1 = [0for _ in range(100)] bucket_2 = [[0for _ in range(100)] for _ in range(100)] bucket_3 = [[[0for _ in range(10)] for _ in range(100)] for _ in range(100)] stk = [] commands = int(input()) for _ in range(commands): info = input() if info == "Pop": if len(stk) == 0: print("Invalid") else: num = stk[-1] bucket_3[num // 1000][(num % 1000)//10][num % 10] -= 1 bucket_2[num // 1000][(num % 1000)//10] -= 1 bucket_1[num // 1000] -= 1 print(stk.pop()) elif info == "PeekMedian": if len(stk) != 0: target = (len(stk) + 1) // 2 index = 0 for num, count in enumerate(bucket_1): index += count if index >= target: index -= count for num_2, count_2 in enumerate(bucket_2[num]): index += count_2 if index >= target: index -= count_2 for num_3, count_3 in enumerate(bucket_3[num][num_2]): index += count_3 if index >= target: print(num * 1000 + num_2 * 10 + num_3) break break break else: print("Invalid") else: num = int(info.split()[1]) bucket_3[num // 1000][(num % 1000)//10][num % 10] += 1 bucket_2[num // 1000][(num % 1000)//10] += 1 bucket_1[num // 1000] += 1 stk.append(num)