-
Notifications
You must be signed in to change notification settings - Fork 6
/
caching.py
35 lines (31 loc) · 946 Bytes
/
caching.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from heapq import *
from collections import deque
import sys
c, n, a = list(map(int, input().split()))
arr = []
times = {}
for line in sys.stdin:
arr.append(int(line))
for i in range(a):
if arr[i] not in times:
times[arr[i]] = deque([i])
else:
times[arr[i]].append(i)
used, ans = 0, 0
active = [False] * n
pq = [] # max PQ of times for the next read
for i in range(a):
if not active[arr[i]]:
active[arr[i]] = True
ans += 1
if used < c:
used += 1
else: # need to evict something
_, id_evict = heappop(pq) # pop the cache that is going to be met gain next latest
active[id_evict] = False
times[arr[i]].popleft() # this must be i but we need the next timings to compare in the PQ
if times[arr[i]]:
heappush(pq, (-times[arr[i]][0], arr[i]))
else:
heappush(pq, (-a, arr[i]))
print(ans)