feat: new alternative WA solutions implemented
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": ["wa.cpp"],
|
"wrong-answer": ["wa.cpp", "wa_LFU.cpp", "wa_LRU.cpp"],
|
||||||
"time-limit": [],
|
"time-limit": [],
|
||||||
"time-limit-or-ac": [],
|
"time-limit-or-ac": [],
|
||||||
"time-limit-or-memory-limit": [],
|
"time-limit-or-memory-limit": [],
|
||||||
|
|||||||
49
caching-offline/src/wa_LFU.cpp
Normal file
49
caching-offline/src/wa_LFU.cpp
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
#define MAX_PAGE 1010
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
vector<int> pages;
|
||||||
|
unordered_set<int> cache;
|
||||||
|
vector<int> referencesCount;
|
||||||
|
|
||||||
|
int getLeastFrequentlyUsedPage() {
|
||||||
|
int lfuCount = INT_MAX, lfuPage = -1;
|
||||||
|
for (const int page : cache) {
|
||||||
|
if (referencesCount[page] < lfuCount) {
|
||||||
|
lfuPage = page;
|
||||||
|
lfuCount = referencesCount[page];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lfuPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int k, v; cin >> k >> v;
|
||||||
|
|
||||||
|
pages.resize(v);
|
||||||
|
for (int i = 0; i < v; i++) {
|
||||||
|
cin >> pages[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
int misses = 0;
|
||||||
|
referencesCount.resize(MAX_PAGE, 0);
|
||||||
|
for (int i = 0; i < v; i++) {
|
||||||
|
if (!cache.count(pages[i])) {
|
||||||
|
bool isCacheFull = cache.size() == k;
|
||||||
|
if (isCacheFull) {
|
||||||
|
cache.erase(getLeastFrequentlyUsedPage());
|
||||||
|
}
|
||||||
|
misses++;
|
||||||
|
cache.insert(pages[i]);
|
||||||
|
referencesCount[pages[i]] = 1;
|
||||||
|
} else {
|
||||||
|
referencesCount[pages[i]]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << misses << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
47
caching-offline/src/wa_LRU.cpp
Normal file
47
caching-offline/src/wa_LRU.cpp
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
#define MAX_PAGE 1010
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
vector<int> pages;
|
||||||
|
unordered_set<int> cache;
|
||||||
|
vector<int> lastReference;
|
||||||
|
|
||||||
|
int getLeastRecentlyUsedPage() {
|
||||||
|
int lruTime = INT_MAX, lruPage = -1;
|
||||||
|
for (const int page : cache) {
|
||||||
|
if (lastReference[page] < lruTime) {
|
||||||
|
lruPage = page;
|
||||||
|
lruTime = lastReference[page];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lruPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int k, v; cin >> k >> v;
|
||||||
|
|
||||||
|
pages.resize(v);
|
||||||
|
for (int i = 0; i < v; i++) {
|
||||||
|
cin >> pages[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
int misses = 0;
|
||||||
|
lastReference.resize(MAX_PAGE, -1);
|
||||||
|
for (int i = 0; i < v; i++) {
|
||||||
|
if (!cache.count(pages[i])) {
|
||||||
|
bool isCacheFull = cache.size() == k;
|
||||||
|
if (isCacheFull) {
|
||||||
|
cache.erase(getLeastRecentlyUsedPage());
|
||||||
|
}
|
||||||
|
misses++;
|
||||||
|
cache.insert(pages[i]);
|
||||||
|
}
|
||||||
|
lastReference[pages[i]] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << misses << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user