Lower_bound upper_bound STL function and usage of the algorithm contest

Before these two functions more exclusive, met need half a scenario is hand-written \ (the while (left <= right) \) .

The decision to turn over records about the usage of these two functions in the algorithm of competition,After all, generally do not result in TLE

In fact, Baidu Encyclopedia has outlined more clearly, and

We assume \ (value \) to a given value,
\ (Lower \ _bound \) is in a ascending sequence in front from the first to find a greater than or equal \ (value \) values,
\ (Upper \ _bound \ ) is an ascending sequence in the first looking from the front and rear is greater than \ (value \) values.

For example: \ (Lower \ _bound (A +. 1, A + n-+. 1, K) \) is the \ (A \) array \ ([1, n] \ ) to find the range (to be in ascending order) in the front to back first greater than or equal \ (K \) values, and returns it as a pointer.

Note that both the return value is a pointer to the original value in the sequence can be inserted, without breaking the first position of the container sequence.
We can add a function pointer values * break before, to obtain the value conceivable.

After seeing this, you should know the meaning of lower and upper two words of it, is about to insert a position value of the sequence still are monotonically strictly increasing and monotonically strictly increasing in.

For chestnut:

    int *p1,*p2;
    int a[6]={3,5,6,6,7,9};
    p1=lower_bound(a,a+6,6);
    p2=upper_bound(a,a+6,6);
    printf("内存地址: p1 -> %d p2 -> %d\n",p1,p2);
    printf("在原序列中找到的值: p1 = %d p2 = %d\n",*p1,*p2);

Operating results as follows:

内存地址: p1 -> 7339512 p2 -> 7339520
在原序列中找到的值: p1 = 6 p2 = 7

If we do not want to use the pointer operation can be written: \ (Lower \ _bound (A + 1, A + the n-+ 1, k) - A \)

Means to find the address of the pointer by subtracting a starting memory address of the array (a memory array is continuous), the position of the value of the subscript a in the array.

    int p1,p2;
    int a[6]={3,5,6,6,7,9};
    p1=lower_bound(a,a+6,6)-a;
    p2=upper_bound(a,a+6,6)-a;
    printf("找出的值的下标: p1 = %d p2 = %d\n",p1,p2);
    printf("找出的值: a[%d] = %d a[%d] = %d\n",p1,a[p1],p2,a[p2]);

Operating results as follows:

找出的值的下标: p1 = 2 p2 = 4
找出的值: a[2] = 6 a[4] = 7



What if we want to find a monotonically decreasing sequence in the first number is less than or equal to a value less than what?

Just make modifications after the cmp incoming \ (lower \ _bound \) or \ (upper \ _bound \) within a function, compares way within the array elements modified.

Usage:

bool cmp(const int &a,const int &b) { return a > b; }
int *p1=lower_bound(a+1,a+n+1,k,cmp);
int *p2=upper_bound(a+1,a+n+1,k,cmp);

Where \ (a \) arrays may also be an array of structures, if embedded overloaded function can not write cmp.

In other words, just let the system know the way this comparison of array elements, in this way compare arrays in a given range is \ (ordered \) , you can use these functions.

If \ (A \) arrays are integer variables, the comparison function can use native C ++ (descending).

\(lower\_bound(a+1,a+n+1,value,greater<int>())\)

Guess you like

Origin www.cnblogs.com/zhwer/p/12232910.html