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

代码:

 #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. 【BZOJ4940】【YNOI2016】这是我自己的发明

    阅读此篇文章前请先跟我大喊三声:dllxl!dllxl!dllxl! 咳咳. 题意: Description 给一个树,n 个点,有点权,初始根是 1. m 个操作,每次操作: 1. 将树根换为 x. ...

  2. tree编译

    没有tree命令,就需要下载源代码 [root@fyc tree-1.7.0]#cd /opt/src [root@fyc tree-1.7.0]# wget ftp://mama.indstate. ...

  3. [luogu] P1772 [ZJOI2006]物流运输(动态规划,最短路)

    P1772 [ZJOI2006]物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线 ...

  4. 【Henu ACM Round#24 D】Iterated Linear Function

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把B提取出来就是一个等比数列了. 求和一下会发现是这种形式. \(B*\frac{(A^n-1)}{A-1}+A^n*x\) 则求一 ...

  5. Camera Calibration 相机标定:原理简介(二)

    2 针孔相机模型 常见的相机标定中,使用的相机多为针孔相机(Pinhole camera),也就是大家熟知的小孔成像理论.将其中涉及的坐标系之间的相互转换抽离出来,即为针孔相机模型的核心. 上图所示的 ...

  6. Android 4.4 KitKat NotificationManagerService使用具体解释与原理分析(二)__原理分析

    前置文章: <Android 4.4 KitKat NotificationManagerService使用具体解释与原理分析(一)__使用具体解释> 转载请务必注明出处:http://b ...

  7. C++的IO操作

    #include <iostream> using namespace std; int main() {         char name[20];         char gend ...

  8. hibernate动态表名映射--仅仅有想不到,没有做不到

    近期的一个项目有一个需求,有N个考核单位,要对每一个考核单位生成一张考核情况表.这样做的目的是横切数据库,这这个需求的实现中,我的组员遇到了一个技术问题,我将我的解决的方法和整个思考过程与大家分享, ...

  9. 本地代码中使用Java对象

    通过使用合适的JNI函数,你可以创建Java对象,get.set 静态(static)和 实例(instance)的域,调用静态(static)和实例(instance)函数.JNI通过ID识别域和方 ...

  10. 12.红黑树set

    #include <iostream> //红黑树(自动保证平衡,自动生成平衡查找树) #include <set> #include <cstring> #inc ...