JSOI2010 BZOJ1826 缓存交换
生活随笔
收集整理的這篇文章主要介紹了
JSOI2010 BZOJ1826 缓存交换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
洛谷
BZOJ
分析
貪心策略很好想,顯然,當 \(cache\) 未滿時,直接放進去就行了; \(cache\) 滿了的時候,考慮交換哪一個,不難看出可以交換下一次出現最晚的那個。
注意:不開 \(map\) 過不了!
代碼
#include <map>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define il inline
#define re register
#define maxn 100005
#define tie0 cin.tie(0),cout.tie(0)
#define fastio ios::sync_with_stdio(false)
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;template <typename T> inline void read(T &x) {T f = 1; x = 0; char c;for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1;for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);x *= f;
}priority_queue <P> q;
map <int, int> lst, inq;int n, m, ans;
int a[maxn], nxt[maxn];int main() {memset(nxt, 1, sizeof(nxt));read(n), read(m);for (int i = 1; i <= n; ++i) {read(a[i]);nxt[lst[a[i]]] = i;lst[a[i]] = i;}for (int i = 1; i <= n; ++i) {if (inq[a[i]]) {q.push(make_pair(nxt[i], a[i]));m++;continue;}if (q.size() < m) {q.push(make_pair(nxt[i], a[i]));ans++; inq[a[i]] = 1;continue;}int k = q.top().second; q.pop();inq[k] = 0;q.push(make_pair(nxt[i], a[i]));inq[a[i]] = 1;ans++;}printf("%d", ans);return 0;
}
轉載于:https://www.cnblogs.com/hlw1/p/11437155.html
總結
以上是生活随笔為你收集整理的JSOI2010 BZOJ1826 缓存交换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BZOJ4401 块的计数
- 下一篇: 国家集训队2009 书堆