CHESS_PAIR - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: raysh07
Tester: apoorv_me
Editorialist: iceknight1093

DIFFICULTY:

892

PREREQUISITES:

None

PROBLEM:

2N chess players play among themselves, with X of them being rated and the remaining being unrated.
If they’re split up into N pairs, what’s the minimum number of rated players who must play against other rated players?

EXPLANATION:

There are 2N players in total, of which X are rated. This means there are 2N-X unrated players.

If X \leq 2N-X (so there are more unrated players), it’s always possible to pair every rated player with some unrated player; so the answer is 0.

If X \gt 2N-X, there are more rated players.
At best, 2N-X of the rated players can be made to play against an unrated player.
This leaves X - (2N - X) = 2X - 2N rated players who haven’t been paired up yet.
Since all of them are rated, and play among themselves, they all contribute to the count of “rated player playing against another rated player”.
So, in this case, the answer is 2X - 2N.

Thus, the final solution is to print 0 if X \leq 2N-X (equivalently, X \leq N), and 2X - 2N otherwise.
This can be succinctly written as \max(0, 2X-2N).

TIME COMPLEXITY

\mathcal{O}(1) per testcase.

CODE

Editorialist's code (Python)
for _ in range(int(input())):
    n, x = map(int, input().split())
    print(max(0, 2*x-2*n))