luogu#P6981. [NEERC2015] Iceberg Orders
[NEERC2015] Iceberg Orders
题目描述
You are working for Metagonia stock exchange. Recently traders in Metagonia had heard about Iceberg orders traded on London stock exchange and asked your employer to add such functionality as well. A stock exchange is an engine that receives orders and generates trades.
An iceberg order is a quintuple of integers (ID, , TV). Each order has an identifier ID (unique among all orders), type (which is equal to either BUY or SELL , price , total remaining volume and tip volume TV. For each order, exchange additionally keeps track of its current volume CV and priority PR. There is also a global priority counter of the exchange GP. An order book of the exchange is a set of orders.
Trades that are generated by the exchange are quadruples of integers (BUY ID, SELL ID, . Each trade has BUY ID and SELL ID -- identifiers of matching BUY and SELL orders, trade price , and trade volume .
When an order is received by the exchange it is matched against orders currently on the order book. This is done as follows. Suppose an order a is received with SELL. Among all orders currently on the order book we look for an order such that BUY and We select such an order with the largest price, and if there are several -- one with the smallest priority. If there is such an order , then a trade is generated with BUY and SELL at trade price with trade volume and are all decreased by trade volume. If after this, then the order is removed from the order book. If (but then we set current volume of order to set GP, and increment GP. We continue these operations of selecting and generating trades until either or there are no more orders on the order book which satisfy the condition. In the latter case, we add order a to the order book with and GP, and then increment GP. When the process of matching the order a is finished with several trades between the same pair of orders a and (and there can be lots of them!), they are all united into a single trade with the volume equal to the sum of individual trade volumes.
If BUY we are looking for an order with SELL and and select such an order with the smallest price and the smallest priority among those. The rest of the matching process is as described above, with trades having BUY SELL and
Initially the order book is empty. You are presented with several orders that are received by the exchange one by one. You need to print generated trades and the order book state after all orders are processed.
Hint: The priority and GP are introduced in the problem statement only for the purpose of a formal description of the algorithm. The actual implementation does not have to keep track of priority. Typically, an exchange simply keeps a priority-ordered list of orders of each type at each price in its order book.
输入格式
The first line of the input contains the number of orders . Each of the following lines represent an order. Each order is given by a space-separated quintuple ID TV , where ID for BUY and for SELL, and TV .
输出格式
For each order print all trades generated by processing this order, in ascending order of pairs (BUY ID, SELL ID), each trade on its own line. Each trade shall be printed as a space-separated quadruple of integers BUY ID SELL ID . It is guaranteed that the total number of trades would not exceed . Print a blank line after all trades, followed by the order book. Each order that is still on the book shall be printed as a space-separated sextuple ID TV CV, sorted first by and then by PR.
7
42 1 100 200 20
239 1 100 50 50
1111 1 101 30 15
1234 1 100 300 15
4321 2 99 125 25
5678 1 101 30 30
8765 2 101 100 20
42 4321 100 30
239 4321 100 50
1111 4321 101 30
1234 4321 100 15
5678 8765 101 30
42 1 100 170 20 10
1234 1 100 285 15 15
8765 2 101 70 20 20
提示
Time limit: 1 s, Memory limit: 256 MB.