SILLYPRS - Editorial

Problem Link - Magda and Silly Pairs Practice Problem in 1400 to 1600 difficulty problems

Problem Statement:

We need to pair N chefs with N chefettes to maximize the total sum of heights of their children. The height of the child resulting from the pairing of the i-th chef and the j-th chefette is given by:

Height of child=⌊A_i+B_j⌋/2. Where A_i is the height of the i-th chef and $B_j$​ is the height of the j-th chefette.

Approach:

  • Read the heights of chefs and add each height to sum.
  • Check if each height is odd using x & 1 (bitwise operation to check if x is odd). Increment odd if it is.
  • Read the heights of chefettes, add each height to sum, and decrement odd if the height is odd.
  • After processing both arrays, odd holds the net difference in the number of odd numbers between the two arrays.
  • The result for the maximum possible sum of heights of the children is calculated as: ans=(sum−∣odd∣)/2.
  • This formula ensures the sum is adjusted to balance out the pairing of odd and even numbers to maximize the overall sum.
  • Pairing an odd number with another odd or an even with an even results in an integer average. Pairing an odd with an even results in a non-integer average, which, when floored, results in a lower sum.
  • By subtracting abs(odd), we ensure that we adjust for these cases to maximize the sum.

Complexity:

  • Time Complexity: O(n) per test case for reading input and calculating sum and odd.
  • Space Complexity: O(1) for auxiliary space since only variables are used.