💻
Algorithm
  • README
  • Array
    • At Most To Equal
    • Count Inversions In An Array
    • Interleaving Placement
    • Kadane
    • Left To Right State Transition
    • Permutation
    • Quick Select
    • Sliding Window
    • Two Pointers
  • Binary Tree
    • Avl Tree
    • Binary Search Tree
    • Serialization And Deserialization
    • Traversal
  • Company
    • Facebook
  • Cpp
    • Array
    • Memset 3 F
    • Overflow
  • Data Structure
    • Binary Indexed Tree
    • Segment Tree And Binary Index Tree
    • Segment Tree
    • Stack
    • Trie
    • Union Find
  • Dynamic Programming
    • Knapsack
      • 0 1 Knapsack
      • Bounded Knapsack
      • Unbounded Knapsack
    • Bitmask Dp
    • Dp On Subsets
    • Dp On Tree
    • Dp With Sorting
    • Selective State Dp
    • Travelling Salesperson
  • Graph
    • Minimum Spanning Tree
      • Kruskal
      • Prim
    • Shortest Path
      • Bellman Ford
      • Dijkstra
      • Floyd Warshall
      • Johnson
      • Shortest Path Faster Algorithm
    • Bi Directional Breadth First Search
    • Bipartite
    • Breadth First Search
    • Component Coloring
    • Component Count
    • Depth First Search
    • Eulerian Path
    • Maximum Bipartite Matching
    • Tarjan
    • Topological Sort
    • Tree Diameter
    • Tree Ring Order Traversal
  • Greedy
    • Greedy Scheduling
    • Regret Greedy
  • Math
    • Catalan Number
    • Combinatorics
    • Factorial
    • Factorization
    • Fast Pow
    • Gcd
    • Geometry
    • Get Digits
    • Lcm
    • Median Minimizes Sum Of Absolute Deviations
    • Mode
    • Modular Multiplicative Inverse
    • Palindrome
    • Prime Number
    • Round Up
    • Sieve Of Eratosthenes
    • Stars And Bars
    • Sum Of Sequence
  • Miscellaneous
    • Bin Packing
    • Floyds Tortoise And Hare
    • Hungarian
    • Palindrome
  • Sort
    • Bubble Sort
    • Cycle Sort
    • Heap Sort
    • Merge Sort
    • Quick Sort
    • Sorting
  • Stl
    • Cpp Stl
    • Istringstream
    • Lower Bound Upper Bound
    • Priority Queue
  • String
    • Kmp
    • Manacher
    • Rabin Karp
    • String Processing
    • Z
  • Backtracking
  • Binary Answer
  • Binary Lifting
  • Binary Search
  • Bit Manipulation
  • Date
  • Difference Array
  • Discretization
  • Divide And Conquer
  • Gray Code
  • Great Problems For Practice
  • Interval Scheduling Maximization
  • Io Optimization
  • K Subset Partitioning
  • Line Sweep
  • Longest Common Subsequence
  • Longest Increasing Subsequence
  • Meet In The Middle
  • Minmax
  • Mono Deque
  • Monotonic Stack
  • Offline Query
  • P And Np
  • Prefix State Map
  • Prefix Sum
  • Random
  • Reservoir Sampling
  • Reverse Polish Notation
  • Sqrt Decomposition
Powered by GitBook
On this page
  • lower_bound and upper_bound
  • lower_bound + comparator
  • upper_bound + comparator
  • Problems

Was this helpful?

  1. Stl

Lower Bound Upper Bound

lower_bound and upper_bound

lower_bound + comparator

struct Item {
    int start, end;
    string string() { return "(" + to_string(start) + "," + to_string(end) + ")"; }
};
int main() {
    /*
    <=
    0   F F F F => A[0] = (10,60)        
    20  T T F F => A[2] = (30,35)
    40  T T T T => not found
    50  T T T T => not found

    <
    0   F F F F => A[0] = (10,60)        
    20  T F F F => A[1] = (20,40)
    40  T T T F => A[3] = (40,50)
    50  T T T T => not found

    >=  (Note that we shouldn't use this operator because this comparator doesn't split the array into TRUE then FALSE parts)
    0   T T T T => not found
    20  F T T T => not found
    40  F F F T => A[0] = (10,60)
    50  F F F F => A[0] = (10,60)
    */
    vector<Item> A = {{10,60},{20,40},{30,35},{40,50}};
    for (int target : {0, 20, 40, 50}) {
        int j = lower_bound(begin(A), end(A), target, [](auto &val, int target) {
            cout << "comparing " << val.string() << " and " << target << endl;
            return val.start >= target; // change this operator to see the above results
        }) - begin(A);
        cout << target << " => ";
        if (j == A.size()) {
            cout << "NOT FOUND" << endl;
        } else {
            cout << "A[" << j << "] = " << A[j].string() << endl;
        }
    }
}

Summary:

  • comparator is in form (element, target).

  • The comparator returns true if the first argument (element) is ordered before the second argument (target). It separates the array into TRUE segment followed by FALSE segment.

  • lower_bound returns the first element returning false.

                 ┌-- the returned index
                 |
      true       v         false
[--------------] [----------------------]

upper_bound + comparator

struct Item {
    int start, end;
    string string() { return "(" + to_string(start) + "," + to_string(end) + ")"; }
};
int main() {
    /*
    <=
    0   T T T T => A[0] = (10,60)        
    20  F T T T => A[1] = (20,40)
    40  F F F T => A[3] = (40,50)
    50  F F F F => not found

    <
    0   T T T T => A[0] = (10,60)        
    20  F F T T => A[2] = (30,35)
    40  F F F F => not found
    50  F F F F => not found

    >= (Note that we shouldn't use this operator because this comparator doesn't split the array into FALSE then TRUE parts)
    0   F F F F => not found
    20  T T F F => not found
    40  T T T T => A[0] = (10,60)
    50  T T T T => A[0] = (10,60)
    */
    vector<Item> A = {{10,60},{20,40},{30,35},{40,50}};
    for (int target : {0, 20, 40, 50}) {
        int j = upper_bound(begin(A), end(A), target, [](int target, auto &val) {
            cout << "comparing " << val.string() << " and " << target << endl;
            return target >= val.start;
        }) - begin(A);
        cout << target << " => ";
        if (j == A.size()) {
            cout << "NOT FOUND" << endl;
        } else {
            cout << "A[" << j << "] = " << A[j].string() << endl;
        }
    }
}

Summary

  • comparator is in form (target, element).

  • The comparator returns true if the first argument (target) is ordered before the second argument (element). It separates the array into FALSE segment followed by TRUE segment.

  • upper_bound returns the first element returning TRUE.

                 ┌-- the returned index
                 |
      false      v        true 
[--------------] [----------------------]

Problems

PreviousIstringstreamNextPriority Queue

Last updated 3 years ago

Was this helpful?

1751. Maximum Number of Events That Can Be Attended II (Hard)