BLOBBYVOLLEY - Editorial

PROBLEM LINK:

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

Tester and Editorialist: iceknight1093

DIFFICULTY:

962

PREREQUISITES:

None

PROBLEM:

Alice and Bob play a game of Blobby Volley. Alice serves first.

When a player wins a point:

  • If they were the server, their score increases by 1 and they remain the server.
  • If they were the receiver, their score doesn’t change but they become the server for the next turn.

Given who won each point, find Alice’s and Bob’s final scores.

EXPLANATION:

It suffices to directly simulate the process based on the given rules.

Keep three variables: the current \text{server} (either \texttt{A} or \texttt{B}), Alice’s score s_A, and Bob’s score s_B.
Initially, \text{server} = \texttt{A}, and s_A = s_B = 0.

Then, for each point in order:

  • If the current server wins the point, increase the corresponding score by 1.
  • Otherwise, switch the server to \texttt{B} if it was \texttt{A} and vice versa.

Finally, print s_A and s_B.

TIME COMPLEXITY

\mathcal{O}(N) per test case.

CODE:

Editorialist's code (Python)
for test in range(int(input())):
    n = int(input())
    s = input()
    alice, bob = 0, 0
    server = 'A'
    for i in range(n):
        if server == s[i]:
            if server == 'A': alice += 1
            else: bob += 1
        else:
            if server == 'A': server = 'B'
            else: server = 'A'
    print(alice, bob)