-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0026.cpp
More file actions
99 lines (78 loc) · 1.72 KB
/
0026.cpp
File metadata and controls
99 lines (78 loc) · 1.72 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// https://coderun.yandex.ru/problem/avto
// caching + belady (farthest next use) + heap
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define rep(i,a,b) for (int i = (a); i < (b); ++i)
#define rsr(v,n) (v).reserve(n)
#define fi first
#define se second
const int INF = 1e9;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, k, p;
if (!(cin >> n >> k >> p)) return 0;
vi req(p), nxt(p);
rep(i, 0, p) cin >> req[i];
if (k <= 0)
{
cout << p << '\n';
return 0;
}
vi last(n + 1, INF);
for (int i = p - 1; i >= 0; --i)
{
int x = req[i];
nxt[i] = last[x];
last[x] = i;
}
vi best(n + 1, INF);
vector<unsigned char> in(n + 1, 0);
priority_queue<pair<int, int>> pq;
int cur = 0;
int ans = 0;
rep(i, 0, p)
{
int id = req[i];
int nu = nxt[i];
best[id] = nu;
if (in[id])
{
pq.push({nu, id});
continue;
}
++ans;
if (cur < k)
{
in[id] = 1;
++cur;
pq.push({nu, id});
}
else
{
while (!pq.empty())
{
auto t = pq.top();
pq.pop();
int vid = t.se;
int vnu = t.fi;
if (in[vid] && best[vid] == vnu)
{
in[vid] = 0;
--cur;
break;
}
}
in[id] = 1;
++cur;
pq.push({nu, id});
}
}
cout << ans << '\n';
return 0;
}