Problem:
Problem A:
Cache Control
Mr. Haskins is working on tuning a database system. The database is a simple associative storage that contains key-value pairs. In this database, a key is a distinct identification (ID) number and a value is an object of any type.
In order to boost the performance, the database system has a cache mechanism. The cache can be accessed much faster than the normal storage, but the number of items it can hold at a time is limited. To implement caching, he selected least recently used (LRU) algorithm: when the cache is full and a new item (not in the cache) is being accessed, the cache discards the least recently accessed entry and adds the new item.
You are an assistant of Mr. Haskins. He regards you as a trusted programmer, so he gave you a task. He wants you to investigate the cache entries after a specific sequence of accesses.
Input
The first line of the input contains two integers
N
and
M
.
N
is the number of accessed IDs, and
M
is the size of the cache. These values satisfy the following condition: 1 ≤
N
,
M
≤ 100000.
The following
N
lines, each containing one ID, represent the sequence of the queries. An ID is a positive integer less than or equal to 10
9
.
Output
Print IDs remaining in the cache after executing all queries. Each line should contain exactly one ID. These IDs should appear in the order of their last access time, from the latest to the earliest.
Sample Input and Output
Input #1
3 2
1
2
3
Output #1
3
2
Input #2
5 3
1
2
3
4
1
Output #2
1
4
3

Submitted solutions:

code_snippet: #include<cstdio>
#include<algorithm>
#include<queue>
#include<set>
#include<stack>
using namespace std;

const int maxn=1e5+100;
int a[maxn];
set<int> s;
int main()
{
    int n,m,t,temp,i;
    scanf("%d%d",&n,&m);
	for(i=0;i<n;++i)
		scanf("%d",&a[i]);
	for(i=n-1;i>=0;--i)
	{
		if(s.find(a[i])==s.end()&&m)	
		{
			printf("%d\n",a[i]);--m;
			s.insert(a[i]);
		}
	}
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 5896
final_rank: 2

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    map<int, int> last_access;
    set<pair<int, int>> st;
    for (int tm = 1; tm <= n; tm++) {
        int a;
        cin >> a;
        if (st.size() < m) {
            if (st.find({last_access[a], a}) != st.end()) {
                st.erase(st.find({last_access[a], a}));
                last_access[a] = -tm;
                st.insert({-tm, a});
            } else {
                last_access[a] = -tm;
                st.insert({-tm, a});
            }
        } else {
            if (st.find({last_access[a], a}) != st.end()) {
                st.erase(st.find({last_access[a], a}));
                last_access[a] = -tm;
                st.insert({-tm, a});
            } else {
                auto it = st.end();
                it--;
                st.erase(*it);
                last_access[a] = -tm;
                st.insert({-tm, a});
            }
        }
    }
    for (auto &p : st) {
        cout << p.second << "\n";
    }
}
accuracy: 1
time_ms: 50
memory_kb: 8772
final_rank: 6

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    map<int, int> last_access;
    set<pair<int, int>> st;
    for (int tm = 1; tm <= n; tm++) {
        int a;
        cin >> a;
        if (st.size() < m) {
            last_access[a] = -tm;
            st.insert({-tm, a});
        } else {
            if (st.find({last_access[a], a}) != st.end()) {
                st.erase(st.find({last_access[a], a}));
                last_access[a] = -tm;
                st.insert({-tm, a});
            } else {
                auto it = st.end();
                it--;
                st.erase(*it);
                last_access[a] = -tm;
                st.insert({-tm, a});
            }
        }
    }
    for (auto &p : st) {
        cout << p.second << "\n";
    }
}
accuracy: 0.23529411764705882
time_ms: 20
memory_kb: 7552
final_rank: 13

code_snippet: #include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 5;
int ans[maxn];
set <int> s;

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &ans[i]);
    }
    for(int i=n;i>=1;i--)
    {
        if(!m)break;
        if(!s.count(ans[i]))
        {
            s.insert(ans[i]);
            printf("%d\n",ans[i]);
        }
        m--;
    }
    getchar();
    getchar();
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 6076
final_rank: 3

code_snippet: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
struct node{
	int no,c,p;
}ans[100005];
map<int,int> b;
int a[100005],c[100005],d[100005];
bool cmp(node x,node y)
{
	return x.c>y.c;
}
bool cmp2(node x,node y)
{
	return x.p<y.p;
}
int main(void)
{
	int n,m,i,j,k,cnt=0,f;
	map<int,int>::iterator it;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	if(n==1||m==1){
		printf("%d\n",a[n-1]);
		return 0;
	}
	for(i=n-1;i>=0;i--){
		it=b.find(a[i]);
		if(it==b.end()){
			cnt++;
			b[a[i]]=cnt;
			d[cnt]=a[i];
		}
	}
	for(i=0;i<n;i++){
		c[b[a[i]]]++;
	}
	for(i=1;i<=cnt;i++){
		ans[i].p=i;
		ans[i].no=d[i];
		ans[i].c=c[i];
	}
	sort(ans+1,ans+cnt+1,cmp);
	for(i=1;i<=m;i++){
		if(a[n-1]==d[i]){
			while(i<=m){
				ans[i]=ans[i+1];
				i++;
			}
			break;
		}
	}
	sort(ans+1,ans+m,cmp2);
	printf("%d\n",a[n-1]);
	for(i=1;i<m;i++){
		printf("%d\n",ans[i].no);
	}
	return 0;
}
accuracy: 0.23529411764705882
time_ms: 20
memory_kb: 4652
final_rank: 12

