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. 【C#多线程编程实战笔记】二、 线程同步

    使用Mutex类-互斥锁 owned为true,互斥锁的初始状态就是被主线程所获取,否则处于未获取状态 name为定义的互斥锁名称,在整个操作系统只有一个命名未CSharpThreadingCookb ...

  2. js 你所不知道的一面

    你真的知道JavaScript吗 JavaScript是一门奇怪的语言,要真正掌握并不容易.废话不多说,来一个快速测试,5道题目,看看你对JavaScript是否真正掌握.准备好了吗?开始咯

  3. NOPI读xls文件写到txt中(NPOI系列二)

    private void button2_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); //找 ...

  4. 团队作业4——第一次项目冲刺(ALpha版本)第四天

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 1.界面 完善了昨天的的代码---前端的HTML页面设计 2.功能 完成后台数据处理的全部基本功能: a.数据结构设计及数据交 ...

  5. Swing-BoxLayout用法-入门

    注:本文内容源于http://www.java3z.com/cwbwebhome/article/article20/200016.html?id=4797:细节内容根据笔者理解有修改. BoxLay ...

  6. 201521123106《java程序设计》第四周学习总结

    1. 本周学习总结 2. 书面作业 1.注释的应用使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) 面向对象设计(大作业1-非常重要)2.1 讲故事:将在网上商 ...

  7. 让你的python程序同时兼容python2和python3

    python邮件列表里有人发表言论说「python3在10内都无法普及」.在我看来这样的观点有些过于悲观,python3和python2虽然不兼容,但他们之间差别并没很多人想像的那么大.你只需要对自己 ...

  8. 201521123007《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu ...

  9. 201521123099 《Java程序设计》第9周学习总结

    1. 本周学习总结 2. 书面作业 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免? 容易出现拼写错误还有数组越界的异 ...

  10. Linux系统文件与目录权限管理

    Linux文件目录权限管理 一.Linux文件属性及权限 1.Linux文件及目录权限及属性说明 (1)权限及属性说明 (2)文件权限说明 三种权限说明:r 读  read w 写  write  x ...