AverageVarianceSubset
SRM 712 · 2017-02-20 · by cgy4ever
Problem Statement
- Let mu = (x_1 + ... + x_n) / n be the mean of the set.
- Let y_i = (x_i - mu)^2 be the square of the difference between x_i and the mean.
- The variance of X, denoted var(X), can now be computed as the average of all y_i. (In other words, as the sum of all y_i, divided by n.)
The range of a nonempty finite set is the difference between its maximum and its minimum. For example, the range of the set { 40, 51, 67, 70 } is 70 - 40 = 30.
You are given a
Consider all nonempty subsets of s with range less than or equal to R. Alice computed the variance of each of those subsets. Bob took all Alice's results and computed their average. Compute and return the number computed by Bob.
Notes
- The returned value must have an absolute or relative error less than 1e-9.
Constraints
- s will contain between 1 and 50 elements, inclusive.
- Elements in s will be distinct.
- Each element in s will be between 1 and 1,000,000,000, inclusive.
- R will be between 0 and 1,000,000,000, inclusive.
{1,2,3}
1
Returns: 0.1
This set has seven nonempty subsets. Out of those, five have range at most 1: {1}, {2}, {3}, {1,2}, and {2,3}. Alice computed the variance of each of these subsets: The variance of {1} is 0. The variance of {2} is 0. The variance of {3} is 0. The variance of {1,2} is 1/4. The variance of {2,3} is 1/4. Bob took Alice's results and computed their average: (0 + 0 + 0 + 1/4 + 1/4) / 5 = 1/10.
{1,2,3}
3
Returns: 0.3095238095238096
This time Alice will consider all seven nonempty subsets. The two new subsets: The variance of {1,3} is 1. The variance of {1,2,3} is 2/3. Bob will then compute the value (0+0+0+1/4+1/4+1+2/3)/7 = 13/42.
{5,1,3,2}
1000000000
Returns: 1.2476851851851847
{1,11,111,1111,11111,111111,1111111,11111111,111111111}
123456
Returns: 1.1349430459217174E9
{1,11,111,1111,11111,111111,1111111,11111111,111111111}
999999999
Returns: 9.989198236452121E14
{1,1000000000}
1000000000
Returns: 8.3333333166666672E16
Note that the answer can be very large.
Submissions are judged against all 68 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class AverageVarianceSubset with a public method double average(vector<int> s, int R) · 68 test cases · 2 s / 256 MB per case