Advertisement
georgiy110802

Untitled

Dec 18th, 2021
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     ios::sync_with_stdio(0);
  8.     cin.tie(0);
  9.     int n, m;
  10.     cin >> n >> m;
  11.     vector<int> a(n), b(m);
  12.     for (int &i : a)
  13.         cin >> i;
  14.     for (int &i : b)
  15.         cin >> i;
  16.     sort(a.begin(), a.end());
  17.     sort(b.begin(), b.end());
  18.     int ind_a = 0, ind_b = 0;
  19.     vector<int> ans;
  20.     while (ind_a < n && ind_b < m)
  21.     {
  22.         if (a[ind_a] == b[ind_b])
  23.         {
  24.             if (ans.empty() || ans.back() != a[ind_a])
  25.                 ans.push_back(a[ind_a]);
  26.             ind_b += 1;
  27.             ind_a += 1;
  28.         }  
  29.         else if (a[ind_a] < b[ind_b])
  30.             ind_a += 1;
  31.         else
  32.             ind_b += 1;
  33.     }
  34.     for (int i : ans)
  35.         cout << i << " ";
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement