diff --git a/caching-offline/caching-offline.pdf b/caching-offline/caching-offline.pdf index 9f4db23..5ed04af 100644 Binary files a/caching-offline/caching-offline.pdf and b/caching-offline/caching-offline.pdf differ diff --git a/caching-offline/problem.json b/caching-offline/problem.json index d55a823..52eb09d 100644 --- a/caching-offline/problem.json +++ b/caching-offline/problem.json @@ -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": [], diff --git a/caching-offline/src/wa.cpp b/caching-offline/src/wa.cpp new file mode 100644 index 0000000..ecdd702 --- /dev/null +++ b/caching-offline/src/wa.cpp @@ -0,0 +1,53 @@ +#include + +#define MAX_PAGE 1010 + +using namespace std; + +vector pageReferencesCount; +vector pages; +unordered_set 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; +} \ No newline at end of file