Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- int n, k;
- std::queue<int> q;
- std::set<int> cur;
- int main(){
- std::cin >> n >> k;
- for(int i =0 ;i < n; i++){
- int id;
- std::cin >> id;
- if(!cur.count(id)){
- if((int)(q.size()) >= k){
- int x = q.front();
- q.pop();
- cur.erase(x);
- }
- cur.insert(id);
- q.push(id);
- }
- }
- std::vector<int> ans;
- while(!q.empty()){
- ans.push_back(q.front());
- q.pop();
- }
- reverse(ans.begin(), ans.end());
- std::cout << ans.size() << std::endl;
- for(auto i : ans){
- std::cout << i << " ";
- }
- std::cout << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment