Operating system

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 659    Accepted Submission(s): 207

Problem Description
As a CS student, operating system is an important lesson. Today, let’s think about the cache’s working mode. All object accesses go through the cache, so every time an object is accessed, it must be inserted into the cache if it was not already there. If the cache is full, you must take out a object which is already in the cache . All objects are of equal size, and no writes occur in the system, so a cached object is always valid. When the system starts, the cache is empty. To make cache works efficiently, we need find the optimal replacement algorithm. Optimality here means minimizing the number of times an object is read into the cache.
 
Input
There are several test cases in the input.

Each test case begins with three integers(C,N,B), separated by single spaces, telling you how many objects fit in the cache, 0 < C ≤ 10000, how many different objects are in the system, C ≤ N ≤ 100000, and how many accesses, 0 ≤ B ≤ 100000, will occur. The following line contains B integers between 0 and N-1 (inclusive) indicating what object is accessed.

 
Output
For each test case, output one integer, indicating the least number of times an object must be read into the cache to handle the accesses.
 
Sample Input
1 2 3
0 1 0
2 2 3
0 1 0
 
Sample Output
3
2
 
 
 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <set>
#include <queue>
using namespace std;
typedef struct abcd
{
int p,last;
} abcd;
abcd a[];
int flag[];
int main()
{
int c,n,b,i,j,now,ans;
while(~scanf("%d%d%d",&c,&n,&b))
{
for(i=; i<n; i++)flag[i]=;
for(i=; i<b; i++)
scanf("%d",&a[i].p);
for(i=b-; i>=; i--)
{
a[i].last=flag[a[i].p];
flag[a[i].p]=i;
}
ans=now=;
memset(flag,,sizeof(flag));
priority_queue<pair<int,int> > q;
while(!q.empty())q.pop();
for(i=; i<b; i++)
{
if(now<c)
{
if(flag[a[i].p])
{
q.push(make_pair(a[i].last,a[i].p));
}
else
{
flag[a[i].p]=;
now++;
ans++;
q.push(make_pair(a[i].last,a[i].p));
}
}
else
{
if(flag[a[i].p])
{
q.push(make_pair(a[i].last,a[i].p));
}
else
{
ans++;
while(flag[q.top().second]==)q.pop();
pair<int,int >t=q.top();
q.pop();
flag[a[i].p]=;
flag[t.second]=;
q.push(make_pair(a[i].last,a[i].p));
}
}
}
cout<<ans<<endl;
}
}
 

Operating system hdu 2835 OPT的更多相关文章

  1. OSMC Vs. OpenELEC Vs. LibreELEC – Kodi Operating System Comparison

    Kodi's two slim-and-trim kid brothers LibreELEC and OpenELEC were once great solutions for getting t ...

  2. DBCC CHECKDB 遭遇Operating system error 112(failed to retrieve text for this error. Reason: 15105) encountered

    我们一个SQL Server服务器在执行YourSQLDBa的作业YourSQLDba_FullBackups_And_Maintenance时遇到了错误: Exec YourSQLDba.Maint ...

  3. The World's Only Advanced Operating System

    The World's Only Advanced Operating System

  4. Unable to open the physical file xxxx. Operating system error 2

    在新UAT服务器上,需要将tempdb放置在SSD(固态硬盘)上.由于SSD(固态硬盘)特性,所以tempdb的文件只能放置在D盘下面,而不能是D盘下的某一个目录下面. ALTER  DATABASE ...

  5. CREATE FILE encountered operating system error 5(Access is denied.)

    这篇博文主要演示"CREATE FILE encountered operating system error 5(Access is denied.)"错误如出现的原因(当然只是 ...

  6. Linux启动报错missing operating system

    用UltraISO制作了一个Red Hat Enterprise Linux Server release 5.7系统的U盘启动盘,然后在一台PC上安装,由于安装过程中在干别的事情,有些选项没有细看. ...

  7. Learning Roadmap of Robotic Operating System (ROS)

    ROS Wiki: http://wiki.ros.org/ Robots Using ROS Textbooks: A Gentle Introduction to ROS Learning ROS ...

  8. Full exploitation of a cluster hardware configuration requires some enhancements to a single-system operating system.

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Operating System Desi ...

  9. Multiprocessor Operating System Design Considerations SYMMETRIC MULTIPROCESSORS

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION An SMP operating syst ...

随机推荐

  1. hibernate 基本配置文件及CRDU的操作和基本的HQL查询

    以下所有内容保存在 E:\JavaWebSrc\firstHibernate 目录中,项目名称为firstHibernate,使用IDEA打开,项目所需jar包在  F:\常用综合\常用jar包\hi ...

  2. 在JS事件封装时,addEventListener()方法的this问题

    最近在写js的类库,模仿的是jquery的编程风格,当封装到事件监听的时候发现遇到了一个问题,代码是这样的: 上面是封装的一个事件委托的代码,我以为上面的封装跟普通的事件监听一样简单,结果我在调用时发 ...

  3. TC358749XBG:HDMI转MIPI CSI芯片简介

    TC358749XBG是一颗HDMI转MIPI CSI功能的视频转换芯片,分辨率:1920*1080,电源3.3/1.8/1.2,通信方式:IIC,封装形式BGA80

  4. C++栈和堆的生长方向

    C++内存区域分为5个区域.分别是堆,栈,自由存储区,全局/静态存储区和常量存储区. 栈:由编译器在需要的时候分配,在不需要的时候自动清除的变量存储区.里面通常是局部变量,函数参数等. 堆:由new分 ...

  5. zTree勾选状态的禁用节点不在选中节点里

    问题描述: 由于业务需求,需要将一部分节点设置为选中并且是禁用的状态.设置这部分节点的chkDisabled和checked属性值都为true.在zTree树上这部分节点是选中且禁用的状态,但是在保存 ...

  6. 猜数字游戏--基于python

    """题目:练习使用python写一个猜数字的游戏,数字范围0-100,每次猜错,需要给出缩小后的范围,每个人只有10次的猜测机会,猜测机会用完游戏结束!"&q ...

  7. IntelliJ IDEA使用(一)基本设置与类、方法模板设置

    其实之前一直开发都是在使用的是Eclipse,但是最近在做Maven项目的时候要用IntelliJ IDEA,据说这个idea功能非常的强大,最近在使用的时候发现如果适应的真的是非常的强大.感觉是比E ...

  8. jQuery的less和scss之less的基本介绍(一)

    简单的整理了一下less的基本用法,希望对大家有所帮助ㅎㅎ 一.less基础语法 1.声明变量:@变量名:变量值 使用变量:@变量名 例如 @color : #ff0000; @length : 10 ...

  9. 【Beta阶段】第一次scrum meeting

    Coding/OSChina 地址 1. 会议内容 学号 主要负责的方向 昨日任务 昨日任务完成进度 接下去要做 9 9 PM 博客编写,会议总结,代码整理 100% 准备下一次会议内容,并对已完成的 ...

  10. sudoku作业

    1.Github项目地址: https://github.com/ataiyang/ls 2.PSP表格 PSP2.1 Personal Software Process Stages 预估耗时(分钟 ...