1 条题解

  • 0
    @ 2025-4-26 18:15:05

    AC

    
    #include <iostream>
    #include <vector>
    #include <queue>
    #include <unordered_set>
    
    using namespace std;
    
    int main() {
        int M, N;
        cin >> M >> N;
        
        vector<int> words(N);
        for (int i = 0; i < N; ++i) {
            cin >> words[i];
        }
        
        queue<int> memoryQueue;
        unordered_set<int> memorySet;
        int dictionaryLookups = 0;
        
        for (const int& word : words) {
            if (memorySet.find(word) == memorySet.end()) {
                // Word not in memory, need to look up in dictionary
                dictionaryLookups++;
                
                if (memoryQueue.size() == M) {
                    // Memory is full, remove the oldest word
                    int oldestWord = memoryQueue.front();
                    memoryQueue.pop();
                    memorySet.erase(oldestWord);
                }
                
                // Add new word to memory
                memoryQueue.push(word);
                memorySet.insert(word);
            }
        }
        
        cout << dictionaryLookups << endl;
        
        return 0;
    }
    
    
    
    
    • 1

    信息

    ID
    353
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    6
    已通过
    4
    上传者