AdvancedRating 1900
Least Frequently Used Cache
A cache holds at most c keys. 'P k v' stores v for key k; 'G k' prints the value or -1. When storing a new key into a full cache, first remove the key used the fewest times (gets and puts both count); among ties remove the least recently used.
Input
c and q; then q commands.
Output
One line per G.
Sample 1
Input
2 10 P 1 1 P 2 2 G 1 P 3 3 G 2 G 3 P 4 4 G 1 G 3 G 4
Output
1 -1 3 -1 3 4
Sample 2
Input
2 35 G 2 P 4 76 P 3 66 P 4 34 P 6 72 G 5 P 6 29 G 7 G 4 G 4 P 4 62 P 2 67 P 1 72 P 8 58 P 6 13 P 2 46 P 5 4 P 5 29 P 2 19 P 8 76 P 3 71 P 8 43 P 7 86 P 2 64 P 2 63 G 6 G 7 G 6 G 6 P 1 15 G 4 G 3 P 3 25 G 2 G 1
Output
-1 -1 -1 34 34 -1 -1 -1 -1 62 -1 -1 -1
Output appears here