BZOJ 1206 [HNOI2005]虚拟内存:模拟

news/2024/7/20 15:57:42 标签: php, 内存管理, 数据结构与算法

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1206

题意:

  内存大小为n(外存无限大),共有m次访问,每一次访问的信息编号为p。

  对于每一条信息,不在内存中,就在外存中。

  内存和外存的访问速度不同。为了提高整体的访问速度,有这样一种内存管理的算法:

    (1)如果p在内存中,直接访问,算法结束。否则转步骤(2)。

    (2)如果内存有剩余空间,则将p由外存转移到内存中来,算法结束。否则转步骤(3)。

    (3)选出内存中访问次数最少的一条信息(访问次数相同选进入内存时间早的),用p将它替换掉,算法结束。

  访问次数是指:某条信息从进入内存的那一刻开始一直到现在的访问次数。(如果之前进入过内存,然后又被替换掉了,那么之前的访问次数不算)

  问你在内存中直接访问到p的次数。(即执行步骤(1)的次数)

 

题解:

  模拟。

  用STL中map和set神器~~~

 

  map映射:p的编号idx -> (p的访问次数cnt, p进入内存的时间tim)

  set:(idx, cnt, tim) 排序优先级:先cnt,后tim。(均取小)

 

  map用来查询p是否在内存中,以及有关p的信息(以便在set中定位到相应元素)

  set用来作优先队列(可任意插入和删除元素),队首为内存中访问次数最少(或进入内存最早)的一条信息。

 

  map和set分别保证了在log(N)时间内的查找和删除(替换)信息。

  总复杂度为O(m * log(n))。

 

AC Code:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <map>
 5 #include <set>
 6 
 7 using namespace std;
 8 
 9 struct Pro
10 {
11     int idx;
12     int cnt;
13     int tim;
14     Pro(int _idx,int _cnt,int _tim)
15     {
16         idx=_idx;
17         cnt=_cnt;
18         tim=_tim;
19     }
20     Pro(){}
21     friend bool operator < (const Pro &a,const Pro &b)
22     {
23         return a.cnt!=b.cnt?a.cnt<b.cnt:a.tim<b.tim;
24     }
25 };
26 
27 int n,m,p;
28 int ans=0;
29 map<int,pair<int,int> > mp;
30 set<Pro> st;
31 
32 int main()
33 {
34     cin>>n>>m;
35     for(int i=0;i<m;i++)
36     {
37         cin>>p;
38         map<int,pair<int,int> >::iterator it_mp=mp.find(p);
39         if(it_mp!=mp.end())
40         {
41             int cnt=(it_mp->second).first;
42             int tim=(it_mp->second).second;
43             st.erase(Pro(p,cnt,tim));
44             st.insert(Pro(p,cnt+1,tim));
45             mp[p]=pair<int,int>(cnt+1,tim);
46             ans++;
47         }
48         else if(mp.size()<n)
49         {
50             mp.insert(pair<int,pair<int,int> >(p,pair<int,int>(1,i)));
51             st.insert(Pro(p,1,i));
52         }
53         else
54         {
55             set<Pro>::iterator it_st=st.begin();
56             int idx=it_st->idx;
57             int cnt=it_st->cnt;
58             int tim=it_st->tim;
59             mp.erase(idx);
60             st.erase(it_st);
61             mp.insert(pair<int,pair<int,int> >(p,pair<int,int>(1,i)));
62             st.insert(Pro(p,1,i));
63         }
64     }
65     cout<<ans<<endl;
66 }

 

转载于:https://www.cnblogs.com/Leohh/p/7518477.html


http://www.niftyadmin.cn/n/978449.html

相关文章

不知名的用户可以通过病毒推文赚钱

By Michael Tobin迈克尔托宾(Michael Tobin) You don’t need to be Twitter famous to make money from viral tweets.您无需成为Twitter著名人物即可通过病毒性推文赚钱。 A growing number of Twitter users –- many of them with very few followers of their own — ha…

shell--7、Shell test 命令

Shell中的 test 命令用于检查某个条件是否成立&#xff0c;它可以进行数值、字符和文件三个方面的测试。 数值测试 参数说明-eq等于则为真-ne不等于则为真-gt大于则为真-ge大于等于则为真-lt小于则为真-le小于等于则为真实例演示&#xff1a; 12345678num1100num2100if test $[…

elon函数_为什么elon麝香starlink会改变您的生活

elon函数重点 (Top highlight)The most important causes of change are not to be found in political manifestos or in the pronouncements of dead economists, but in the hidden factors that alter the boundaries where power is exercised.变化的最重要原因不是在政治…

charles 踩坑记录

charles破解教程&#xff1a;http://www.jianshu.com/p/12e75eb8f53d 1、需注意软件和破解脚本的版本是否正确&#xff08;例如3.x.x版本的破解脚本不能用于4.x.x版本的charles&#xff09;2、若闪退可尝试重装软件3、若显示文件已损坏可以在系统偏好设置中的安全与隐私设置允许…

Android 4.4(KitKat)表格管理子系统 - 骨架

原文地址&#xff1a;http://blog.csdn.net/jinzhuojun/article/details/37737439 窗体管理系统是Android中的主要子系统之中的一个。它涉及到App中组件的管理&#xff0c;系统和应用窗体的管理和绘制等工作。因为其涉及模块众多&#xff0c;且与用户体验密切相关。所以它也是A…

学习c++第01天

学习c的第01天 前言1、变量是声明&#xff1f;2.建议定义数据都对其进行初始化3.有符号数和无符号数4.进制间的相互转换5.原反补码6.const 、register 、volatile和typedef关键字7.数据类型的自动转换8.左移<< &右移操作>>9.将data的指定位数进行0、1转化的应用…

sqlserver表、视图、索引(创建、修改、删除)相关示例

一、表相关 1、创建 1234567891011121314151617181920212223242526USE [test]GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER ON GOCREATE TABLE [dbo].[Ceshi]( [id] [int] NOT NULL, [name] [varchar(30)] NULL, CONSTRAINT [PK_Ceshi] PRIMARY KEY CLUSTERED ([id] ASC )WIT…

21英里法则_穿着自动鞋带走一英里

21英里法则From owning my own mobile phone at age 11, I got used to charging my phone, as we all are accustomed to doing. As a tech enthusiast, I was an early adopter of many new technologies. Suddenly, charging my watch became commonplace. My first car was…