problems / 144

LFU Cache Audit

medium linked-list hashing simulation 1500 ms · 256 MB

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 key k is 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 key k is in the cache, its value becomes v and its use count increases by 1. Otherwise a new entry with value v and use count 1 is inserted; if the cache already held C entries, 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).

Example 1

Input

8 2
PUT a 5
PUT b 7
GET a
PUT c 9
GET b
GET c
PUT d 1
GET d

Output

3 1 15
2
b a

Example 2

Input

10 2
GET q
PUT x 1
PUT x 4
PUT y 2
PUT z 3
GET x
GET y
GET z
PUT y 6
GET x

Output

3 2 11
2
y z

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

Hint 1

Scanning every cached entry for the eviction victim is too slow with 5·10^5 operations and a large cache. What can you keep so that the entry to evict is always cheap to find?

Hint 2

Group the cached keys by their use count, and inside each group order them by recency of use. Think about how one use moves a key from one group to the next, and which entry sits at the front of the smallest non-empty group when an eviction happens.

Hint 3

A use always raises a count by exactly one, so the smallest count only changes in one specific way. Evict before inserting a new key, since an evicted key forgets its count. The sum of hit values can exceed 32 bits.

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

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