Requests
Time limit1sMemory limit128 MB
Given a cache of capacity K and N timed requests with expiration times, compute the minimum number of fetches over all offline replacement strategies.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Intervals, Sorting
- Solved
- No attempts yet
Problem
You are given a collection of equal-size objects, a cache that can hold at most objects, and a sequence of requests. Each request asks for one object and carries an expiration time telling you until when (inclusive) that object stays valid.
Time starts at and advances by one unit after every request, so the -th request happens at time .
A request for an object that is already in the cache and has not yet expired is served at no cost. If the requested object is not in the cache, or it is in the cache but has expired, it must be fetched into the cache at a cost of one unit. Updating only the expiration time of an object that is still valid costs nothing.
Whenever an object must be fetched into a cache that is already full, a replacement algorithm chooses one currently cached object to evict so the new one fits. The cache starts empty.
All requests are known in advance. Among all replacement strategies, find the one with the least total cost, and output that minimum cost.
Input
The first line contains an integer , the cache capacity in number of objects ().
The second line contains an integer , the number of requests ().
Each of the next lines contains two integers and : is the requested object, and is its expiration time (an absolute time, inclusive).
The requests are given in order; the first request occurs at time , and the absolute time advances by one unit after each request.
Output
Output a single integer: the minimum total cost, i.e. the number of fetches performed by an optimal replacement algorithm.