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
 
 
 #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;
}
}
 
上一篇:Gamemaker Studio运行时弹出打开窗口导致无法启动的错误


下一篇:Graham Scan凸包算法