SWMA - Editorial

PROBLEM LINK:

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

Author: pols_agyi_pols
Tester: watoac2001
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

None

PROBLEM:

Alice scored A marks and Bob scored B.
Alice can swap the digits of her marks and/or Bob’s marks.
Can she end up with higher marks than Bob?

EXPLANATION:

Since 11 \leq A, B \lt 100, both Alice’s and Bob’s marks will contain two digits.
Suppose they are A_1A_2 and B_1B_2 respectively.

By swapping, Alice can turn her marks into A_2A_1, and Bob’s marks into B_2B_1.
So, if either A_1A_2 or A_2A_1 is strictly greater than either B_1B_2 or B_2B_1, the answer is Yes; otherwise it’s No.

There are various ways to check this, some cleaner to implement than others:

  • There are only four possibilities to check, so you can simply try all four.
  • Alternately, you can reduce a bit of casework by only comparing \max(A_1A_2, A_2A_1) against \min(B_1B_2, B_2B_1).
  • You can even further reduce casework by only comparing the digits: \max(A_1, A_2) against \min(B_1, B_2).
    That is, the answer is Yes if Alice has some digit that’s greater than some digit of Bob - and No otherwise. Do you see why?

TIME COMPLEXITY:

\mathcal{O}(1) per testcase.

CODE:

Editorialist's code (Python)
x = int(input())
for _ in range(int(input())):
    x, y = input().split()
    # Comparing digits
    if max(x) > min(y): print('Yes')
    else: print('No')