#P4989. Maximizing Non‐Crossing 0-1 Pairings with Maximum Inspiration

    ID: 18228 Type: Default 1000ms 256MiB

Maximizing Non‐Crossing 0-1 Pairings with Maximum Inspiration

Maximizing Non‐Crossing 0-1 Pairings with Maximum Inspiration

A mysterious observation leads to a peculiar pairing between the digits 0 and 1. Given a binary string, you are to form a set of pairs satisfying the following conditions:

  • Each pair must consist of a 0 occurring before a 1 (i.e. in positions, the 0 is in a higher place/high-order than the 1).
  • No digit may belong to more than one pair.
  • The pairs must be non‐crossing. Here, two pairs (i, j) and (k, l) (with i < k) are said to be crossing if they share some common interval but neither pair fully contains the other. For example, if one pair is formed by the 2nd and the 4th digit, and another is formed by the 3rd and the 5th digit, these two pairs cross and cannot both be selected. However, if one pair is (1, 5) and the other is (2, 4), they do not count as crossing since the interval corresponding to (2, 4) is entirely contained within (1, 5).

Additionally, each formed pair is assigned an inspiration coefficient defined by the absolute difference of their positions (where positions are counted from the highest digit on the left to the lowest on the right). Since in any valid pair the 0 always comes before the 1, the coefficient is j - i (if the pair is formed by the i-th and j-th digits, with i < j).

Your objective is two‐fold:

  1. First, maximize the total number of pairs.
  2. Then, among all selections that yield the maximum number of pairs, choose one in which the sum of inspiration coefficients is as large as possible.
  3. </p>

    Output the maximum pair count and the maximum total inspiration coefficient sum.

    The formulas used in the problem are in LaTeX format: for any valid pair $(i,j)$ the coefficient is given by $j-i$.

    inputFormat

    The input consists of a single line containing a binary string of 0s and 1s. The length of the string is at least 1.

    outputFormat

    Output two space‐separated integers: the maximum number of pairs that can be formed under the given constraints, and the maximum sum of inspiration coefficients possible using such a pairing.

    sample

    0101
    2 4