-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRU.java
84 lines (75 loc) · 2.11 KB
/
LRU.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
* Created by Jarvis on 1/16/2017.
*/
class LRU {
static int A[]=new int[1000];
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine()),s,n;
String[]s1;
while (t-->0){
s1=br.readLine().split("\\s");
n=Integer.parseInt(s1[0]);
s=Integer.parseInt(s1[1]);
s1=br.readLine().split("\\s");
for (int i = 0; i < n; i++) {
A[i]=Integer.parseInt(s1[i]);
}
lru(n,s,A,new int[s]);
}
}
static void lru(int N, int S, int[] A, int cached_pages[]){
boolean hash[]=new boolean[102];
int cnt[]=new int[102];
for (int i = 0; i < S && i<N; i++) {
hash[A[i]]=true;
cnt[A[i]]++;
}
if (N>S){
for (int i = S; i < N; i++) {
if (hash[A[i]])cnt[A[i]]++;
else {
int x=getMinValue(cnt);
hash[x]=false;
hash[A[i]]=true;
cnt[A[i]]++;
}
}
int k=0;
for (int i = 0; i <= 100; i++) {
if (hash[i]){
cached_pages[k]=i;
k++;
}
}
}
else {
for (int i = 0; i < N; i++) {
cached_pages[i]=A[i];
}
}
Arrays.sort(cached_pages);
for (int i = 0; i < S; i++) {
System.out.print(cached_pages[i]+" ");
}
System.out.println();
}
private static int getMinValue(int[] cnt) {
int x=10000;
int min=0;
for (int i = 0; i <101; i++) {
if (cnt[i]>0){
if (cnt[i]<x) {
min = i;
x=cnt[i];
}
}
}
return min;
}
}