Under construction

Still being built. Things may change

Easy Trading

Interview

Time limit2sMemory limit128 MB

Summary
Given daily prices, compute moving averages over the last m and n days and print a buy or sell signal whenever the trend between them reverses.
Level

Easy3 of 10

Topics
Array, Prefix sum, Simulation, Implementation
Solved
No attempts yet

Problem

Frank is a professional stock trader at Advanced Commercial Markets Limited (ACM Ltd). He likes "easy trading" — using a simple strategy to decide when to buy stock and when to sell it.

Frank keeps a database of historical daily stock prices. His strategy has two integer parameters mm and nn (1≤m<n≤1001 \le m < n \le 100). Every day he computes two values: P(m)P(m), the average stock price over the previous mm days, and P(n)P(n), the average stock price over the previous nn days. P(m)>P(n)P(m) > P(n) signals an upward (bullish) trend, and P(m)<P(n)P(m) < P(n) signals a downward (bearish) trend. In practice P(m)P(m) and P(n)P(n) are never equal.

When the trend reverses from bearish to bullish, it is a signal for Frank to buy. When it reverses from bullish to bearish, it is a signal to sell.

Frank has several candidate pairs of mm and nn in mind and wants to backtest them on historical prices. He takes kk historical prices pip_i (n<k≤10 000n < k \le 10\,000; 0<pi<1000 < p_i < 100 for 1≤i≤k1 \le i \le k). For each day ii (n≤i≤kn \le i \le k) he computes Pi(m)P_i(m) and Pi(n)P_i(n), the arithmetic averages of pi−m+1…pip_{i-m+1} \dots p_i and pi−n+1…pip_{i-n+1} \dots p_i respectively.

Backtesting generates signals by these rules:

  • If Pi(m)>Pi(n)P_i(m) > P_i(n), day ii has a bullish trend, and a BUY ON DAY i signal is generated when i=ni = n or day i−1i-1 was bearish.
  • If Pi(m)<Pi(n)P_i(m) < P_i(n), day ii has a bearish trend, and a SELL ON DAY i signal is generated when i=ni = n or day i−1i-1 was bullish.

Write a program that backtests a given strategy: print the signal for the first tested day (day nn), followed by the signals for the remaining days in increasing order of day number.

Input

The first line contains three integers mm, nn, and kk. The next kk lines contain the stock prices for days 11 to kk. Each price pip_i has exactly two digits after the decimal point. The prices are guaranteed to satisfy Pi(m)≠Pi(n)P_i(m) \ne P_i(n) for all ii (n≤i≤kn \le i \le k).

Output

Print the list of signals, one per line, as described above.

Examples1

  1. Example 1

    Input
    3 5 17
    8.45
    9.10
    9.40
    10.15
    10.40
    11.08
    11.52
    12.12
    12.51
    12.15
    11.90
    11.25
    11.73
    10.77
    10.80
    10.01
    9.14
    
    Expected output
    BUY ON DAY 5
    SELL ON DAY 12