您的位置:首页 > Web前端 > JavaScript

1826: [JSOI2010]缓存交换

2018-07-06 18:20 190 查看

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=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
}

const int N = 100010;
#define pa pair<int,int>
#define mp(a,b) make_pair(a,b)
priority_queue< pa > q;
map<int,int> last,vis;
int nxt
,a
;

int main() {
int n = read(),m = read();
for (int i=1; i<=n; ++i) nxt[i] = n+1;
for (int i=1; i<=n; ++i) {
a[i] = read();
if (last[a[i]]) nxt[last[a[i]]] = i;
last[a[i]] = i;
}
int ans = 0,tot = 0;
pa t;
for (int i=1; 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] == 0) continue;
vis[t.second] = 0;
break;
}
}
ans++;
q.push(mp(nxt[i],a[i]));
tot ++;
vis[a[i]] = 1;
}
cout << ans;
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: