#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, T,P,VT , P , V , TV). Each order has an identifier ID (unique among all orders), type TT (which is equal to either BUY =1= 1 or SELL =2)= 2) , price PP , total remaining volume VV 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, P,V)P , V) . Each trade has BUY ID and SELL ID -- identifiers of matching BUY and SELL orders, trade price PP , and trade volume VV .

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 Ta=T_{a} = SELL. Among all orders currently on the order book we look for an order bb such that Tb=T_{b} = BUY and PbPa.P_{b} \ge P_{a}. We select such an order bb with the largest price, and if there are several -- one with the smallest priority. If there is such an order bb , then a trade tt is generated with BUY IDt=IDbID_{t} = ID_{b} and SELL IDt=IDaID_{t} = ID_{a} at trade price Pt=PbP_{t} = P_{b} with trade volume Vt=min(Va,CVb).Va,Vb,V_{t} = mi_n(V_{a}, CV_{b}). V_{a}, V_{b}, and CVbCV_{b} are all decreased by trade volume. If Vb=0V_{b} = 0 after this, then the order bb is removed from the order book. If CVb=0CV_{b} = 0 (but Vb>0)V_{b} > 0) then we set current volume of order bb to CVb=min(Vb,TVb),CV_{b} = mi_n(V_{b}, TV_{b}), set PRb=PR_{b} = GP, and increment GP. We continue these operations of selecting bb and generating trades until either Va=0V_{a} = 0 or there are no more orders bb on the order book which satisfy the condition. In the latter case, we add order a to the order book with CVa=min(Va,TVa)CV_{a} = mi_n(V_{a}, TV_{a}) and PRa=PR_{a} = 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 bb (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 Ta=T_{a} = BUY we are looking for an order bb with Tb=T_{b} = SELL and PbPaP_{b} \le P_{a} and select such an order bb with the smallest price and the smallest priority among those. The rest of the matching process is as described above, with trades having BUY IDt=IDa,ID_{t} = ID_{a}, SELL IDt=IDb,Pt=Pb,ID_{t} = ID_{b}, P_{t} = P_{b}, and Vt=min(Va,CVb).V_{t} = mi_n(V_{a}, CV_{b}).

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 n(1n50000)n (1 \le n \le 50 000) . Each of the following nn lines represent an order. Each order is given by a space-separated quintuple ID TPVT P V TV , where 11 \le ID 1000000,T=1 \le 1 000 000 , T = 1 for BUY and T=2T = 2 for SELL, 1P1000001 \le P \le 100 000 and 11 \le TV V1000000000 \le V \le 1 000 000 000 .

输出格式

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 PVP V . It is guaranteed that the total number of trades would not exceed 100000100 000 . 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 TPVT P V TV CV, sorted first by PP 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.