Mario bought n math books and he recorded their prices. The prices are all integers, and…
- User
Mario bought n math books and he recorded their prices. The prices are all integers, and the price sequence is a = {a0, a2, …ai , …, an−1} of length n (n ≤ 100000). Please help him to manage this price sequence. There are three types of operations: • BUY x: buy a new book with price x, thus x is added at the end of a. • CLOSEST ADJ PRICE: output the minimum absolute difference between adjacent prices. • CLOSEST PRICE: output the absolute difference between the two closest prices in the entire sequence. A total of m operations are performed (1 ≤ m ≤ 100000). Each operation is one of the three mentioned types. You need to write a program to perform given operations. For operations ”CLOSEST ADJ PRICE” and ”CLOSEST PRICE” you need to output the corresponding answers. 2.2 Input The first line contains two integers n and m, representing the length of the original sequence and the number of operations. The second line consists of n integers, representing the initial sequence a. Following that are m lines, each containing one operation: either BUY x, CLOSEST ADJ PRICE, or CLOSEST PRICE (without extra spaces or empty lines). 2.3 Output For each CLOSEST ADJ PRICE and CLOSEST PRICE command, output one line as the answer. Sample Input 1 3 4 7 1 9 CLOSEST_ADJ_PRICE BUY 2 CLOSEST_PRICE CLOSEST_ADJ_PRICE Sample Output 1 6 1 6 Sample Input 2 6 12 30 50 39 25 12 19 BUY 4 CLOSEST_PRICE BUY 14 CLOSEST_ADJ_PRICE CLOSEST_PRICE BUY 0 CLOSEST_PRICE BUY 30 BUY 12 CLOSEST_PRICE BUY 20 CLOSEST_PRICE Sample Output 2 5 7 2 2 0 0 写一个python程序,可以处理上述问题在大数据的情况下
- Assistant
下面是一个处理上述问题的Python程序,它可以处理大数据情况下的操作:
import sys from heapq import * class PriceManager: def __init__(self, prices): self.prices = prices self.adj_price_diff = sys.maxsize self.closest_price_diff = sys.maxsize self.heap = [] def buy_book(self, x): self.prices.append(x) self.adj_price_diff = min(self.adj_price_diff, abs(x - self.prices[-2])) self.closest_price_diff = min(self.closest_price_diff, self.__find_closest_price_diff(x)) def closest_adj_price(self): return self.adj_price_diff def closest_price(self): return self.closest_price_diff def __find_closest_price_diff(self, x): if not self.heap: return sys.maxsize _, closest = self.heap[0] return abs(x - closest) def process_operations(self, operations): for operation in operations: if operation[0] == 'BUY': self.buy_book(int(operation[1])) elif operation[0] == 'CLOSEST_ADJ_PRICE': print(self.closest_adj_price()) elif operation[0] == 'CLOSEST_PRICE': print(self.closest_price()) def __heap_push(self, x): heappush(self.heap, (x, x)) def __heap_remove(self, x): self.heap.remove((x, x)) heapify(self.heap) def __heap_pop(self): return heappop(self.heap)[1] n, m = map(int, input().split()) prices = list(map(int, input().split())) price_manager = PriceManager(prices) operations = [] for _ in range(m): operations.append(input().split()) price_manager.process_operations(operations)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.