中等偏易题。操作系统理论中的最优页面调度算法,贪心。当需要淘汰某个模版时,淘汰掉当前手中在最远的将来才会被用到(或者以后永远不再用到)的那个。

代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <time.h> using namespace std; typedef pair<int, int> PII; const int INF = <<;
const int MAXN = (int) 1e5+; priority_queue<PII> q;
int Ti[MAXN], next[MAXN];
int Find[MAXN], len; //离散化用
int vis[MAXN]; //计算下一次出现用
bool inq[MAXN]; //判断是否拥有
int N, M; void input() {
for (int i = ; i < N; i++)
scanf("%d", &Ti[i]);
} void solve() {
//离散化
for (int i = ; i < N; i++)
Find[i] = Ti[i];
sort(Find, Find+N);
len = unique(Find, Find+N) - Find;
for (int i = ; i < N; i++)
Ti[i] = lower_bound(Find, Find+len, Ti[i]) - Find; //计算next[]
for (int i = ; i <= len; i++) vis[i] = N+; //初始化vis[]
for (int i = N-; i >= ; i--) {
next[i] = vis[Ti[i]];
vis[Ti[i]] = i;
} int ans = , have = ;
while (!q.empty()) q.pop(); //初始化队列
memset(inq, false, sizeof(inq));
for (int i = , j = ; i < M; i++) { //一开始就有的模板
int t = Find[j]==(i+) ? j++ : len;
q.push(make_pair(vis[t], t));
inq[t] = true;
have++;
} for (int i = ; i < N; i++) {
if (!inq[Ti[i]]) { //如果现在没有有这个模板,那么就要去借
have++;
ans++;
}
//把那个模板拿过来
inq[Ti[i]] = true;
q.push(make_pair(next[i], Ti[i])); if (have>M) { //需要丢弃模板
PII x = q.top(); q.pop();
inq[x.second] = false;
have--;
}
} printf("%d\n", ans);
} int main() {
#ifdef Phantom01
freopen("I.txt", "r", stdin);
#endif //Phantom01 while (scanf("%d%d", &N, &M)!=EOF) {
input();
solve();
} return ;
}

HDU 4398 Template Library Management (最优页面调度算法)的更多相关文章

  1. hdu 4398 Template Library Management(贪心+stl)

    题意:n道题,每道题需要一个模板,现在手头有m个模板(标号1~m),解题的时候,如果没有需要的模板,可以向朋友借,但是用完之后必须在还给朋友一个模板(也就是说保持手头拥有m个模板),求解完n道题最少需 ...

  2. Simple Library Management System HDU - 1497(图书管理系统)

    Problem Description After AC all the hardest problems in the world , the ACboy 8006 now has nothing ...

  3. C++ Standard Template Library STL(undone)

    目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...

  4. hdu 4398 STL

    题意描述半天描述不好,直接粘贴了 Now your team is participating a programming contest whose rules are slightly diffe ...

  5. Calibre - book library management application

    http://calibre-ebook.com/ Library Management E-book conversion Syncing to e-book reader devices Down ...

  6. [c++] STL = Standard Template Library

    How many people give up, because of YOU. Continue... 先实践,最后需要总结. 1. 数据流中的数据按照一定的格式<T>提取 ------ ...

  7. django升级2.1python升级3.7时出现的错误:"trying to load '%s': %s" % (entry[1], e) django.template.library.InvalidTemplateLibrary:

    django升级2.1python升级3.7时出现如下的错误: "trying to load '%s': %s" % (entry[1], e) django.template. ...

  8. 基于 ASP.NET Core 2.1 的 Razor Class Library 实现自定义错误页面的公用类库

    注意:文中使用的是 razor pages ,建议使用 razor views ,使用 razor pages 有一个小坑,razor pages 会用到 {page} 路由参数,如果应用中也用到了这 ...

  9. 【HDOJ】1497 Simple Library Management System

    链表. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXM 1001 #def ...

随机推荐

  1. js中es5 使用call方法继承实现 1.0

    function Parent(name){ this.name = name; this.showMess = function(){ return this.name; } } Parent.pr ...

  2. angular踩坑记录

    ng-repeat和ng-click同时使用的时候,注意不能直接在ng-click当中写代码,必须在当前$scope中绑定一个事件,转为调用该事件.否则会出现意想不到的情况.具体原因点击这里. 使用自 ...

  3. 在 android studio 中更新安卓应用版本号

    随着应用程序不断修改,版本号也应当变化.要更新安卓应用的版本号,只需要在 build.gradle(module:app) 中修改 versionCode 和 versionName 即可,也可以只改 ...

  4. 版本号比较[versionCompare]

    /*** * 版本号比较 * @param v1 版本号a * @param v2 版本号b * @return -1代表不是合格版本号:0代表一样大.1 代表版本号a大于版本号b.2代表版本号b大于 ...

  5. 正则表达式中的/i

    i = insensitive means case-insensitive 表示大小写不敏感

  6. Markdown语法简记

    目录 一.标题 1. 六个级别的标题 2. 主.副两级标题 二.根据标题生成文档结构大纲 三.字体 1. 斜体 2. 粗体 3. 倾斜加粗 4. 行首缩进 5. 删除线 四.引用块 五.代码块 1. ...

  7. linux常用命令技巧

    原文地址 这篇文章来源于Quroa的一个问答<What are some time-saving tips that every Linux user should know?>—— Li ...

  8. c traps and pitfalls reading note(1)

    1. 一直知道char *p = 'a';这样写是错误的,但是为什么是错的,没想过,今天看书解惑. p指向一个字符,但是在c中,''引起来的一个字符代表一个整数,这样指针能不报错.o(^▽^)o 2. ...

  9. 开源 PHP 项目找队友、顾问、指导、贡献者等等

    介绍站点还没做,先直接甩代码链接了 https://github.com/litphp/litphp Lit是什么? Lit是我一直在撸的个人框架,按第一次上传代码来说历史 超过4年 了,从还能支持P ...

  10. FZU 1980 AbOr's story

    AbOr's story Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original I ...