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 ...
随机推荐
- matlab张量工具初步
最近从桑迪亚实验室下载了张量工具包.但是不太会用. 很多网上的方法, addpath(pwd) cd met; addpath(pwd) savepath M=ones(4,3,2); X=tenso ...
- SSH框架下的多表增删改查
下载地址:SSH框架下的多表增删改查 点击进入码云Git下载 点击进入CSDN下载 项目结构: 项目代码就不全部贴出来了,只贴下核心代码.需要项目的自己可以去下载. package com.atgui ...
- Calling convention-调用约定
In computer science, a calling convention is an implementation-level (low-level) scheme for how subr ...
- moment.js获取本周本月本年的开始日期和结束日期
//获取本日 const startDate = moment().format('YYYY-MM-DD'); const startDate = moment().format('YYYY-MM-D ...
- vue 键盘监听事件
<template> <div class="hello"> <input v-on:keyup.enter="submit" t ...
- collections模块-namedtuple
namedtuple -> 命名元组 这里的命名指的是对元组中元素的命名. 通过一个例子来看 import collections Person = collections.namedtuple ...
- docker下修改mysql配置文件
原文:docker下修改mysql配置文件 版权声明:本文为博主原创文章,转载注明地址:http://blog.csdn.net/wang704987562 https://blog.csdn.net ...
- 【转载】Failed to load class "org.slf4j.impl.StaticLoggerBinder".问题解决
在进行hibernate配置好后运行测试类的时候出现: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" ...
- 将Spring Boot应用程序迁移到Java9:兼容性
将 Spring Boot 应用程序迁移到 Java 9:兼容性 随着 Java 9 的到来,关于如何迁移应用程序以使用模块系统有很多的讨论.遗憾的是,大多数文章的焦点都集中于简单的 Hello Wo ...
- 锐捷SNMp注意:
http://www.ruijie.com.cn/fw/wt/18855 1)设备名称.运行时间.sysOID.设备描述.联系人.设备位置:分别来自RFC-1213-MIB的system组中的sysN ...