TopcoderArchive
SRM6 Jul 2006
SRM 310

MeasuringTemperature

Problem statement, definition, constraints, and public examples.

Problem Statement

You have a device that measures temperature once a minute. Sadly, the temperature sensor of your device is not 100% reliable, and sometimes it reports wildly inaccurate values.

You don't have the resources to buy a new device so you decide to fix the problem by writing software for your device that will throw away invalid measurements. We consider a measurement invalid if:

  • the value is less than -273, or
  • each value that was measured within 2 minutes before and after this one differs from this value by more than 2

In other words, when deciding whether a measurement is valid, you usually consider the previous two, and the next two measurements.

You are given a int[] measuredValues that contains temperatures measured in the last few minutes. The temperatures are given in chronological order, i.e., the i-th value is the temperature measured i minutes after the device was started. Write a method that computes the average of the valid measurements. If no measurement is valid, return -300.0.

Definition

Class:
MeasuringTemperature
Method:
averageTemperature
Parameters:
int[]
Returns:
double
Method signature:
double averageTemperature(int[] measuredValues)
(be sure your method is public)

Notes

  • The returned value must be accurate to within a relative or absolute error of 1E-9.
  • The lowest possible temperature (in degrees Celsius) is -273.15.
  • It may seem weird that only integer temperatures are used in the input. This is only done to avoid rounding errors.

Constraints

  • measuredValues contains between 2 and 50 elements, inclusive.
  • Each element of measuredValues will be between -1000 and 1000, inclusive.

Examples

  1. {9, 11, 12, 13, 15}
    Returns: 12.0
    All measurements are valid. During this period of time it was getting warmer. The average temperature is (9+11+12+13+15)/5 = 12.
  2. {0, 0, 0, 2, 997, -1, 0}
    Returns: 0.16666666666666666
    The fifth measurement is clearly invalid. The average of the valid ones is slightly positive.
  3. {0, 0, 0, 2, -4, -1, 0}
    Returns: 0.16666666666666666
    This time, the fifth measurement is only slightly off, but still we consider it to be invalid.
  4. {0, 0, 0, 2, -3, -1, 0}
    Returns: -0.2857142857142857
    All the measurements are valid.
  5. {1,2,3,100,100,1,2}
    Returns: 29.857142857142858
    Again, all these measurements are valid. (Sadly, the sensor malfunctioned twice in a row. Our approach can't deal with this situation.)
  6. {1,2,3,4,5,6,7,10}
    Returns: 4.0
    The last measurement is invalid. (The measurements made within 2 minutes from the last measurement gave results 6 and 7, and neither of these values is close enough to 10.)
  7. {-35, -34, -34, -34, -35, 72, -34, 52, -36, -35, -36, 52, -36, -35, 981, -33}
    Returns: -34.75
    It's freezing cold and the sensor is malfunctioning quite often, but luckily we can identify all the wrong measurements (72, 52, 52, and 981) as invalid.
  8. {-273, -273, -274, -273}
    Returns: -273.0
    The third measurement gives a temperature lower than -273, and thus is considered to be invalid.
  9. {10, 20, 30, 40}
    Returns: -300.0
    No valid measurements here.
Back to all problems