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. 针对通达OA20170729集团版设计门户管理解决方案的具体实例

    随着企业信息化建设的深入,应用系统数量不断增加,应用功能不断丰富,数据信息也在呈几何级数增长,在支撑企业正常工作运转的同时,也衍生出许多新的问题: 通达信科根据自身从业多年的行业实践经验,勇于打破传统 ...

  2. MySQL索引选择及规则整理

    索引选择性就是结果个数与总个数的比值. 用sql语句表示为: SELECT COUNT(*) FROM table_name WHERE column_name/SELECT COUNT(*) FRO ...

  3. vim文本基础

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...

  4. 自制VTP实验总结

    (packet tracer模拟器) 6.1)实验拓扑 //绿色:通:橙色:不通 //sw0是根桥:全通 Pc0:ip 192.168.1.1 Pc1: ip 192.168.1.2 Pc2:ip 1 ...

  5. 大家一起来找茬(BUG)

    大家一起来找茬(BUG) ----------目录---------- 一.上手体验 1.主界面 2.功能 二.程序的 BUG 三.必应词典的 BUG 1."每日一句"里的句子不能 ...

  6. bean的生命周期以及延迟实例化

    可以指定bean的初始化创建的时候调用的方法,以及销毁的时候调用的方法. 通过指定中的init-method和destroy-method方法指定bean的创建和销毁的时候执行类中的方法. 把lazy ...

  7. 201521123039 《java程序设计》第七周学习总结

    1. 本周学习总结 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 答:ArrayList的Contains的源码如上图所示,如果o为null,那么 ...

  8. 201521123068《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 点击查看->高清脑图 1.2 使用常规方法总结其他上课内容. 答:学习继承与多态的知识,了解它们之间的关系:super.ext ...

  9. 201521123049 《JAVA程序设计》 第2周学习总结

    *1. 本周学习总结 1.复习了一遍基本类型:整数类型(byte,short,int,long,char).浮点类型(float,double).boolean类型(true, false). 2.了 ...

  10. 201521044091 《Java程序设计》第2周学习总结

    1本章学习总结 (1)一些java的基本语法 (2)java API文件 (3)使用码云管理自己的代码 2.Java Q&A 1)使用Eclipse关联jdk源代码(截图),并查看String ...