Serie: Algorithms
cpp
43 líneas
· Actualizado 2026-04-05
fifo_cache.cpp
Algorithms/fifo_cache.cpp
#include <iostream>
#include <queue>
#include <set>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int m, n;
cin >> m >> n;
queue<int> fifo;
set<int> memory;
int count = 0;
for (int i = 0; i < n; i++) {
int word;
cin >> word;
if (memory.count(word)) {
// word already in memory
continue;
}
// need to look up external memory
count ++;
if ((int)memory.size() == m) {
// memory is full, remove earliest stored word
int oldest = fifo.front();
fifo.pop();
memory.erase(oldest);
}
// store new word in memory
fifo.push(word);
memory.insert(word);
}
cout << count << endl;
return 0;
}
Artículos relacionados
Algorithms
java
Actualizado 2026-03-02
#include <iostream>.java
#include <iostream>.java — java source code from the Algorithms learning materials (Algorithms/#include <iostream>.java).
Leer artículo →
Algorithms
cpp
Actualizado 2026-04-07
748.cpp
748.cpp — cpp source code from the Algorithms learning materials (Algorithms/748.cpp).
Leer artículo →
Algorithms
cpp
Actualizado 2026-04-07
827.cpp
827.cpp — cpp source code from the Algorithms learning materials (Algorithms/827.cpp).
Leer artículo →
Algorithms
cpp
Actualizado 2026-04-07
827_best_greedy.cpp
827_best_greedy.cpp — cpp source code from the Algorithms learning materials (Algorithms/827_best_greedy.cpp).
Leer artículo →
Algorithms
cpp
Actualizado 2026-04-07
8402.cpp
8402.cpp — cpp source code from the Algorithms learning materials (Algorithms/8402.cpp).
Leer artículo →
Algorithms
cpp
Actualizado 2026-04-07
860.cpp
860.cpp — cpp source code from the Algorithms learning materials (Algorithms/860.cpp).
Leer artículo →