Stock Exchange

Interview

Time limit1sMemory limit128 MB

Summary
For each issuer, for every bid output the agents on the opposite side whose price could match it, in input order.
Level

Medium4 of 10

Topics
Array, Implementation, Brute force, Sorting
Solved
No attempts yet

Problem

Stock exchanges around the world have been hit hard by the financial crisis. To protect their profits, they want to cut costs by automating their operations with new software.

Your task is to detect every pair of a buyer and a seller who are able to make a deal with each other.

Input

The input describes several issuers (stocks). Each issuer starts with a line containing an integer NN, a single space, and the issuer's code. The next NN lines each describe one bid.

A bid consists of an agent's name, a single space, the bid type in lowercase letters (either buy or sell), another single space, and a price given as a decimal number with exactly three digits after the decimal point. A buy bid gives the maximum price the agent is willing to pay; a sell bid gives the minimum price for which the agent is willing to sell.

  • Each issuer code consists of 1 to 10 uppercase letters (A–Z).
  • Each agent name has 1 to 20 characters and may contain both lowercase and uppercase letters.
  • Within a single issuer all agent names are distinct, but the same agent may post bids for different issuers.
  • 1≤N≤10001 \le N \le 1000.
  • No price is higher than 1000010000.

The input ends with a line containing the string 0 END.

Output

For each issuer, print its code on its own line. Then, for every bid of that issuer (in the same order as the input), print the agent's name, a colon (:), a single space, and the list of all agents who are potential counter-parties for that agent — one of the two wants to buy and the other wants to sell, and the sell price is less than or equal to the buy price.

Print the counter-party names separated by a single space, in the same order they appear in the input for that issuer. If there is no matching bid, print NO-ONE instead.

Examples1

  1. Example 1

    Input
    3 IBM
    OneBuyer buy 10.600
    TooExpensive sell 12.000
    ThisWillWork sell 10.600
    4 ACM
    one sell 129.999
    two buy 130.000
    three buy 131.000
    four sell 129.888
    4 CVUT
    seller sell 121.110
    toopoor buy 121.109
    sellertwo sell 121.111
    iamok buy 121.112
    0 END
    
    Expected output
    IBM
    OneBuyer: ThisWillWork
    TooExpensive: NO-ONE
    ThisWillWork: OneBuyer
    ACM
    one: two three
    two: one four
    three: one four
    four: two three
    CVUT
    seller: iamok
    toopoor: NO-ONE
    sellertwo: iamok
    iamok: seller sellertwo