- #include <iostream> 
- #include <algorithm> 
- #include <vector> 
- #include <map> 
- #include <set> 
-   
- using namespace std; 
-   
- struct ChildInfo 
- { 
-    int id; 
-    double gram; 
- }; 
-   
- // Custom functor to compare doubles. 
- struct DoubleCompare 
- { 
-    bool operator()(double x1, double x2) 
-    { 
-       static double tolerance = 1.0E-10;  // Pick something else if this doesn't work. 
-       // If the numbers are almost equal. 
-       return ( fabs(x2-x1) > tolerance && x1 < x2); 
-    } 
- }; 
-   
- // Function to compare two ChildInfo to insert them into a set. 
- bool operator < (const ChildInfo& lhs, const ChildInfo& rhs) { return lhs.id < rhs.id; } 
-   
- std::ostream& operator<<(std::ostream& str, const ChildInfo& ci) { 
-    str << " " << ci.id << " gram " << ci.gram << "\n"; 
-    return str; 
- } 
-   
- // Read the info for one child. 
- void input(vector<ChildInfo> &v) 
- { 
-    ChildInfo newInfo; 
-   
-    cin >> newInfo.id >> newInfo.gram; 
-    v.push_back(newInfo);    
- } 
-   
- // Compute the mapped info from the vector of ChildInfo 
- void computeMappedInfo(vector<ChildInfo> const& vinfo, 
-                        std::map<double, std::set<ChildInfo>, DoubleCompare>& mappedInfo) 
- { 
-    // Separate them into sets first. 
-    for ( ChildInfo const& info : vinfo ) 
-    { 
-       mappedInfo[info.gram].insert(info); 
-    } 
-   
-    // Divide the grams in the set. 
-    for ( auto& item : mappedInfo ) 
-    { 
-       // The set can't be changed in place. 
-       // Create a new set and replace the old set with the new set. 
-   
-       std::set<ChildInfo> newSet; 
-       size_t s = item.second.size(); 
-       for ( auto info : item.second ) 
-       { 
-          info.gram /= s; 
-          newSet.insert(info); 
-       } 
-       mappedInfo[item.first] = newSet; 
-    } 
- } 
-   
- // Display the mapped info. 
- void display(std::map<double, std::set<ChildInfo>, DoubleCompare> const& mappedInfo) 
- { 
-    for ( auto const& item : mappedInfo ) 
-    { 
-       for ( auto& info : item.second ) 
-       { 
-          std::cout << info << std::endl; 
-       } 
-    } 
- } 
-   
- int main() 
- { 
-    int childNum = 0; 
-    cin >> childNum; 
-   
-    std::vector <ChildInfo> info; 
-   
-    for (int i = 0; i < childNum; ++i) { 
-       input(info); 
-    } 
-   
-    std::map<double, std::set<ChildInfo>, DoubleCompare> mappedInfo; 
-    computeMappedInfo(info, mappedInfo); 
-    display(mappedInfo); 
-   
-    return 0; 
- } 
-