codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

572B.md (2518B)


      1 Source: https://codeforces.com/problemset/problem/572/B
      2 
      3 B. Order Book
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 In this task you need to process a set of stock exchange orders and use them to create order book.
     22 
     23 An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number *i* has price *p**i*, direction *d**i* — buy or sell, and integer *q**i*. This means that the participant is ready to buy or sell *q**i* stocks at price *p**i* for one stock. A value *q**i* is also known as a volume of an order.
     24 
     25 All orders with the same price *p* and direction *d* are merged into one aggregated order with price *p* and direction *d*. The volume of such order is a sum of volumes of the initial orders.
     26 
     27 An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order.
     28 
     29 An order book of depth *s* contains *s* best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than *s* aggregated orders for some direction then all of them will be in the final order book.
     30 
     31 You are given *n* stock exhange orders. Your task is to print order book of depth *s* for these orders.
     32 
     33 Input
     34 
     35 The input starts with two positive integers *n* and *s* (1 ≤ *n* ≤ 1000, 1 ≤ *s* ≤ 50), the number of orders and the book depth.
     36 
     37 Next *n* lines contains a letter *d**i* (either 'B' or 'S'), an integer *p**i* (0 ≤ *p**i* ≤ 105) and an integer *q**i* (1 ≤ *q**i* ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order.
     38 
     39 Output
     40 
     41 Print no more than 2*s* lines with aggregated orders from order book of depth *s*. The output format for orders should be the same as in input.
     42 
     43 Examples
     44 
     45 Input
     46 
     47 ```
     48 6 2  
     49 B 10 3  
     50 S 50 2  
     51 S 40 1  
     52 S 50 6  
     53 B 20 4  
     54 B 25 10
     55 ```
     56 
     57 Output
     58 
     59 ```
     60 S 50 8  
     61 S 40 1  
     62 B 25 10  
     63 B 20 4
     64 ```
     65 
     66 Note
     67 
     68 Denote (x, y) an order with price *x* and volume *y*. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample.
     69 
     70 You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.