I have some kind of real life walls which are characterized by two heights (leftmost and rightmost height).
e.g.
I I I I
I I I I I
I I I I I I
I_____I I_____I I____I
the first one has leftmost height hl=4, rightmost height hr=3, the second hl=4 and hr=4 and so on.
Given hl and hr my task is now to find the wall loosing minimal volume in order to reach hl and hr. So (a) only lowering the heights on either side of the wall is allowed but not increasing and (b) the lost volume should be minimal.
In a first approach I've reduced the problem to one height using the minimal height hMin=std::min(hl,hr). By doing so I can fill a map with the "walls" and use hMin as keys and in return get the solution using lower_bound searching with max(hl,hr).
Now considering the two heights optimal solution I'm getting into all sorts of trouble constructing a strict weak order. What I have tried until now is to extend the key for a 2nd height, use a custom less and use equivivalently lower_bound. My custom less looks somewhat like:
struct KeyLess
{
bool operator(Key const&x, Key const&y) const
{
if ((x.hl + roundOff < y.hl ) && (x.hr+ roundOff < y.hr))
return true;
if ((y.hl + roundOff < x.hl ) && (y.hr+ roundOff < x.hr))
return false;
return false;
}
};
but obviously has problems for x.hl> y.hl and x.hr < y.hr or visually for these types
I I
I I I I
I I I I I I
I_____I I_____I I____I
and does not give a strict weak ordering afaik.
I would appreciate any help constructing a less operator for my problem or showing me another way of finding a solution to this problem.
Example
I I I I I I
I I I I I I I I
I I I I I I I I
I I I I I I I I I
I_____I I_____I I____I I_____I I_____I
Given hl=3 and hr=5 it should return the 3rd (hl=4 and hr=5).
The order the walls are saved in the map is not per se relevant as long as I can get to the solution (But I think that is also my problem to find a meaningful ordering here).