Operating system hdu 2835 OPT

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[110000];
14 int flag[110000];
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=0; i<n; i++)flag[i]=110000;
21         for(i=0; i<b; i++)
22             scanf("%d",&a[i].p);
23         for(i=b-1; i>=0; i--)
24         {
25             a[i].last=flag[a[i].p];
26             flag[a[i].p]=i;
27         }
28         ans=now=0;
29         memset(flag,0,sizeof(flag));
30         priority_queue<pair<int,int> > q;
31         while(!q.empty())q.pop();
32         for(i=0; 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]=1;
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]==0)q.pop();
58                     pair<int,int >t=q.top();
59                     q.pop();
60                     flag[a[i].p]=1;
61                     flag[t.second]=0;
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,布布扣,bubuko.com

时间: 2024-08-10 23:30:00

Operating system hdu 2835 OPT的相关文章

China Operating System 电脑操作系统 2016全球互联网排名

韩梦飞沙  韩亚飞  [email protected]  yue31313  han_meng_fei_sha 电脑操作系统 China Operating System 2016全球互联网排名 ========

Linux Operating System

Linux is a Unix-like computer operating system assembled under the model of free and open source software developmentand distribution .The defining component of Linux is the Linux kernel , andoperating system kernel was first released October 5 , 199

Linux安装Oracle报Checking operating system version must be redhat-3, SuSE-9, redhat-4

解决办法:vi /xx/database/install/oraparam.ini 找到[Certified Versions] Linux=redhat-3,SuSe-9,redhat-4,后面加上redhat-5保存并退出即可.如图: Linux安装Oracle报Checking operating system version must be redhat-3, SuSE-9, redhat-4,布布扣,bubuko.com Linux安装Oracle报Checking operating

Portable Operating System Interface for uni-X

https://kb.iu.edu/d/agjv Short for "Portable Operating System Interface for uni-X", POSIX is a set of standards codified by the IEEE and issued by ANSI and ISO. The goal of POSIX is to ease the task of cross-platform software development by esta

Modern Operating System --- Thin Clients

Over the years, the main computing paradigm has oscillated between centralized and decentralized computing. The first computers, such as the ENIAC, were, in fact, personal computers, albeit large ones, because only one person could use one at once. T

资源向导之 &quot;Operating System&quot;

资源向导之"Operating System" ------------------------------------------------- MIT --- JOS  ------------------------------------------------ MIT关于JOS实验的主站 http://pdos.csail.mit.edu/6.828/2014/index.html MIT - Operating System Engineering(不同于6.008) ht

Learning Roadmap of Robotic Operating System (ROS)

ROS Wiki: http://wiki.ros.org/ Robots Using ROS Textbooks: A Gentle Introduction to ROS Learning ROS for Robotics Programming 1st Edition [pdf] [code] [ros wiki] 2nd Edition [code] [wiki] [online reading] Blog / Tutorial ROS 101: INTRO TO THE ROBOT O

Modern Operating System --- Power Management (Hard Disk)

Another major villain is the hard disk. It takes substantial energy to keep it spinning at high speed, even if there are no accesses. Many conputers, especially notebooks, spin the disk down after a certain number of seconds or minutes of inactivity.

Modern Operating System --- Power Management (OS Issues)

The operating system plays a key role in energy management. It controls all the devices, so it must decide what to shutdown and when to shut it down. If it shuts down a device and that device is needed again quickly, there may be an annoying delay wh