fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define endl '\n'
  4. #define ll long long int
  5. #define ull unsigned long long int
  6. #define ld long double
  7. #define loop(a,b,c) for(ll i=a;i<=b;i+=c)
  8. #define intarr(arr,n) ll arr[n];for(ll i=0;i<n;i++)cin>>arr[i]
  9. #define inparr(arr,n) for(ll i=0;i<n;i++)cin>>arr[i]
  10. #define inpvec(vec,n) for(ll i=0;i<n;i++){ll var;cin>>var;vec.push_back(var);}
  11. #define pb push_back
  12. #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
  13. #define mod 1000000007
  14. #define newline cout<<endl
  15. #define ump unordered_map<ll,ll>
  16. #define vec vector<ll>
  17. #define mkp make_pair
  18. #define disp(var1,var2) cout<<var1<<" "<<var2<<endl;
  19. #define all(v) v.begin(),v.end()
  20. #define cout(var) cout<<var<<endl
  21. #define p 998244353
  22. #define teensort(a,r,g,b) a[0]=r;a[1]=g;a[2]=b;sort(a,a+3);
  23. ll a[200005];
  24. void AcDegaYe()
  25. {
  26. ll n,k;
  27. cin>>n>>k;
  28. k=min(k,n);
  29. for(ll i=0;i<n;i++)
  30. cin>>a[i];
  31. list<ll> v;
  32. ump h;
  33. for(ll i=0;i<n;i++)
  34. {
  35. if(v.size()<k)
  36. {
  37. if(h.count(a[i])==0)
  38. {
  39. v.push_front(a[i]);
  40. h[a[i]]=1;
  41. }
  42. else
  43. continue;
  44. }
  45. else
  46. {
  47. if(h.count(a[i])==1)
  48. {
  49. continue;
  50. }
  51. else
  52. {
  53. h.erase(v.back());
  54. v.pop_back();
  55. v.push_front(a[i]);
  56. h[a[i]]=1;
  57.  
  58. }
  59. }
  60. }
  61. cout(v.size());
  62. for(auto it:v)
  63. {
  64. cout<<it<<" ";
  65. }
  66. cout<<endl;
  67. }
  68.  
  69.  
  70. int main()
  71. {
  72. fastio
  73. //ll t;
  74. //cin>>t;
  75. ll t=1;
  76. while(t--)
  77. {
  78. AcDegaYe();
  79. }
  80. cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  81. return 0;
  82. }
Success #stdin #stdout #stderr 0s 4300KB
stdin
Standard input is empty
stdout
0

stderr
Time elapsed: 3ms