Offline prefetching

We are given (offline) a sequence of page request, one for every time unit. We have a cache of size k. If a page is in the cache it can be served, otherwise it must be fetched, and some page from the cache must be evicted to make room. This operation costs F time units, meaning that F stall time units are introduced. The idea is that pages can be fetched in advance, and in parallel to the fetch other pages might be served, reducing so the stall time. The constraint is that two fetches cannot overlap in time.
In this implementation we assume that the cache contains initially the first k distinct requested pages.

Input

Cache size
Fetch duration
Request sequence
output horizontally vertically

Output

request
|   cache at beginning of request
a : a b h d
b : a b h d
h : a b h d
d : a b _ d
    a b _ d
    a b _ d
    a b _ d
c : a b c d
b : a b c d
a : a b c d
b : _ b c d
d : _ b c d
d : _ b c d
    _ b c d
e : e b c _
c : e b c _
    e b c _
    e b c _
h : _ b c h
h : _ b c h
b : _ b c h
    _ b c h
g : g b c _
c : g b c _
    g b c _
    g b c _
d : g b c d
c : g b c d
g : g b c d
total cost (stall time) =  9