Advertisement
chevengur

СПРИНТ № 6 | Просто о сложности. Теория быстродействия | Урок 6: Логарифмическая сложность

Apr 3rd, 2024 (edited)
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template<typename F>
  6. int FindFloor(int n, F m) {
  7.     int low = 1;
  8.     int high = n;
  9.  
  10.     while (low < high) {
  11.         int mid = (high + low) / 2;
  12.         if (m(mid)) {
  13.             high = mid;
  14.         }
  15.         else {
  16.             low = mid + 1;
  17.         }
  18.     }
  19.     return low;
  20. }
  21. int main() {
  22.     int n, t;
  23.     cout << "Enter n and target floor number: "s << endl;
  24.     cin >> n >> t;
  25.     int count = 0;
  26.  
  27.     int found = FindFloor(n, [t, &count](int f) {
  28.         ++count;
  29.         return f >= t;
  30.              });
  31.  
  32.     cout << "Found floor "s << found << " after "s << count << " drops"s;
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement