堆/优先队列
这个题目写起来不难,题目读起来是真的困难,出题人根本没把题目意思交代清楚……
观摩大佬AC代码之后,看的有点懵,反正给出的数,需要求一下下一次出现的位置,可以用2个数组,也可以用umap去离散记录。我就不展开了。
其余的就是贪心的思路了,按下一次出现优先降序排序,最大的那个最先出队,如果出现时间相同,按照输入的数据升序排序,可看代码用了pair自带的二元组,第一元素先升序,第二元素再升序。关于最先出队C++的优先队列即可满足。
#include <bits/stdc++.h>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
const int N = 1e5 + 7;
int a[N], vis[N], ne[N], last[N];
priority_queue<pair<int, int>> pq;
int main() {
js; int n, m, q;
while (cin >> n >> m >> q) {
while (pq.size()) pq.pop();
memset(vis, 0, sizeof(vis));
memset(ne, 0, sizeof(ne));
memset(last, 0x3f, sizeof(last)); //无穷大
for (int i = 1; i <= q; ++i) cin >> a[i];
for (int i = q; i; --i)
ne[i] = last[a[i]], last[a[i]] = i;
int ans = 0;
for (int i = 1; i <= q; ++i) {
if (pq.size() < n and !vis[a[i]])
++ans, vis[a[i]] = 1;
else if (pq.size() == n and !vis[a[i]]) {
++ans;
vis[pq.top().second] = 0;
vis[a[i]] = 1;
pq.pop();
}
pq.push({ ne[i],a[i] });
}
cout << ans << endl;
}
return 0;
} 
京公网安备 11010502036488号