Connection Status:
Competition Arena > WorkingRabbits
SRM 542 · 2011-11-22 · by lyrically · Simple Search, Iteration
Class Name: WorkingRabbits
Return Type: double
Method Name: getEfficiency
Arg Types: (vector<string>)
Problem Statement

Problem Statement

N rabbits (numbered 0 through N - 1) are going to work together at the new TopCoder office in Rabbitland.

Each pair of rabbits will make a certain profit when they work together. The efficiency of the group of rabbits is defined as follows: Let P be the sum of profits from all pairs of rabbits, and Q be the number of pairs of rabbits. Then the efficiency is the real number P / Q.

You are given a String[] profit, the j-th character of the i-th element of which represents the profit from the fact that rabbits i and j work together. The characters '0', '1', ..., '9' represent the values 0, 1, ..., 9, respectively. Compute and return the efficiency of this TopCoder office.

Notes

  • The returned value must have an absolute or relative error less than 1e-9.

Constraints

  • profit will contain between 2 and 50 elements, inclusive.
  • Each element of profit will contain exactly N characters, where N is the number of elements in profit.
  • Each character in each element of profit will be a digit ('0' - '9').
  • For each index i and j, the i-th character of the j-th element of profit will be equal to the j-th character of the i-th element of profit.
  • For each index i, the i-th character of the i-th element of profit will be '0'.
Examples
0)
{ "071", 
  "702", 
  "120" }
Returns: 3.3333333333333335

There are three pairs of rabbits: rabbit 0 and rabbit 1 (profit 7) rabbit 0 and rabbit 2 (profit 1) rabbit 1 and rabbit 2 (profit 2) So P = 7 + 1 + 2, Q = 3, and the efficiency is P / Q = 10/3.

1)
{ "00", 
  "00" }
Returns: 0.0
2)
{ "0999", 
  "9099", 
  "9909", 
  "9990" }
Returns: 9.0
3)
{ "013040", 
  "100010", 
  "300060", 
  "000008", 
  "416000", 
  "000800" }
Returns: 1.5333333333333334
4)
{ "06390061", 
  "60960062", 
  "39090270", 
  "96900262", 
  "00000212", 
  "00222026", 
  "66761201", 
  "12022610" }
Returns: 3.2142857142857144

Submissions are judged against all 41 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class WorkingRabbits with a public method double getEfficiency(vector<string> profit) · 41 test cases · 2 s / 256 MB per case

Submitting as anonymous