Search in a unimodal vector X82938


Statement
 

pdf   zip   main.cc

In this problem, we say that a vector with nn integer numbers @v[0..@n1n-1@]@ is unimodal if n1n \ge 1, and there exists an index jj such that 0jn10 \leq j \leq n-1 and satisfying:

  • @v[0]@ <<< \ldots < @v[@j1j-1@]@ << @v[@jj@]@, and

  • @v[@jj@]@ >> @v[@j+1j+1@]@ >> @v[@j+2j+2@]@ >>> \ldots > @v[@n1n-1@]@.

For instance, the vector @[0, 2, 5, 7, 6, 5, 4, 3, 1]@ is unimodal (with j=3j = 3).

Note that vectors with n2n \leq 2 different elements are unimodal. In general, note that any strictly increasing vector is also unimodal (and in all cases j=n1j = n-1), and analogously, any strictly decreasing vector is also unimodal (and then j=0j = 0).

Implement an efficient function

    bool search(int x, const vector<int>& v);

such that, given an integer number @x@ and a unimodal vector @v@, returns true if @x@ appears in @v@, and false otherwise. You can use and implement auxiliary functions if you need them.

Precondition

The vector @v@ is unimodal.

Observation

You only need to submit the required procedure; your main program will be ignored.

Information
Author
Prof. EDA
Language
English
Translator
Prof. EDA
Original language
Catalan
Other languages
Catalan
Official solutions
C++
User solutions
C++