1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <vector>
#include <iostream>
// stores the minimum value in range [start, start+k] in the position output+k
template<class inIter, class outIter>
void minimum_so_far( inIter begin, inIter end, outIter output ){
for(inIter min = begin; begin not_eq end; ++begin, ++output){
if( *begin < *min )
min = begin;
*output = *min;
}
}
int main(int argc, char **argv){
std::vector<int>
values = {7, 3, 3, 2, 6, 1, 0, 3, 9, 7},
left(values.size()),
right(values.size());
minimum_so_far( values.begin(), values.end(), left.begin() );
minimum_so_far( values.rbegin(), values.rend(), right.rbegin() );
for( auto x : left )
std::cout << x << ' ';
std::cout << '\n';
for( auto x : right )
std::cout << x << ' ';
std::cout << '\n';
return 0;
}
|
7 3 3 2 2 1 0 0 0 0
0 0 0 0 0 0 0 3 7 7 | |