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 bisect import insort def closest_adj_price(prices): min_diff = float('inf') for i in range(1, len(prices)): diff = abs(prices[i] - prices[i-1]) min_diff = min(min_diff, diff) return min_diff def closest_price(prices): prices.sort() min_diff = float('inf') for i in range(1, len(prices)): diff = abs(prices[i] - prices[i-1]) min_diff = min(min_diff, diff) return min_diff def manage_price_sequence(n, m, initial_seq, operations): prices = initial_seq[:] output = [] for operation in operations: if operation.startswith('BUY'): _, x = operation.split() x = int(x) insort(prices, x) elif operation == 'CLOSEST_ADJ_PRICE': min_diff = closest_adj_price(prices) output.append(min_diff) elif operation == 'CLOSEST_PRICE': min_diff = closest_price(prices) output.append(min_diff) return output if __name__ == '__main__': n, m = map(int, sys.stdin.readline().split()) initial_seq = list(map(int, sys.stdin.readline().split())) operations = [sys.stdin.readline().strip() for _ in range(m)] output = manage_price_sequence(n, m, initial_seq, operations) for val in output: print(val) - User
不使用bisect解决该问题
- Assistant
以下是不使用bisect解决该问题的Python程序:
import sys def closest_adj_price(prices): min_diff = float('inf') for i in range(1, len(prices)): diff = abs(prices[i] - prices[i-1]) min_diff = min(min_diff, diff) return min_diff def closest_price(prices): min_diff = float('inf') for i in range(len(prices)): for j in range(i+1, len(prices)): diff = abs(prices[i] - prices[j]) min_diff = min(min_diff, diff) return min_diff def manage_price_sequence(n, m, initial_seq, operations): prices = initial_seq[:] output = [] for operation in operations: if operation.startswith('BUY'): _, x = operation.split() x = int(x) prices.append(x) elif operation == 'CLOSEST_ADJ_PRICE': min_diff = closest_adj_price(prices) output.append(min_diff) elif operation == 'CLOSEST_PRICE': min_diff = closest_price(prices) output.append(min_diff) return output if __name__ == '__main__': n, m = map(int, sys.stdin.readline().split()) initial_seq = list(map(int, sys.stdin.readline().split())) operations = [sys.stdin.readline().strip() for _ in range(m)] output = manage_price_sequence(n, m, initial_seq, operations) for val in output: print(val)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.