21
Binary Search 陳鵬宇 (float) < float.tw @ gmail.com >

[ACM-ICPC] Binary Search

  • Upload
    -

  • View
    310

  • Download
    3

Embed Size (px)

Citation preview

Binary Search

陳鵬宇 (float)< float.tw @ gmail.com >

Rights to CopyAttribution-ShareAlike 3.0

You are free:

– to Share — to copy, distribute and transmit the work

– to Remix — to adapt the work

– to make commercial use of the work

● Under the following conditions:

– Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).

– Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.

– License text: http://creativecommons.org/licenses/by-sa/3.0/legalcode

03/06/13 3

Binary Search● Binary Search

– Search X, Given f(x) and f(X) = Y– f(x) must be Monotonic function (sorted)

● Basic Operation

– Find Value, Max or Min– "canbe" function– beg, mid, end

03/06/13 4

0 1 2 3 4 5 6 7 8 9 10 11 12 13

Example● Find 33

6 13 14 25 33 43 51 65 66 76 78 81 98 99

03/06/13 5

0 1 2 3 4 5 6 7 8 9 10 11 12 13

Example● Find 33

● f(0) = 6, f(1) = 13, f(2) = 14 …

6 13 14 25 33 43 51 65 66 76 78 81 98 99

03/06/13 6

0 1 2 3 4 5 6 7 8 9 10 11 12 13

Example● Find 33

6 13 14 25 33 43 51 65 66 76 78 81 98 99

Beg End

03/06/13 7

0 1 2 3 4 5 6 7 8 9 10 11 12 13

Example● Find 33

6 13 14 25 33 43 6551 66 76 78 81 98 99

Beg EndMid

03/06/13 8

0 1 2 3 4 5 6 7 8 9 10 11 12 13

Example● Find 33

6 13 14 25 33 43 6551 66 76 78 81 98 99

Beg End

03/06/13 9

0 1 2 3 4 5 6 7 8 9 10 11 12 13

Example● Find 33

6 13 3325 43 6551 66 76 78 81 98 99

Beg EndMid

14

03/06/13 10

0 1 2 3 4 5 6 7 8 9 10 11 12 13

Example● Find 33

6 13 14 3325 43 6551 66 76 78 81 98 99

Beg End

03/06/13 11

0 1 2 3 4 5 6 7 8 9 10 11 12 13

Example● Find 33

6 13 14 25 4333 6551 66 76 78 81 98 99

Beg EndMid

03/06/13 12

C/C++ function● C: bsearch

void* bsearch (const void* key, const void* base,

size_t num, size_t size,

int (*compar)(const void*,const void*));

● C++: binary_search template <class ForwardIterator, class T, class Compare>

bool binary_search (ForwardIterator first, ForwardIterator last,

const T& val, Compare comp);

03/06/13 13

maximize minimum valueminimize maximum value

Your job is to find out the minimal possible something which has maximal something.

03/06/13 14

0 1 2 3 4 5 7 8 9 10 11 12 13

Example● Find Max < 60

6 13 14 25 33 43 65 66 76 78 81 98 99

Beg End

6

51

Mid

03/06/13 15

0 1 2 3 4 5 7 8 9 10 11 12 13

Example● Find Max < 60

6 13 14 25 33 43 65 66 76 78 81 98 99

Beg End

6

51

03/06/13 16

0 1 2 3 4 5 7 8 9 10 11 12 13

Example● Find Max < 60

6 13 14 25 33 43 65 66 78 81 98 99

Beg End

6

51

Mid

76

03/06/13 17

0 1 2 3 4 5 7 8 9 10 11 12 13

Example● Find Max < 60

6 13 14 25 33 43 65 66 78 81 98 99

Beg End

6

51 76

03/06/13 18

0 1 2 3 4 5 7 8 9 10 11 12 13

Example● Find Max < 60

6 13 14 25 33 43 66 78 81 98 99

Beg End

6

51 76

Mid

65

03/06/13 19

0 1 2 3 4 5 7 8 9 10 11 12 13

Example● Find Max < 60

6 13 14 25 33 43 66 78 81 98 99

Beg

End

6

51 7665

03/06/13 20

Practice

UVa - 11413

03/06/13 21

Thank You for Your Listening.