shushus

Спринт 6. Улучшаем сложность.

Dec 25th, 2022
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. #include <random>
  5.  
  6. using namespace std;
  7.  
  8. bool TestPermut(const vector<int>& v1, const vector<int>& v2) {
  9.     if (v1.size() != v2.size()) {
  10.         return false;
  11.     }
  12.     vector<int> v11 = v1;
  13.     vector<int> v22 = v2;
  14.     sort(v11.begin(), v11.end());
  15.     sort(v22.begin(), v22.end());
  16.     return v11 == v22;
  17. }
  18.  
  19. int main() {
  20.     std::mt19937 g;
  21.  
  22.     int n;
  23.     cin >> n;
  24.     vector<int> v1, v2;
  25.     v1.reserve(n);
  26.     v2.reserve(n);
  27.  
  28.     for (int i = 0; i < n; ++i) {
  29.         v1.push_back(rand());
  30.         v2.push_back(rand());
  31.     }
  32.  
  33.     cout << "Random vectors match? "s << flush;
  34.     cout << (TestPermut(v1, v2) ? "Yes"s : "No"s) << endl;
  35.  
  36.     v2 = v1;
  37.     shuffle(v2.begin(), v2.end(), g);
  38.     cout << "Permuted vectors match? "s << flush;
  39.     cout << (TestPermut(v1, v2) ? "Yes"s : "No"s) << endl;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment