This page is still under construction.

Parts of this page are still being built. What you see may change.

Toy Cars

Time limit3sMemory limit128 MB

Summary
Belady's caching: given a sequence of toy cars a child will request, minimize the number of times a car must be fetched from the shelf when at most k can be on the floor.
Level

Medium7 of 10

Topics
Greedy, Heap, Simulation
Solved
No attempts yet

Problem

Johnny is a little boy, only three years old, and he loves playing with toy cars. He owns nn different cars, all kept on a shelf so high that he cannot reach them by himself. His room is small, so at no moment may there be more than kk toy cars on the floor.

Johnny plays with one car from the floor at a time. His mother stays in the room with him the whole time. When Johnny wants another car that is already on the floor, he reaches it himself. But when the car he wants is on the shelf, his mother has to hand it to him. Whenever she gives him a car, she may at the same time pick any one car from the floor and put it back on the shelf, so that there is always enough room on the floor.

His mother knows him so well that she can perfectly predict which cars Johnny will want to play with, and in what order. Using this knowledge, she wants to minimize the number of times she has to hand a car down from the shelf. To do so she must choose very carefully which car to put back on the shelf each time.

Write a program that reads the sequence of cars Johnny will want to play with, in order, and computes the minimal number of times his mother has to take a car from the shelf.

Input

The first line contains three integers nn, kk, pp (1≤k≤n≤100,0001 \le k \le n \le 100{,}000, 1≤p≤500,0001 \le p \le 500{,}000), separated by single spaces: the total number of cars, the maximum number of cars that may be on the floor at once, and the length of the sequence of cars Johnny will want to play with. Each of the next pp lines contains one integer — the number of a car Johnny will want to play with (the cars are numbered from 11 to nn).

Output

Print a single integer: the minimal number of times his mother has to take a car from the shelf.

Examples3

  1. Example 1

    Input
    3 2 7
    1
    2
    3
    1
    3
    1
    2
    
    Expected output
    4
    
  2. Example 2

    Input
    3 2 5
    1
    2
    1
    3
    2
    
    Expected output
    3
    
  3. Example 3

    Input
    5 2 5
    1
    2
    3
    4
    5
    
    Expected output
    5