LocalMax
TCCC05 Finals · 2005-01-10 · by dgoodman
Problem Statement
The sampling will be done sequentially: we choose a value of x and find the corresponding y value, f(x). We repeat this process N times, choosing the x value based on all the data collected so far. We are willing to assume that no two y values will be exactly equal to each other.
Create a class LocalMax that contains a method length that is given N, the
number of samples we can take, and
Notes
- The returned value must be accurate to within a relative or absolute value of 1E-9
Constraints
- N will be between 0 and 25 inclusive.
- xData will contain between 1 and 50 elements inclulsive.
- yData will contain the same number of elements as xData.
- The elements of yData will be distinct.
- The elements of xData will be distinct.
- Each element of xData and yData will be greater than 0.0 and less than 10000.0.
1
{3,4,5}
{3,4,5}
Returns: -1.0
From the data we already have we cannot guarantee that this function even has a local maximum -- it might be monotonically increasing.
0
{3,4,5}
{3,5,4}
Returns: 2.0
We know that there is a local maximum between x=3 and x=5.
2
{3,5,8}
{35,923.2,17}
Returns: 2.0
Here is one way (there are others) get an interval of length 2 or less with just two additional samples. Take the first sample at x=6.5. The worst case is that the corresponding y value is less than 923.2. In that case we know that the interval (3,6.5) contains a local maximum, and that the y value at x=5 is greater than either endpoint y value. Take the second sample at x=4.7. In the worst case the corresponding value of y will be bigger than 923.2, in which case we could conclude that there is a local max somewhere in the interval (3,5).
1
{3,4,6,5}
{32,53,68,47}
Returns: 1.0
If we take a sample at x=4.0+epsilon, for some tiny epsilon, then the worst case would be that the corresponding y value would be smaller than 53. In that case the interval between 3.0 and 4.0+epsilon would contain a local maximum. So by choosing epsilon arbitrarily small, we get an interval whose length is (arbitrarily close to) 1.0.
3
{1,4.3,7.2,95.4,534.0}
{72, 83, 19, 25.3, 624.0}
Returns: 1.4500000000000002
Submissions are judged against all 61 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class LocalMax with a public method double length(int N, vector<double> xData, vector<double> yData) · 61 test cases · 2 s / 256 MB per case