main.cc 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #include <set>
  6. using std::priority_queue; using std::set;
  7. const int MAX = 100000;
  8. int a[MAX];
  9. int prev[MAX + 1];
  10. int next[MAX + 1];
  11. bool rem[MAX + 1];
  12. struct node {
  13. int x;
  14. int y;
  15. node(int x, int y) : x(x), y(y) {}
  16. int operator<(const node &that) const {
  17. return that.y - this->y;
  18. }
  19. };
  20. int main() {
  21. int N, M, K;
  22. scanf("%d %d %d", &N, &M, &K);
  23. for (int i = 1; i <= N; i++) {
  24. prev[i] = K;
  25. }
  26. for (int i = 0; i < K; i++) {
  27. scanf("%d", &a[i]);
  28. next[i] = K;
  29. next[prev[a[i]]] = i;
  30. prev[a[i]] = i;
  31. }
  32. int m = 0, cnt = 0;
  33. memset(rem, 0, sizeof(rem));
  34. priority_queue<node> pq;
  35. for (int i = 0; i < K; i++) {
  36. if (rem[a[i]]) {
  37. pq.push(node(a[i], next[i]));
  38. continue; // Hit the cache
  39. }
  40. if (m == M) {
  41. m--;
  42. while (!pq.empty()) {
  43. node top = pq.top();
  44. pq.pop();
  45. if (rem[top.x]) {
  46. rem[top.x] = false;
  47. break;
  48. }
  49. }
  50. }
  51. m++;
  52. cnt++;
  53. rem[a[i]] = true;
  54. pq.push(node(a[i], next[i]));
  55. }
  56. printf("%d\n", cnt);
  57. }