Sliding Log Rate Limiter

easy queue sliding-window simulation 1000 ms · 256 MB

Sliding Log Rate Limiter

An API gateway protects a backend with a sliding log rate limiter. It is configured with a limit k and a window length w milliseconds, and it keeps a log of the timestamps of the requests it has accepted.

Requests arrive one by one with integer millisecond timestamps that never decrease (several requests may share a timestamp). When a request arrives at time t, the gateway counts the accepted requests whose timestamps s satisfy t − w < s ≤ t:

  • if fewer than k accepted requests are in that window, the request is accepted and t is added to the log;
  • otherwise the request is rejected. Rejected requests are not logged and never count against later requests.

Requests are processed in input order, so a request accepted at time t counts for later requests that share the same timestamp t.

Report the decision for every request.

Input

The first line contains three integers n, k and w (1 ≤ n ≤ 10^6, 1 ≤ k ≤ 10^6, 1 ≤ w ≤ 10^9): the number of requests, the limit and the window length in milliseconds.

The second line contains n integers t_1, t_2, …, t_n (0 ≤ t_1 ≤ t_2 ≤ … ≤ t_n ≤ 10^9): the arrival times of the requests in milliseconds.

Output

Print n lines. The i-th line must be ACCEPT if the i-th request is accepted and REJECT otherwise.

Notes

In the first example k = 2 and w = 1000. The requests at 0 and 300 are accepted. At 999 the window (−1, 999] already holds two accepted requests, so it is rejected. At 1000 the window is (0, 1000]: the request at 0 has left it, only the one at 300 remains, so it is accepted. At 1300 the window (300, 1300] contains just the request at 1000 (the one at 300 is exactly w old and no longer counts), so it is accepted. At 1500 the window holds 1000 and 1300, so it is rejected.

In the second example all requests arrive at the same millisecond: the first three are accepted and the rest are rejected.

Example 1

Input

6 2 1000
0 300 999 1000 1300 1500

Output

ACCEPT
ACCEPT
REJECT
ACCEPT
ACCEPT
REJECT

Example 2

Input

5 3 10
7 7 7 7 7

Output

ACCEPT
ACCEPT
ACCEPT
REJECT
REJECT

Hints · reveal one at a time if you're stuck

Hint 1

Only accepted requests are stored, and the times never decrease. Think about which stored timestamps can still matter for a later request: once one has left the window, will it ever come back?

Hint 2

Keep the accepted times in a queue in arrival order. Before each decision, remove the old times from the front that are too far back, then accept the request only if fewer than k times remain. Check exactly how the window boundary treats a timestamp that is exactly w old.

report an issue Prefer your own editor? sooks prep 50 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.