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 }
View Code
 
原文地址:https://www.cnblogs.com/ERKE/p/3837525.html