Official

E - Pairing Wizards Editorial by evima


For each given pair \((x,y)\), the strengths of Wizards \(x, y\) must be at least \(\min (b_x,b_y)\). Below, we assume that this requirement is already met by applying the operation appropriately.

Let \(X\) be the set of wizards planning to defeat monsters whose strengths are greater than that of the wizard, and \(Y\) be the set of the other wizards. The requirement for a pair \((x,y)\) that is not yet good can be written as follows (by swapping \(x\) and \(y\) if necessary): at least one of Wizards \(x, y\) must have a strength of at least \(b_x\), where \(x\) belongs to \(X\), and \(y\) belongs to \(Y\).

From the above, the minimum number of operations needed to make all the given pairs good equals the size of a minimum \(S-T\) cut of the following directed graph.

  • The vertice set consists of the following vertices:
    • The vertices \(S, T\)
    • A vertex corresponding to \(x\), for each \(x \in X\)
    • A vertex corresponding to a pair \((y, i)\), for each \(y \in Y\) and each integer \(i \, (1 \leq i \leq 100)\)
  • The edge set consists of the following edges:
    • An edge of weight \(b_x-a_x\) from \(S\) to the vertex corresponding to \(x\)
    • An edge of weight \(1\) from the vertex corresponding to \((y,i)\) to \(T\)
    • An edge of weight \(\infty\) from the vertex corresponding to \((y,i)\) to the vertex corresponding to \((y,i-1)\), for each \(i\geq 2\)
    • An edge of weight \(\infty\) from the vertex corresponding to \(x\) to the vertex corresponding to \((y,b_x-a_y)\), for each pair \((x,y)\) that is yet to be good

Indeed, for each pair \((x,y)\), making the strength of \(x\) at least \(b_x\) corresponds to the edge from \(S\) to the vertex corresponding to \(x\), and making the strength of \(y\) at least \(b_x\) corresponds to the edges from the vertices corresponding to \((y,i)\) for each \(i=1,\ldots,b_x-a_y\) to \(T\).

Let us analyze the complexity of this method. An \(S-T\) flow on this graph has a value of \(\mathrm O(NA)\), and there are \(O(N^2 + NA)\) edges, where \(A\) is the upper limit for \(a_i\) and \(b_i\), so the complexity is \(\mathrm O(N^3A + N^2A^2)\).

posted:
last update: