1826: [JSOI2010]缓存交换

1826: [JSOI2010]缓存交换

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

分析:

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

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

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 
 5 inline int read() {
 6     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
 7     for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
 8 }
 9 
10 const int N = 100010;
11 #define pa pair<int,int>
12 #define mp(a,b) make_pair(a,b)
13 priority_queue< pa > q;
14 map<int,int> last,vis;
15 int nxt[N],a[N];
16 
17 int main() {
18     int n = read(),m = read();
19     for (int i=1; i<=n; ++i) nxt[i] = n+1;
20     for (int i=1; i<=n; ++i) {
21         a[i] = read();
22         if (last[a[i]]) nxt[last[a[i]]] = i;
23         last[a[i]] = i;
24     }
25     int ans = 0,tot = 0;
26     pa t;
27     for (int i=1; i<=n; ++i) {
28         if (vis[a[i]]) {q.push(mp(nxt[i],a[i]));continue;}
29         if (tot == m) {
30             tot --;
31             while (!q.empty()) {
32                 t = q.top();
33                 q.pop(); //--居然忘记写了!!! 
34                 if (vis[t.second] == 0) continue;
35                 vis[t.second] = 0;
36                 break;
37             }
38         }
39         ans++;
40         q.push(mp(nxt[i],a[i]));
41         tot ++;
42         vis[a[i]] = 1;
43     }
44     cout << ans;
45     return 0;
46 }

猜你喜欢

转载自www.cnblogs.com/mjtcn/p/9275064.html