cswork / OA打开练习工作台

JPMorgan ChaseOA #5

Calculate Net Profit

估计中等可在线判题

考点(AI 标注)模拟/实现哈希计数

BUY and SELL trade at the current market price, CHANGE is a change in price, and QUERY asks for the net profit and loss from the start until now, including the unrealized profit and loss on holdings not yet sold; SELL is guaranteed to have enough holdings. The original example's output is a placeholder; the correct query results are 120 and 200.

输入格式

The input is a JSON array of strings, where each item is an event: BUY stock quantity, SELL stock quantity, CHANGE stock delta, or QUERY. Original bounds: 1≤number of events≤100000, and each event has ≤21 characters (this site counts Unicode code points); quantities are 1..1000, and |delta|≤1000. A stock name is a single non-empty token without whitespace, preserving Unicode and the non-whitespace control characters represented by JSON escapes; event fields are separated by a single space.

输出格式

The first line is the number of queries, followed by one net profit and loss per line; if there is no QUERY, output only 0.

样例 1

输入:

["BUY googl 20", "BUY aapl 50", "CHANGE googl 6", "QUERY", "SELL aapl 10", "CHANGE aapl 2", "QUERY"]

输出:

2
120
200

样例 2

输入:

["QUERY", "BUY A 2", "CHANGE A -3", "QUERY"]

输出:

2
0
-6

样例 3

输入:

["BUY A 1", "SELL A 1"]

输出:

0

样例说明

The outputs of the three public examples are, in order: 2 / 120 / 200; 2 / 0 / -6; 0. These answers have been checked by independent enumeration or direct simulation.