Main.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Main main = new Main();
  5. Scanner in = new Scanner(System.in);
  6. int n = in.nextInt(), m = in.nextInt(), k = in.nextInt();
  7. int[] a = new int[k];
  8. for (int i = 0; i < k; i++) {
  9. a[i] = in.nextInt();
  10. }
  11. in.close();
  12. System.out.println(main.solve(n, m, a));
  13. }
  14. public int solve(int n, int m, int[] a) {
  15. int[] prev = new int[n + 1];
  16. int[] next = new int[a.length + 1];
  17. Arrays.fill(prev, a.length);
  18. for (int i = 0; i < a.length; i++) {
  19. next[i] = a.length;
  20. next[prev[a[i]]] = i;
  21. prev[a[i]] = i;
  22. }
  23. PriorityQueue<Node> pq = new PriorityQueue<>();
  24. boolean[] rem = new boolean[n + 1];
  25. int size = 0, ans = 0;
  26. for (int i = 0; i < a.length; i++) {
  27. if (rem[a[i]]) {
  28. pq.add(new Node(a[i], next[i]));
  29. continue;
  30. }
  31. if (size == m) {
  32. size--;
  33. while (!pq.isEmpty()) {
  34. Node top = pq.poll();
  35. if (rem[top.x]) {
  36. rem[top.x] = false;
  37. break;
  38. }
  39. }
  40. }
  41. size++;
  42. ans++;
  43. rem[a[i]] = true;
  44. pq.add(new Node(a[i], next[i]));
  45. }
  46. return ans;
  47. }
  48. }
  49. class Node implements Comparable<Node> {
  50. int x;
  51. int y;
  52. public Node(int x, int y) {
  53. this.x = x;
  54. this.y = y;
  55. }
  56. @Override
  57. public int compareTo(Node that) {
  58. return that.y - this.y;
  59. }
  60. }