#P4404. [JSOI2010] 缓存交换

    ID: 3332 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>贪心2010各省省选离散化江苏优先队列

[JSOI2010] 缓存交换

Description

In a computer, the CPU can exchange data directly only with the Cache. When the required main memory block is not in the Cache, the data must be brought in from main memory. If the Cache is already full at that time, one block must be removed first.

For example, suppose the Cache capacity is 33, and it currently holds main memory blocks numbered 1010 and 2020.
Now, the CPU accesses block 1010, which is a cache hit.

Next, the CPU accesses block 2121, so we just bring that block into the Cache, causing one cache miss.

Then, the CPU accesses block 3131, so one block must be evicted from the Cache before bringing block 3131 in. Suppose we evict block 1010.

After that, the CPU accesses block 1010 again, which causes another miss. We can see that if we had evicted a different block the previous time, we could have avoided this miss.

Modern computers often use the LRU (Least Recently Used) algorithm for Cache management—but as the example shows, it is not optimal.
Given an empty Cache with fixed capacity and a sequence of main memory access requests, Congcong wants to know which block to evict on each cache miss to minimize the total number of cache misses.

Input Format

The first line contains two integers NN and MM (1MN1051 \le M \le N \le 10^5), representing the number of main memory accesses and the capacity of the Cache.

The second line contains NN space-separated positive integers, giving the ID of each main memory block in access order (each does not exceed 10910^9).

Output Format

Output one line: the minimum possible number of cache misses.

6 2
1 2 3 1 2 3
4

Hint

On the 44-th miss, evict block number 33 from the Cache.

Translated by ChatGPT 5