Advertisement
chevengur

СПРИНТ № 6 | Просто о сложности. Теория быстродействия | Урок 10: Не худший случай

Apr 8th, 2024
627
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <cstdint>
  4. #include <iostream>
  5. #include <random>
  6. #include <vector>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11. int EffectiveCount(const std::vector<int>& v, int n, int i) {
  12.     if (log2(v.size()) > static_cast<int64_t>(v.size()) * (i + 1) / (n + 1)) {
  13.         auto beg_find = std::find_if(v.begin(), v.end(), [&i](int x) {
  14.             return x > i;
  15.             });
  16.         cout << "Using find_if" << endl;
  17.         return (beg_find - v.begin());
  18.     }
  19.     else {
  20.         auto beg_upper = std::upper_bound(v.begin(), v.end(), i);
  21.         cout << "Using upper_bound" << endl;
  22.         return (beg_upper - v.begin());
  23.     }
  24. }
  25.  
  26. int main() {
  27.     static const int NUMBERS = 1'000'000;
  28.     static const int MAX = 1'000'000'000;
  29.  
  30.    mt19937 r;
  31.    uniform_int_distribution<int> uniform_dist(0, MAX);
  32.  
  33.    vector<int> nums;
  34.    for (int i = 0; i < NUMBERS; ++i) {
  35.        int random_number = uniform_dist(r);
  36.        nums.push_back(random_number);
  37.    }
  38.    sort(nums.begin(), nums.end());
  39.  
  40.    int i;
  41.    cin >> i;
  42.    int result = EffectiveCount(nums, MAX, i);
  43.    cout << "Total numbers before "s << i << ": "s << result << endl;
  44. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement