← 返回 optiver 的题目列表Order Book Transactions
类型:online_judge
Market participants submit 'buy' and 'sell' orders at various prices, forming an order book. When a new 'buy' order at a price p is submitted, one of two scenarios can occur:
There exists a 'sell' order in the order book at a price lower or equal to p. In this case, a transaction occurs at a price of that 'sell' order, and both orders are removed from the order book. Among multiple suitable 'sell' orders, the one with the better (lower) price takes priority.
There is no 'sell' order at a lower or equal price to p, so the 'buy' order gets added to the order book.
Similarly, for a 'sell' order, if there is a suitable 'buy' order, it is executed at the highest viable price, otherwise added to the order book.
Orders are encoded as a 2-element integer array. The first element is +1 or -1 indicating a buy or sell order, respectively. The second element represents the price.
Given a sequence of orders, return the sum of prices across all transactions that occurred.
Example
Input
[[1, 100], [-1, 90], [1, 110], [-1, 100]]