using
System;
using
System.Collections;
using
System.Collections.Generic;
class
Test
{
static
int
pageFaults(
int
[]pages,
int
n,
int
capacity)
{
HashSet<
int
> s =
new
HashSet<
int
>(capacity);
Queue indexes =
new
Queue() ;
int
page_faults = 0;
for
(
int
i = 0; i < n; i++)
{
if
(s.Count < capacity)
{
if
(!s.Contains(pages[i]))
{
s.Add(pages[i]);
page_faults++;
indexes.Enqueue(pages[i]);
}
}
else
{
if
(!s.Contains(pages[i]))
{
int
val = (
int
)indexes.Peek();
indexes.Dequeue();
s.Remove(val);
s.Add(pages[i]);
indexes.Enqueue(pages[i]);
page_faults++;
}
}
}
return
page_faults;
}
public
static
void
Main(String []args)
{
int
[]pages = {7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2};
int
capacity = 4;
Console.Write(pageFaults(pages, pages.Length, capacity));
}
}