jainbot27

ehhhh

Jul 21st, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. int n, k;
  3. std::queue<int> q;
  4. std::set<int> cur;
  5. int main(){
  6.     std::cin >> n >> k;
  7.     for(int i =0 ;i < n; i++){
  8.         int id;
  9.         std::cin >> id;
  10.         if(!cur.count(id)){
  11.             if((int)(q.size()) >= k){
  12.                 int x = q.front();
  13.                 q.pop();
  14.                 cur.erase(x);
  15.             }
  16.             cur.insert(id);
  17.             q.push(id);
  18.         }
  19.     }
  20.     std::vector<int> ans;
  21.     while(!q.empty()){
  22.         ans.push_back(q.front());
  23.         q.pop();
  24.     }
  25.     reverse(ans.begin(), ans.end());
  26.     std::cout << ans.size() << std::endl;
  27.     for(auto i : ans){
  28.         std::cout << i << " ";
  29.     }
  30.     std::cout << std::endl;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment