Question F of the 16th Program Design Competition of Xidian University

Link: https://www.nowcoder.com/acm/contest/107/F
Source: Niuke.com

Topic description

In the process of learning Operating System, Glory encountered such a problem. Now there is a memory with a size that can hold N pages. The content in the hard disk is divided into M pages, which are identified by 1~M. At the beginning, there is no memory in the memory. For any page, the next user will request Q pages, you need to design a replacement algorithm to minimize the number of page faults. A page fault is when a user requests a page with a certain number, but the page is not in memory. After a page fault occurs, you must transfer the corresponding page in the hard disk into the memory. If the memory is full, you need to replace a page in the current memory.

Enter description:

Multiple sets of data, please process until the end of the input.
For each set of data, the first row is three integers N, M, Q (0 < N, M, Q <= 50000)
The number of Q in the next line represents the page number requested by the user.

Output description:

For each set of data, output a number that represents the minimum number of page faults.

enter

2 3 5
3 1 2 1 2
3 4 5
3 2 1 4 3

output

3
4
#include<iostream>
#include<set>
using namespace std;
intmain()
{
	int n,m,q;
	while(cin>>n>>m>>q){
		set<int> s;
		while(q--){
			int x;
			cin>>x;
			s.insert(x);
		}
		cout<<s.size()<<endl;
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324691320&siteId=291194637