# "Brush title record" && "algorithmic talk" binary enumeration with Upper_bound and Lower_bound

Source: Internet
Author: User

"What is Upper_bound and Lower_bound?"

In a nutshell, Lower_bound is you give him a non-descending series [first,last] and X, which gives you the first position in the non-descending sequence, the last, is greater than or equal to the value x .

And Upper_bound is you give him a non-descending sequence [first,last] and X, which gives you a return to the first position in the non-descending order of the number of values greater than the value x .

The algorithm for implementing these two functions in STL is two-point ...

"Upper_bound and Lower_bound Code"

`Lower_bound source code in STL//In this algorithm, first is the final position to return int lower_bound (int *array, int size, int key) {int first = 0, middle;    int half, Len;    len = size;        while (Len > 0) {half = Len >> 1;        Middle = first + half;                      if (Array[middle] < key) {first = middle + 1;   len = len-half-1;      Find in the right sub-sequence} else len = half; In the left sub-sequence (contains middle), look for} return first;}    —————————— upper_bound —————————————————— int upper_bound (int *array, int size, int key) {int first = 0, len = size-1;    int half, middle;        while (Len > 0) {half = Len >> 1;        Middle = first + half;            if (Array[middle] > key)//The median is greater than key and is found in the left half of the sequence containing the last.        Len = half;    else{first = middle + 1;            The median is less than or equal to key and is found in the right half of the sequence.        len = len-half-1; }} return first;} ______________end___________________________________________________________`

"POJ 2785"

"Original title"

The SUM problem can formulated as Follows:given four lists A, B, C, D of an integer values, compute how many quadruplet ( A, B, C, D) ∈a x B x C x D is such that A + B + c + d = 0. In the following, we assume this all lists has the same size n.

"The main topic"

A a,b,c,d of 4 series with n integers given. To each number in each sequence, make four numbers and 0. Find out the number of such combinations. (When you have the same number in the same series, you see it in different numbers--Bo main note)

"Input description"

There are n rows, a row of 4 numbers, respectively A[i],b[i],c[i],d[i]

"Input Sample"

`6-45 22 42-16-41-27 56 30-36 53-37 77-36 30-75-4626-38-10 62-32-54-6 45`

"Output description"

A number

"Output Example"

`5`

"Bo Master Notes"

There are 5 kinds of cases, namely -45-27+42+30 26+30-10-46-32+22+56-46-32+30-75+77-32-54+56+36

"Problem Analysis"

We divide these numbers into AB and CDs. Remove A[i],b[i] From AB first, in order to make the sum of 0 need to be removed from the CD C[i]+d[i]=a[i]-d[i]. So enumerate these cases, and then use Upper_bound and Lower_bound for two points. Time Complexity of O (n^2 logn)

Code

`#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;const int Maxn=4001;int N;int a[maxn],b[maxn],c[maxn],d[maxn];int cd[16000001];int lower_bound (int *array, int size, int key) {int first = 0, M    Iddle;    int half, Len;    len = size;        while (Len > 0) {half = Len >> 1;        Middle = first + half;                      if (Array[middle] < key) {first = middle + 1;   len = len-half-1;      Find in the right sub-sequence} else len = half; In the left sub-sequence (contains middle), look for} return first;}    int upper_bound (int *array, int size, int key) {int first = 0, len = size-1;    int half, middle;        while (Len > 0) {half = Len >> 1;        Middle = first + half;    if (Array[middle] > key) len = half;        The median is greater than key and is found in the left half of the sequence that contains the last.    else{first = middle + 1;            The median is less than or equal to key and is found in the right half of the sequence.        len = len-half-1; }} return first;} int main () {CIN&GT;&Gt;n;for (int i=0;i<n;i++) cin>>a[i]>>b[i]>>c[i]>>d[i];   for (int i=0;i<n;i++) cin>>b[i];   for (int i=0;i<n;i++) cin>>c[i]; for (int i=0;i<n;i++) cin>>d[i];  for (int i=0;i<n;i++) {for (int j=0;j<n;j++) cd[i*n+j]=c[i]+d[j];} Sort (cd,cd+n*n); long long res=0;for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {int cd=-(A[I]+B[J]        ); Res+=upper_bound (CD,CD+N*N,CD)-lower_bound (CD,CD+N*N,CD);}} Cout<<res;return 0;}`

"Brush title record" && "algorithmic talk" binary enumeration with Upper_bound and Lower_bound

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.