Untitled
unknown
c_cpp
a year ago
956 B
3
Indexable
Never
#include <iostream> #include <queue> #include <set> #include <vector> using namespace std; int main() { int N, rackSize; cin >> N; vector<int> demands(N); for(int i = 0; i < N; i++) { cin >> demands[i]; } cin >> rackSize; queue<int> rack; // Represents the current CDs on the rack in order set<int> rackSet; // Used for quick lookup to see if a CD is on the rack int misses = 0; for(int i = 0; i < N; i++) { if(rackSet.find(demands[i]) == rackSet.end()) { // CD is not on the rack misses++; if(rack.size() == rackSize) { // Rack is full, need to remove a CD int removedCd = rack.front(); rack.pop(); rackSet.erase(removedCd); } rack.push(demands[i]); rackSet.insert(demands[i]); } } cout << misses << endl; return 0; }