LFU Cache Audit
A feature-flag service keeps flag values in a small in-process key-value cache. The team is evaluating a frequency-based eviction policy and wants to replay a recorded trace of operations against it before shipping.
The cache holds at most C entries and starts empty. Every entry has a key, an integer value, and
a use count. The trace is processed in order; each operation is one of:
GET k: if keykis in the cache, this is a hit: the cache returns the entry's value and the entry's use count increases by 1. Otherwise it is a miss and the cache is not changed (nothing is inserted).PUT k v: if keykis in the cache, its value becomesvand its use count increases by 1. Otherwise a new entry with valuevand use count 1 is inserted; if the cache already heldCentries, one entry is evicted first to make room.
The victim of an eviction is the entry with the smallest use count. If several entries share that
smallest count, the victim is the one among them whose most recent use is the oldest. A use of an
entry is its insertion, a hit on it, or a PUT that updated it.
An evicted key keeps no history: if it is inserted again later, it starts over with use count 1.
Report the number of hits, the number of misses, the sum of the values returned by all hits, and the evicted keys in the order they were evicted.
Input
The first line contains two integers n and C (1 ≤ n ≤ 5·10^5, 1 ≤ C ≤ 5·10^5): the number of
operations and the cache capacity.
Each of the next n lines contains one operation, either GET k or PUT k v, where k is a key
of 1 to 8 characters, each a lowercase English letter or a digit, and v is an integer
(0 ≤ v ≤ 10^9). Keys are compared as strings, so 007 and 7 are different keys.
Output
On the first line print three integers: the number of hits, the number of misses, and the sum of the values returned by all hits.
On the second line print E, the number of evictions.
On the third line print the E evicted keys separated by spaces, in the order they were evicted.
If E = 0, this line is empty.
Notes
In the first example the capacity is 2. After PUT a 5, PUT b 7 and GET a, entry a has use
count 2 and b has use count 1. PUT c 9 evicts b, the only entry with the smallest count. Then
GET b misses, and GET c hits, raising c to count 2. Now a and c are tied at count 2, and
a was used longer ago, so PUT d 1 evicts a. Finally GET d hits and returns 1. The hits
returned 5, 9 and 1, for a sum of 15.
In the second example PUT x 4 on an existing key updates the value and counts as a use, so x
reaches use count 2 and PUT z 3 evicts y, even though y was used more recently. Later
PUT y 6 inserts y afresh and evicts z (count 2) rather than x (count 3).