code_snippet: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
map<int,int> b;
int a[100005],c[100005],d[100005];
int main(void)
{
	int n,m,i,j,k,cnt=0;
	map<int,int>::iterator it;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	if(n==1||m==1){
		printf("%d\n",a[n-1]);
		return 0;
	}
	for(i=n-1;i>=0;i--){
		it=b.find(a[i]);
		if(it==b.end()){
			cnt++;
			b[a[i]]=cnt;
			d[cnt]=a[i];
		}
	}
	for(i=0;i<n;i++){
		c[b[a[i]]]++;
	}
	printf("%d\n%d\n",d[1],d[2]);
	for(i=3;i<=cnt&&i<m+1;i++){
		if(c[i]>=c[i-1]){
			printf("%d\n",d[i]);
		}
	}
	return 0;
}
accuracy: 0.23529411764705882
time_ms: 20
memory_kb: 3424
final_rank: 10

code_snippet: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
struct node{
	int no,cnt,s;
}c[100005];
map<int,int> b;
int a[100005];
bool cmp(node x,node y)
{
	return x.cnt<y.cnt;
}
bool cmp2(node x,node y)
{
	return x.s>y.s;
}
int main(void)
{
	int n,m,i,k,cnt=0;
	map<int,int>::iterator it;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
		it=b.find(a[i]);
		if(it==b.end()){
			cnt++;
			b[a[i]]=cnt;
		}
	}
	for(i=0;i<n;i++){
		c[b[a[i]]].cnt++;
		c[b[a[i]]].no=a[i];
		c[b[a[i]]].s=i;
	}
	if(cnt<=m){
		for(i=cnt;i>=1;i--){
			printf("%d\n",c[i].no);
		}
		return 0;
	}
	sort(c,c+cnt+1,cmp);
	sort(c+cnt+1-m,c+cnt+1,cmp2);
	for(i=cnt-m+1;i<=cnt;i++){
		printf("%d\n",c[i].no);
	}
	return 0;
}
accuracy: 0.1568627450980392
time_ms: 10
memory_kb: 3004
final_rank: 14

code_snippet: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#define ll long long
using namespace std;
int n,m;
map<int,int> s;
map<int,int> vis;
int vcnt;
int re[100033];
int siz=0;
int timeorder[100033];
map<int,int>::iterator it;
int ans[100033],acnt;
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		int k;
		scanf("%d",&k);
		if(vis[k]==0){
			vis[k]=++vcnt;
			re[vcnt]=k;
		}
		k=vis[k];
		if(timeorder[k]==0)
		{
			timeorder[k]=i;
			
			if(siz==m)
			{
//				printf("?%d %d %d\n",i,re[k],siz);
				it=s.begin();
//				printf("!%d\n",re[(*it).second]);
				s.erase(it);
//				printf("!!%d\n",re[(*s.begin()).second]);
				siz--;
			}
			siz++;
			s[i]=k;
		}else{
			if((it=s.find(timeorder[k]))!=s.end()){
				it=s.find(timeorder[k]);
				s.erase(it);
				timeorder[k]=i;
				s[i]=k;
//				printf("??%d\n",re[timeorder[i]]);
				continue;
			}
//			printf("?%d %d %d\n",siz,(*s.begin()).first,re[(*s.begin()).second]);
			if(siz==m)
			{
				it=s.begin();
				s.erase(it);
				siz--;
			}
			timeorder[k]=i;
			s[i]=k;
			siz++;
		}
	}
	
	for(it=s.begin();it!=s.end();it++)
	{
//		printf("%d\n",re[(*it).second]);
		ans[++acnt]=re[(*it).second];
	}
	for(int i=acnt;i>=1;i--)
		printf("%d\n",ans[i]);
//	s[1]=2;s[3]=4;
//	it=s.begin();
//	cout<<(*it).first<<endl;
//	s.erase(it);
//	cout<<s[1];
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 9080
final_rank: 7

