HDU 4398 Template Library Management (最优页面调度算法)
中等偏易题。操作系统理论中的最优页面调度算法,贪心。当需要淘汰某个模版时,淘汰掉当前手中在最远的将来才会被用到(或者以后永远不再用到)的那个。
代码:
#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 (最优页面调度算法)的更多相关文章
- hdu 4398 Template Library Management(贪心+stl)
题意:n道题,每道题需要一个模板,现在手头有m个模板(标号1~m),解题的时候,如果没有需要的模板,可以向朋友借,但是用完之后必须在还给朋友一个模板(也就是说保持手头拥有m个模板),求解完n道题最少需 ...
- Simple Library Management System HDU - 1497(图书管理系统)
Problem Description After AC all the hardest problems in the world , the ACboy 8006 now has nothing ...
- C++ Standard Template Library STL(undone)
目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...
- hdu 4398 STL
题意描述半天描述不好,直接粘贴了 Now your team is participating a programming contest whose rules are slightly diffe ...
- Calibre - book library management application
http://calibre-ebook.com/ Library Management E-book conversion Syncing to e-book reader devices Down ...
- [c++] STL = Standard Template Library
How many people give up, because of YOU. Continue... 先实践,最后需要总结. 1. 数据流中的数据按照一定的格式<T>提取 ------ ...
- 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. ...
- 基于 ASP.NET Core 2.1 的 Razor Class Library 实现自定义错误页面的公用类库
注意:文中使用的是 razor pages ,建议使用 razor views ,使用 razor pages 有一个小坑,razor pages 会用到 {page} 路由参数,如果应用中也用到了这 ...
- 【HDOJ】1497 Simple Library Management System
链表. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXM 1001 #def ...
随机推荐
- C++ STL next_permutation() prev_permutation(a,a+n)用法。
int a[3] = {1,2,3}; a可能形成的集合为{1,2,3},{1,3,2},{2,1,3},{2,3,1},{3,1,2},{3,2,1}. {2,1,3}的prev是{1,3,2}, ...
- load多个数据文件的yaml
VERSION: 1.0.0.1DATABASE: testUSER: adminHOST: node31PORT: 5432GPLOAD: INPUT: - SOURCE: LOCAL_HOSTNA ...
- 转 c#中 base64字符串与普通字符串互转
https://blog.csdn.net/hwt0101/article/details/79758912 转成 Base64 形式的 System.String: string a = &q ...
- wordpress 后台登录增加访问效验
目前已知的增加 wordpress 后台登录安全的方案有三种: 安全插件:如Limit Login Attempts Reloaded.WPS Hide Login 等等: 登录 URL 增加自定义k ...
- JSON.stringify(),JSON.parse(),eval(string)
JSON.stringify()用于从一个对象解析出字符串 : var obj = {"name":"week","age":" ...
- C#获取实例运行时间StopWatch类
在程序运行时有时需要获取某一步骤的操作时间,C#提供的StopWatch类可以很方便的实现这一目的. StopWatch sw=new StopWatch(); sw.Start(); //Do So ...
- css 遮照镂空效果
实现这个效果有以下几种方式.(欢迎指出不足之处!!!) 一:最简单最粗暴的方法!截图! 实现原理:先截一张图片,然后写一个遮罩层,再把图片放上去就可以了! 过程过于简单,就别写代码跟截图效果了! 优 ...
- CSS常用样式--font
CSS font 属性 参考:W3school- CSS font 所有浏览器都支持 font 属性,可在一个声明中设置所有字体属性,各属性需按顺序,语法如下: selector{ font:styl ...
- Edward Frenkel关于几何化朗兰兹纲领的采访
本文来自:菲尔兹奖座谈会 博客 Edward Frenkel教授的主要研究方向是数学与量子物理中的对称.他现在在做的许多问题都与朗兰兹纲领有关.他现在是加州大学伯克利分校的数学教授. 在今年的菲尔兹奖 ...
- Python seed() 函数--每次产生一样的随机数系列
import random random.seed( 10 ) print("Random number with seed 10 : ", random.random()) #0 ...