Investment Investigation

시간 제한4초메모리 제한1024 MB

요약
일반 주문과 전량체결주문(FoK)을 처리하는 매칭 엔진을 시뮬레이션하고, 체결된 모든 거래의 주문 번호와 수량을 출력한다.
난이도

보통10점 중 7점

유형
시뮬레이션, 힙, 그리디, 정렬
정답자
아직 제출이 없습니다

문제

To make some extra money on the side, you have recently started running your own cryptocurrency exchange, where people can trade their Budget Amplifying Profit Coin (BAPC). It is quickly gaining popularity, however, this has also resulted in government regulators asking some questions... As part of their investigation, they have asked for a list of all transactions that have been made via your exchange. You have never bothered to keep track of this, but luckily, you still have the list of all orders that were made since the start of the exchange.

The exchange operates by keeping a list of outstanding buy and sell orders, each with a price and an amount. Whenever a normal order comes in, it is checked whether the new lowest sell price is less than or equal to the highest buy price. If this is the case, a transaction is made between the sell order with the lowest price and the buy order with the highest price, such that at least one of these orders is completely fulfilled. In case of a tie in price, older orders are fulfilled first. This is repeated until the lowest sell price is strictly larger than the highest buy price.

If instead a Fill-or-Kill (FoK) buy order comes in, there must currently be enough outstanding sell orders with a price of at most the offered price to completely fulfil this order. If there are, the order will be fulfilled in the same way as a normal order. Otherwise, the order is completely cancelled, without any transaction taking place. Note that multiple orders may be used to complete a FoK order, as long as it happens immediately.

FoK sell orders are processed in a similar way, but then there should be sufficient outstanding buy orders with a price of at least the asked price.

As an example, consider the first sample case. The six orders are handled as follows:

  1. The first order is added to the list of outstanding orders.
  2. The second order is partially fulfilled by selling 1010 BAPC to the first order. This removes the first order from the list of outstanding orders, and adds the remainder of the second order (consisting of 1010 BAPC) to this list.
  3. The third order is added to the list of outstanding orders.
  4. The fourth order is a FoK buy order that cannot be immediately fulfilled, so it is ignored. It is not added to the list of outstanding orders.
  5. The fifth order can be immediately fulfilled by first buying 1010 BAPC from order 22 and then buying 5050 BAPC from order 33. The resulting list of outstanding orders only consists of the remaining 88 BAPC of order 33.
  6. The sixth order is added to the list of outstanding orders.

Given a list of all orders in the order that they have been made, create a list of all transactions that have been performed by your exchange.

입력

The input consists of:

  • One line with an integer nn (1≤n≤1051 \leq n \leq 10^5), the number of orders.

  • nn lines, each describing an order:

    • A string ss, either "buy" or "sell", the side of the order.
    • A string tt, either "normal" or "fok", the type of the order.
    • An integer pp (1≤p≤1091 \leq p \leq 10^9), the offered or asked price per BAPC.
    • An integer aa (1≤a≤1091 \leq a \leq 10^9), the amount of BAPC being asked or offered.

출력

The output consists of the number of performed transactions, and then for each transaction, in the order that they have been performed:

  • The index of the corresponding "sell" order.
  • The index of the corresponding "buy" order.
  • The amount of BAPC being traded.

Here, the index of an order is its position in the input, where the first order has index 11.

예제2

  1. 예제 1

    입력
    6
    buy normal 700 10
    sell normal 500 20
    sell normal 800 58
    buy fok 600 30
    buy fok 900 60
    sell normal 300 42
    
    예상 출력
    3
    2 1 10
    2 5 10
    3 5 50
    
  2. 예제 2

    입력
    3
    buy normal 19 10
    buy normal 19 20
    sell fok 19 17
    
    예상 출력
    2
    3 1 10
    3 2 7