code_snippet: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<set>
#include<cstring>
using namespace std;
int b[100005];
int a[100005];
int c[100005];
int n,m,k=0,f=0;
int main (){
	memset(c,0,sizeof(c));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
a[i]=b[i];
}
sort(b+1,b+n+1);
for(int i=1;i<=n;i++){
if(c[lower_bound(b+1,b+n+1,a[i])-b]++==0)k++;
if(k>m){
while(1){
if(--c[lower_bound(b+1,b+n+1,a[++f])-b]==0){k--;break;}
}
}
}
for(int i=n;i>=1;i--){
if(c[lower_bound(b+1,b+n+1,a[i])-b]){
printf("%d\n",a[i]);
c[lower_bound(b+1,b+n+1,a[i])-b]=0;
k--;
}
if(k==0)break;
}
}
accuracy: 1
time_ms: 30
memory_kb: 4472
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXM=5e5+50;
const double Pi=acos(-1.0);
const ll MOD=1e9+7;
ll a[MAXM];
map<ll,int>mp;

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    int l=0;
    for(int i=1;i<=n;i++)
    {
        bool flag=false;
        if(mp[a[i]])
            flag=true;
        mp[a[i]]++;
        if(i-l>m&&!flag)
        {
            l++;
            mp[a[l]]=0;
        }
    }
    for(int i=n;i>l;i--)
    {
        if(mp[a[i]]==1)
            printf("%lld\n",a[i]);
        mp[a[i]]--;
    }
    return 0;
}
accuracy: 0.1568627450980392
time_ms: 10
memory_kb: 3708
final_rank: 15

code_snippet: //B
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<queue>
#include<set>
#define ld long double 
#define ll long long
#define lll __int128 
//#define int ll
#define eps 1e-11
#define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef pair<int, int> P;

const int maxn = 1e5 + 5;

set<int> t;
int arr[maxn];

signed main()
{
	fastio;
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
	}
	for (int i = n - 1; i >= 0; i--)
	{
		if (m == 0)
			break;
		if (!t.insert(arr[i]).second)
			continue;
		cout << arr[i] << endl;
		m--;
	}
}
accuracy: 1
time_ms: 60
memory_kb: 6012
final_rank: 5

code_snippet: #include<bits/stdc++.h>

using namespace std;

const int maxn=1e5+5;
int ans[maxn];

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&ans[i]);
    }
    if(n>=m)
    {
        for(int i=n;i>=n-m+1;i--)
        {
            printf("%d\n",ans[i]);
        }
    }
    else
    {
        for(int i=n;i>=1;i--)
        {
            printf("%d\n",ans[i]);
        }
    }
    
    getchar();getchar();
    return 0;
}
accuracy: 0.23529411764705882
time_ms: 10
memory_kb: 3480
final_rank: 9

code_snippet: #include <bits/stdc++.h>

using namespace std;

int flag[100010], nxt[100010], lst[100010], a[1000010], b[1000010];

int main()
{
    int n, m, i, s, num, sz = 0, fr = -1, en = -1;
    cin >> n >> m;
    int cnt = 0;
    for(i=1;i<=n;i++)
    {
        cin >> a[i];
        b[i] = a[i];
    }
    sort(b+1,b+1+n);
    s = unique(b+1,b+1+n)-b;
    for(i=1;i<=n;i++)
    {
        num = find(b+1,b+1+s,a[i])-b;
        if(flag[num] && fr != num)
        {
            lst[nxt[num]] = lst[num];
            if(en != num) nxt[lst[num]] = nxt[num];
            else  en = nxt[num];
            nxt[fr] = num;
            lst[num] = fr;
            fr = num;
            nxt[fr] = -1;
        }
        if(!flag[num] && sz < m)
        {
            flag[num] = 1;
            nxt[fr] = num;
            lst[num] = fr;
            fr = num;
            sz++;
        }
        if(!flag[num] && sz == m)
        {
            flag[num] = 1;
            flag[en] = 0;
            en = nxt[en];
            lst[en] = -1;
            nxt[fr] = num;
            lst[num] = fr;
            fr = num;
        }
    }
    while(sz > 0)
    {
        cout << b[fr] << endl;
        fr = lst[fr];
        sz--;
    }
    return 0;
}
accuracy: 1
time_ms: 870
memory_kb: 5080
final_rank: 8

code_snippet: #include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#define maxn 100100
using namespace std;
int a[maxn];
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	map<int,bool>mp;
	for(int i=n-1;m>0&&i>=0;i--)
	{
		if(mp.find(a[i])==mp.end())
		{
			mp[a[i]]=true;
			printf("%d\n",a[i]);
			m--;
		}
	}
}
accuracy: 1
time_ms: 20
memory_kb: 6076
final_rank: 3

code_snippet: #include<bits/stdc++.h>

using namespace std;

const int maxn=1e6+5;
int ans[maxn];
queue <int> s;

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        int a;
        scanf("%d",&a);
        if(s.size()<m)
        {
            s.push(a);
        }
        else
        {
            s.pop();
            s.push(a);
        }
    }
    int cnt=0;
    while(!s.empty())
    {
        ans[cnt++]=s.front();
        s.pop();
    }
    for(int i=cnt-1;i>=0;i--)
    {
        printf("%d\n",ans[i]);
    }
    getchar();getchar();
    return 0;
}
accuracy: 0.23529411764705882
time_ms: 10
memory_kb: 3724
final_rank: 11