Could someone tell the problem with this solution? Problem 802B. (Used Optimal page replacement like solution)

Revision en2, by nik96, 2017-06-19 06:59:33
#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	int n,k,chf = 0;
	cin>>n>>k;
	unordered_set<int> s;
	vector<int> v;
	queue<int> q[n+1];
	priority_queue<pair<int,int>> p;
	for(int i=0;i<n;i++)
	{
		int temp;
		cin>>temp;
		v.push_back(temp);
		q[temp].push(i);
	}
	for(int i=0;i<n;i++)
	{
		q[v[i]].pop();
		if(s.find(v[i])==s.end())
		{
			chf++;
			if(s.size()<k)
			{
				s.insert(v[i]);
				if(q[v[i]].empty())
				{
					p.push(make_pair(INT_MAX,v[i]));
				}
				else
				{
					p.push(make_pair(q[v[i]].front(),v[i]));
				}
			}
			else
			{
				//we have to replace a book
				pair<int,int> pa = p.top();
				p.pop();
				s.erase(pa.second);
				if(q[v[i]].empty())
				{
					p.push(make_pair(INT_MAX,v[i]));
				}
				else
				{
					p.push(make_pair(q[v[i]].front(),v[i]));
				}
				s.insert(v[i]);
			}
		}
	}
	cout<<chf;
	return 0;
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English nik96 2017-06-19 06:59:33 20
en1 English nik96 2017-06-19 06:58:27 1056 Initial revision (published)