feat: new WA solution added.

This commit is contained in:
2026-02-03 19:46:17 -03:00
parent 53ce2e4730
commit 3aa9fe7c42
3 changed files with 54 additions and 1 deletions

Binary file not shown.

View File

@@ -44,7 +44,7 @@
"solutions": {
"main-ac": "ac.cpp",
"alternative-ac": [],
"wrong-answer": [],
"wrong-answer": ["wa.cpp"],
"time-limit": [],
"time-limit-or-ac": [],
"time-limit-or-memory-limit": [],

View File

@@ -0,0 +1,53 @@
#include <bits/stdc++.h>
#define MAX_PAGE 1010
using namespace std;
vector<int> pageReferencesCount;
vector<int> pages;
unordered_set<int> cache;
int getPageWithLeastReferenceCount() {
int count = INT_MAX, leastReferencedPage = -1;
for (const int page : cache) {
// page has no more future references, so remove it
if (pageReferencesCount[page] <= 0) {
return page;
}
if (pageReferencesCount[page] < count) {
count = pageReferencesCount[page];
leastReferencedPage = page;
}
}
// otherwise, remove page with the least number of future references
return leastReferencedPage;
}
int main(){
int k, v; cin >> k >> v;
pages.resize(v);
pageReferencesCount.resize(MAX_PAGE);
for (int i = 0; i < v; i++) {
cin >> pages[i];
pageReferencesCount[pages[i]]++;
}
int misses = 0;
for (int i = 0; i < v; i++) {
if (!cache.count(pages[i])) {
bool isCacheFull = cache.size() == k;
if (isCacheFull) {
cache.erase(getPageWithLeastReferenceCount());
}
misses++;
cache.insert(pages[i]);
}
pageReferencesCount[pages[i]]--;
}
cout << misses << endl;
return 0;
}