Nội dung Bài tập
- Mã:
- Caching
- Tên:
- Caching
- Dạng thi:
- oi
- Thang điểm:
- 10 điểm
- Giới hạn thời gian:
- 1 giây
- Giới hạn bộ nhớ:
- 64 MB
- Được tạo bởi:
- admin
The infamous problem of caching! John hasn’t notice it when his operating system decide which memory page to swap out, or which file to keep in the SSD storage and which go to the larger but slower HDD. But fate has left John in charge of his startup company brand new distributed contain delivery network. In this system data is never where you need it, and fetching data over a network takes time and consumes bandwidth.
The problem can be mitigated by adding a cache, where a node stores some resources locally and if those resources need to be used again, it can simply take them from its cache rather than asking someone else for them. However, caches have a nasty tendency to fill up, so at some point, objects must be evicted from the cache to make room for new objects. Choosing what object to remove from the cache is not easy and there are several different algorithms to choose from.
Lucky for John, his startup is dealing with fortune-tellers and they can see into the future, knowing exactly what objects will be accessed and in what order. Using this information John can make optimal decisions on what objects to remove from the cache. Optimality here means that he will have to minimize the number of times an object is read into the cache.
All object accesses go through the cache, so every time an object is accessed, it must be inserted into the cache if it was not already there. All objects are of equal size, and no writes occur in the system, so a cached object is always valid. When the system starts, the cache is empty.
But can John do that? You have been tasked with evaluating John’s algorithm
Input
Input include many test cases, each of which share the following structure:
The first line of input contains three integers, separated by single spaces, telling you how many objects fit in the cache, 0 < c ≤ 103, how many different objects are in the system, c ≤ n ≤ 105, and how many accesses, 0 ≤ a ≤ 105, will occur.
The following a lines contain a single integer between 0 and n − 1 (inclusive) indicating what object is accessed. The first line corresponds to the first object accessed access and the last line to the last.
Output
Output the least number of times an object must be read into the cache to handle the accesses listed in each test case, follow by new line character.
The problem can be mitigated by adding a cache, where a node stores some resources locally and if those resources need to be used again, it can simply take them from its cache rather than asking someone else for them. However, caches have a nasty tendency to fill up, so at some point, objects must be evicted from the cache to make room for new objects. Choosing what object to remove from the cache is not easy and there are several different algorithms to choose from.
Lucky for John, his startup is dealing with fortune-tellers and they can see into the future, knowing exactly what objects will be accessed and in what order. Using this information John can make optimal decisions on what objects to remove from the cache. Optimality here means that he will have to minimize the number of times an object is read into the cache.
All object accesses go through the cache, so every time an object is accessed, it must be inserted into the cache if it was not already there. All objects are of equal size, and no writes occur in the system, so a cached object is always valid. When the system starts, the cache is empty.
But can John do that? You have been tasked with evaluating John’s algorithm
Input
Input include many test cases, each of which share the following structure:
The first line of input contains three integers, separated by single spaces, telling you how many objects fit in the cache, 0 < c ≤ 103, how many different objects are in the system, c ≤ n ≤ 105, and how many accesses, 0 ≤ a ≤ 105, will occur.
The following a lines contain a single integer between 0 and n − 1 (inclusive) indicating what object is accessed. The first line corresponds to the first object accessed access and the last line to the last.
Output
Output the least number of times an object must be read into the cache to handle the accesses listed in each test case, follow by new line character.
Theme :
Mời bạn soạn code
Ai có thể xem bài này :
Thông tin
Phần thảo luận