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
 
 
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <set>
  7. #include <queue>
  8. using namespace std;
  9. typedef struct abcd
  10. {
  11. int p,last;
  12. } abcd;
  13. abcd a[];
  14. int flag[];
  15. int main()
  16. {
  17. int c,n,b,i,j,now,ans;
  18. while(~scanf("%d%d%d",&c,&n,&b))
  19. {
  20. for(i=; i<n; i++)flag[i]=;
  21. for(i=; i<b; i++)
  22. scanf("%d",&a[i].p);
  23. for(i=b-; i>=; i--)
  24. {
  25. a[i].last=flag[a[i].p];
  26. flag[a[i].p]=i;
  27. }
  28. ans=now=;
  29. memset(flag,,sizeof(flag));
  30. priority_queue<pair<int,int> > q;
  31. while(!q.empty())q.pop();
  32. for(i=; i<b; i++)
  33. {
  34. if(now<c)
  35. {
  36. if(flag[a[i].p])
  37. {
  38. q.push(make_pair(a[i].last,a[i].p));
  39. }
  40. else
  41. {
  42. flag[a[i].p]=;
  43. now++;
  44. ans++;
  45. q.push(make_pair(a[i].last,a[i].p));
  46. }
  47. }
  48. else
  49. {
  50. if(flag[a[i].p])
  51. {
  52. q.push(make_pair(a[i].last,a[i].p));
  53. }
  54. else
  55. {
  56. ans++;
  57. while(flag[q.top().second]==)q.pop();
  58. pair<int,int >t=q.top();
  59. q.pop();
  60. flag[a[i].p]=;
  61. flag[t.second]=;
  62. q.push(make_pair(a[i].last,a[i].p));
  63. }
  64. }
  65. }
  66. cout<<ans<<endl;
  67. }
  68. }
 

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. h5video标签

    在video标签中,我们可以使用属性:videoWidth & videoHeight,它获取的是video的宽度和高度(媒体本身).虽然不能直接使用,但是可以通过计算宽高比得到 video ...

  2. window.onerror 应用实例

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp75   window.onerror = function(sMessa ...

  3. poj 2455 二分+最大流

    这个因为点少用邻接矩阵做的. 题意:求由1到n的t条不重复路径中最大边权值的最小值. 思路:先对边权进行排序,然后二分边权值,建图求从1到n的最大流,当最大流为t时便求出答案. 代码: #includ ...

  4. 通过createObjectURL实现图片预览

    实现原理:通过createObjectURL 创建一个临时指向某地址的二进制对象. 过程:点击触发隐藏的 input   file  的点击事件,使用createObjectURL读取 file,创建 ...

  5. JPG、PNG和GIF图片的基本原理及优…

    JPG.PNG和GIF图片的基本原理及优化方法 一提到图片,我们就不得不从位图开始说起,位图图像(bitmap),也称为点阵图像或绘制图像,是由称作像素(图片元素)的单个点组成的.这些点可以进行不同的 ...

  6. 转:【Java并发编程】之二十三:并发新特性—信号量Semaphore(含代码)

    载请注明出处:http://blog.csdn.net/ns_code/article/details/17524153 在操作系统中,信号量是个很重要的概念,它在控制进程间的协作方面有着非常重要的作 ...

  7. FFmpeg 常用命令收集

    FFmpeg 常用命令 合并视频 ffmpeg -i "KTDS-820A_FHD.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts i ...

  8. 结对编程四则运算gui

    码市地址:https://git.coding.net/linzhao/sizeyunsuangui.git 林 钊 -- 201421123105 吴世荣 -- 201421123119 王坤彬 - ...

  9. 使用properties配置文件为javabean注入属性值

    ①:实体类 package com.hts.entity; import java.io.Serializable; public class A implements Serializable{ p ...

  10. 使用Eclipse Egit与码云管理你的代码

    总体流程: 建立远程仓库 建立本地仓库并与远程仓库关联 将Eclipse中的项目提交到本地仓库并进而push到远程仓库 一. 配置Eclipse EGit 图解Eclipse中安装及配置EGit插件中 ...