日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

JSOI2010 BZOJ1826 缓存交换

發布時間:2023/11/27 生活经验 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 缓存交换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。