feat: new greedy problem partially implemented.
This commit is contained in:
52
caching-offline/src/ac.cpp
Normal file
52
caching-offline/src/ac.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
#define MAX_PAGE 1010
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<vector<int>> pageReferences;
|
||||
vector<int> pageReferencesIndex;
|
||||
vector<int> pages;
|
||||
unordered_set<int> cache;
|
||||
|
||||
int getPageWithFarthestReference() {
|
||||
int farthestIndex = INT_MIN;
|
||||
for (const int page : cache) {
|
||||
// page has no more future references, so remove it
|
||||
if (pageReferencesIndex[page] >= pageReferences[page].size()) {
|
||||
return page;
|
||||
}
|
||||
farthestIndex = max(farthestIndex, pageReferences[page][pageReferencesIndex[page]]);
|
||||
}
|
||||
// otherwise, remove page in the farthest index
|
||||
return pages[farthestIndex];
|
||||
}
|
||||
|
||||
int main(){
|
||||
int k, v; cin >> k >> v;
|
||||
|
||||
pages.resize(v);
|
||||
pageReferences.resize(MAX_PAGE);
|
||||
for (int i = 0; i < v; i++) {
|
||||
cin >> pages[i];
|
||||
pageReferences[pages[i]].push_back(i);
|
||||
}
|
||||
|
||||
pageReferencesIndex.resize(MAX_PAGE, 0);
|
||||
int misses = 0;
|
||||
for (int i = 0; i < v; i++) {
|
||||
if (!cache.count(pages[i])) {
|
||||
bool isCacheFull = cache.size() == k;
|
||||
if (isCacheFull) {
|
||||
cache.erase(getPageWithFarthestReference());
|
||||
}
|
||||
misses++;
|
||||
cache.insert(pages[i]);
|
||||
}
|
||||
pageReferencesIndex[pages[i]]++;
|
||||
}
|
||||
|
||||
cout << misses << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
17
caching-offline/src/checker.cpp
Normal file
17
caching-offline/src/checker.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
setName("compare two signed int%d's", 8 * int(sizeof(int)));
|
||||
registerTestlibCmd(argc, argv);
|
||||
|
||||
int ja = ans.readInt();
|
||||
int pa = ouf.readInt();
|
||||
|
||||
if (ja != pa)
|
||||
quitf(_wa, "expected %d, found %d", ja, pa);
|
||||
|
||||
quitf(_ok, "all answers are correct");
|
||||
}
|
||||
99
caching-offline/src/generator.cpp
Normal file
99
caching-offline/src/generator.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MIN_K = 1;
|
||||
const int MAX_K = 1000;
|
||||
const int MIN_V = 1;
|
||||
const int MAX_V = 1000;
|
||||
const int MIN_VI = 1;
|
||||
const int MAX_VI = 1000;
|
||||
|
||||
const int rnd_test_n = 100;
|
||||
|
||||
template <typename T> void append(vector<T> &dest, const vector<T> &orig) {
|
||||
dest.insert(dest.end(), orig.begin(), orig.end());
|
||||
}
|
||||
|
||||
string output_tc(int k, int v, const vector<int> &pages) {
|
||||
ostringstream oss;
|
||||
oss << k << " " << v << endl;
|
||||
for (int i = 0; i < pages.size(); i++) {
|
||||
if (i != 0) oss << " ";
|
||||
oss << pages[i];
|
||||
}
|
||||
oss << endl;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
vector<string> generate_sample_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc(1, 3, {1, 2, 3}));
|
||||
tests.push_back(output_tc(2, 5, {1, 2, 3, 1, 1}));
|
||||
tests.push_back(output_tc(5, 10, {1, 2, 3, 4, 5, 5, 4, 3, 2, 1}));
|
||||
return tests;
|
||||
}
|
||||
|
||||
vector<string> generate_manual_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc(2, 11, {1, 2, 3, 2, 3, 2, 3, 1, 1, 1, 1}));
|
||||
return tests;
|
||||
}
|
||||
|
||||
vector<int> generateRandomArray(int size) {
|
||||
vector<int> arr(size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = rnd.next(MIN_VI, MAX_VI);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
string rnd_test(int i){
|
||||
int min_k = MIN_K;
|
||||
int max_k = MAX_K;
|
||||
|
||||
if(i<rnd_test_n / 3){
|
||||
max_k = 5;
|
||||
}
|
||||
else if(i<rnd_test_n / 2){
|
||||
max_k = 20;
|
||||
}
|
||||
|
||||
int k = rnd.next(min_k, max_k);
|
||||
int v = rnd.next(MIN_V, MAX_V);
|
||||
return(output_tc(k, v, generateRandomArray(v)));
|
||||
}
|
||||
|
||||
vector<string> generate_random_tests() {
|
||||
vector<string> tests;
|
||||
for (int i = 0; i < rnd_test_n; i++){
|
||||
tests.push_back(rnd_test(i));
|
||||
}
|
||||
return tests;
|
||||
}
|
||||
|
||||
string extreme_test_1(){
|
||||
return (output_tc(MAX_K, MAX_V, generateRandomArray(MAX_V)));
|
||||
}
|
||||
|
||||
vector<string> generate_extreme_tests(){
|
||||
vector<string> tests;
|
||||
tests.push_back(extreme_test_1());
|
||||
return tests;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
registerGen(argc, argv, 1);
|
||||
vector<string> tests;
|
||||
size_t test = 0;
|
||||
append(tests, generate_sample_tests());
|
||||
append(tests, generate_manual_tests());
|
||||
append(tests, generate_random_tests());
|
||||
append(tests, generate_extreme_tests());
|
||||
for (const auto &t : tests) {
|
||||
startTest(++test);
|
||||
cout << t;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
1
caching-offline/src/script.sh
Normal file
1
caching-offline/src/script.sh
Normal file
@@ -0,0 +1 @@
|
||||
generator
|
||||
5963
caching-offline/src/testlib.h
Normal file
5963
caching-offline/src/testlib.h
Normal file
File diff suppressed because it is too large
Load Diff
22
caching-offline/src/validator.cpp
Normal file
22
caching-offline/src/validator.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
registerValidation(argc, argv);
|
||||
inf.readInt(1, 1000, "k");
|
||||
inf.readSpace();
|
||||
int v = inf.readInt(1, 1000, "v");
|
||||
inf.readEoln();
|
||||
|
||||
for (int i = 0; i < v; i++) {
|
||||
if (i != 0) inf.readSpace();
|
||||
int vi = inf.readInt(1, 1000, "vi");
|
||||
}
|
||||
inf.readEoln();
|
||||
inf.readEof();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user