|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.StringTokenizer; |
| 8 | + |
| 9 | +public class Main { |
| 10 | + |
| 11 | + static int size,totalLen,count; |
| 12 | + static int[] arr; |
| 13 | + static HashSet<Integer> set = new HashSet<>(); |
| 14 | + |
| 15 | + public static void main(String[] args) throws IOException { |
| 16 | + init(); |
| 17 | + process(); |
| 18 | + print(); |
| 19 | + } |
| 20 | + |
| 21 | + private static void init() throws IOException{ |
| 22 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in));; |
| 23 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 24 | + //이거 그냥 lfd 라는 페이지 교체 알고리즘을 쓰면 된다! |
| 25 | + size = Integer.parseInt(st.nextToken()); |
| 26 | + totalLen = Integer.parseInt(st.nextToken()); |
| 27 | + arr = new int[totalLen]; |
| 28 | + |
| 29 | + st = new StringTokenizer(br.readLine()); |
| 30 | + for (int i = 0; i <totalLen; i++) { |
| 31 | + arr[i] = Integer.parseInt(st.nextToken()); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + private static void process() { |
| 38 | + count = 0; |
| 39 | + |
| 40 | + |
| 41 | + for (int i = 0; i < totalLen; i++) { |
| 42 | + if (set.size() < size) { |
| 43 | + set.add(arr[i]); |
| 44 | + continue; |
| 45 | + } |
| 46 | + if (set.contains(arr[i])) continue; |
| 47 | + |
| 48 | + int target = -1; |
| 49 | + int targetDistance = 0; |
| 50 | + for (int num: set) { |
| 51 | + int distance = 10000; |
| 52 | + for (int j = i; j < totalLen; j++) { |
| 53 | + if (arr[j] == num) { |
| 54 | + distance = j-i; |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + if (targetDistance < distance) { |
| 59 | + target = num; |
| 60 | + targetDistance = distance; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + count++; |
| 65 | + set.remove(target); |
| 66 | + set.add(arr[i]); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + private static void print() { |
| 76 | + System.out.println(count); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +``` |
0 commit comments