From 3aa9fe7c4200ea924da3ce4487c5106f5d49d95b Mon Sep 17 00:00:00 2001 From: arthur Date: Tue, 3 Feb 2026 19:46:17 -0300 Subject: [PATCH] feat: new WA solution added. --- caching-offline/caching-offline.pdf | Bin 99709 -> 99709 bytes caching-offline/problem.json | 2 +- caching-offline/src/wa.cpp | 53 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 caching-offline/src/wa.cpp diff --git a/caching-offline/caching-offline.pdf b/caching-offline/caching-offline.pdf index 9f4db23b2fbd333a300605b6b15338d705cbb0cd..5ed04af53bcd036f2dc95fddce0768ca9fce7efd 100644 GIT binary patch delta 120 zcmey{#rC(0tziqJSRteFcJV^SKqhu$LrX(bQ;X^O#f&mA&h|;gj63+8on1^#ER0Pp oT%8Rp+>Bh@Tus~z&0L+GEewo|UCb;@?G$VXDcRoJ#Q1>;0MnNr`2YX_ delta 120 zcmey{#rC(0tziqJSRtd)cJV^SKqhu0Lkkm2Q`71B#f&mA&h|;gj63+8Or_O&kqP94#%)-OP;0M1Sy;s5{u 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