-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLRU.cpp
More file actions
60 lines (60 loc) · 1.32 KB
/
LRU.cpp
File metadata and controls
60 lines (60 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//http://practice.geeksforgeeks.org/problems/page-faults-in-lru/0
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int arr[N];
int Hash[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(Hash,0,sizeof(Hash));
int sizze,n;
scanf("%d",&n);
for (int i = 1; i <=n; ++i)
scanf("%d",&arr[i]);
scanf("%d",&sizze); // cache size (Maximum Number of Pages that can be accommodated)
int pagefault=0;
set<int> s;
for (int i = 1; i <=n; ++i)
{
if (s.size() < sizze) // push until our cache size if Full !
{
if(Hash[arr[i]]==0)//Dont have this page in our cache
{
s.insert(i); // insert it in our cache
pagefault++;
}
else
{ //update the previous page
s.erase(s.find(Hash[arr[i]]));
s.insert(i);
}
Hash[arr[i]]=i; // mark the page with index
}
else
{
if (Hash[arr[i]])
{
//update the previous page
s.erase(s.find(Hash[arr[i]]));
s.insert(i);
Hash[arr[i]]=i;
}
else
{ //cant find this page hence pagefault
pagefault++;
int idx = *s.begin();//get the least recent used page as set.begin() will have least value !
s.erase(s.begin());
s.insert(i);
Hash[arr[idx]]=0; // remove that page
Hash[arr[i]]=i; //make new page
}
}
}
printf("%d\n",pagefault);
}
return 0;
}