Official

E - Priority Queue Editorial by evima


For each \(1 \leq v \leq N\), let us consider maximizing the number of values not less than \(v\) that are contained in \(s\) in the end.

For this objective, we can see that it is optimal to insert \(i\) in the \(i\)-th push.

This strategy does not depend on \(v\). Let \(Z=\{Z_1 < Z_2 < \cdots < Z_K\}\) (\(K=A-B\)) be the set \(s\) that results from this strategy.

Let us consider whether it is possible to have \(x=\{x_1< x_2 < \cdots < x_K\}\) as \(s\) in the end. First, it is necessary that \(x_i \leq Z_i\) \((1 \leq i \leq K)\). This is because otherwise there would be \(v\) such that there are more values not less than \(v\) in \(x\) than in \(Z\).

Actually, this necessary condition is also sufficient. After figuring it out, we can count sets \(x\) satisfying this condition with simple \(O(A^2)\)-time DP.

Finally, we will show the sufficiency of the condition. The following way to push numbers will leave the desired set in the end.

  • Let \(W=\{W_1<W_2<\cdots<W_{B}\} \) be the set of \(i\) such that \(1 \leq i \leq N\) and not contained in \(Z\).
  • Let \(y=\{y_1<y_2<\cdots<y_{B}\} \) be the set of \(i\) such that \(1 \leq i \leq N\) and not contained in \(x\).
  • In the \(Z_i\)-th push, insert \(x_i\).
  • In the \(W_i\)-th push, insert \(y_i\).

We can see that this strategy works by noting that \(W_i \leq y_i\) holds.

posted:
last update: