feat: new WA solution added.
This commit is contained in:
Binary file not shown.
@@ -44,7 +44,7 @@
|
|||||||
"solutions": {
|
"solutions": {
|
||||||
"main-ac": "ac.cpp",
|
"main-ac": "ac.cpp",
|
||||||
"alternative-ac": [],
|
"alternative-ac": [],
|
||||||
"wrong-answer": [],
|
"wrong-answer": ["wa.cpp"],
|
||||||
"time-limit": [],
|
"time-limit": [],
|
||||||
"time-limit-or-ac": [],
|
"time-limit-or-ac": [],
|
||||||
"time-limit-or-memory-limit": [],
|
"time-limit-or-memory-limit": [],
|
||||||
|
|||||||
53
caching-offline/src/wa.cpp
Normal file
53
caching-offline/src/wa.cpp
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user