1826: [JSOI2010]缓存交换

时间:2022-04-10 11:35:09

1826: [JSOI2010]缓存交换

https://www.lydsy.com/JudgeOnline/problem.php?id=1826

分析:

  简单的贪心,然后调啊调。。。最近怎么了,码力大大下降,各种奇奇怪怪的bug漫天飞,以后少熬夜。

  贪心:每次pop一定是pop最远点的点。

代码:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for (;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
#define pa pair<int,int>
#define mp(a,b) make_pair(a,b)
priority_queue< pa > q;
map<int,int> last,vis;
int nxt[N],a[N]; int main() {
int n = read(),m = read();
for (int i=; i<=n; ++i) nxt[i] = n+;
for (int i=; i<=n; ++i) {
a[i] = read();
if (last[a[i]]) nxt[last[a[i]]] = i;
last[a[i]] = i;
}
int ans = ,tot = ;
pa t;
for (int i=; i<=n; ++i) {
if (vis[a[i]]) {q.push(mp(nxt[i],a[i]));continue;}
if (tot == m) {
tot --;
while (!q.empty()) {
t = q.top();
q.pop(); //--居然忘记写了!!!
if (vis[t.second] == ) continue;
vis[t.second] = ;
break;
}
}
ans++;
q.push(mp(nxt[i],a[i]));
tot ++;
vis[a[i]] = ;
}
cout << ans;
return ;
}