TopCoder problem "StockQuotes" used in SRM 257 (Division I Level Two)



Problem Statement

    A 'quote' is a message from a stock exchange that says at what price that exchange is currently willing to buy a given stock (the 'bid') and what price it is willing to sell the stock (the 'ask'). The 'spread' is defined as the difference between the ask and the bid. For our purposes, you can assume that the ask will always be greater than the bid and that the bid will be greater than zero. The 'inside quote' is defined as the highest bid from any exchange and the lowest ask from any exchange (they do not necessarily need to be the same exchange). The inside quote changes when any exchange improves upon the current inside quote or when the best exchange moves away from the inside exposing a lower bid or higher ask.



You will be given a String[] representing a number of quotes, in the order they are made. Each element of the input will represent a single quote and will be formatted as: "EXCHANGE BID ASK", where EXCHANGE is the index of the stock exchange making the quote and BID and ASK are as defined above. You are to issue a report based on the quotes. Each element of the report should be formatted as "EXCHANGE COUNT AVERAGE_SPREAD", where EXCHANGE is either the index of an EXCHANGE, as in the input, or 10, representing the inside quote. COUNT is the number of times that the quote (either bid or ask) for that exchange changed (or that the inside quote changed). AVERAGE_SPREAD is the average spread, averaged only over the quotes that caused the quote to change and with exactly 2 digits after the decimal point (rounded in the standard way to the nearest hundredth where .005 rounds up). The return should contain one entry for each exchange that issues one or more quotes in the input. It should be sorted by the index of the exchanges, with the entry for the inside spread coming last.
 

Definition

    
Class:StockQuotes
Method:report
Parameters:String[]
Returns:String[]
Method signature:String[] report(String[] quotes)
(be sure your method is public)
    
 

Constraints

-Each element of quotes will be formatted as "EXCHANGE BID ASK" where EXCHANGE is a digit between '0' and '9', inclusive, and BID and ASK are integers between 1 and 1000, inclusive, without leading zeros.
-quotes will contain between 1 and 50 elements inclusive.
-An exchange will never issue a quote that is the same is its existing quote (so the number of times that its quote changes will be identical to the number of quotes it issues).
-ASK will be greater than BID in each element of quote.
-The inside spread will always be greater than 0.
 

Examples

0)
    
{"0 10 14",
 "1 9 16",
 "2 11 15",
 "0 11 13",
 "1 10 15",
 "2 12 14",
 "0 9 15",
 "2 8 20"}
Returns: {"0 3 4.00", "1 2 6.00", "2 3 6.00", "10 6 2.83" }
The following table shows how the best bid and ask change as new bids come in:
              |     BEST
Exch|Bid |Ask | Bid|Ask | Changed
----+----+----+----+----+------
 0  | 10 | 14 | 10 | 14 | Yes
 1  |  9 | 16 | 10 | 14 | 
 2  | 11 | 15 | 11 | 14 | Yes
 0  | 11 | 13 | 11 | 13 | Yes
 1  | 10 | 15 | 11 | 13 |  
 2  | 12 | 14 | 12 | 13 | Yes
 0  |  9 | 15 | 12 | 14 | Yes
 2  |  8 | 20 | 10 | 15 | Yes
1)
    
{"8 931 944",
 "8 926 946",
 "8 926 951",
 "8 928 953",
 "8 929 954"}
Returns: {"8 5 21.60", "10 5 21.60" }
2)
    
{"2 711 730",
 "5 716 729",
 "7 711 734",
 "0 718 731",
 "5 713 731",
 "1 713 730"}
Returns: {"0 1 13.00", "1 1 17.00", "2 1 19.00", "5 2 15.50", "7 1 23.00", "10 4 13.75" }

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=4706

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=8005&pm=4706

Writer:

lars2520

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Simple Math