/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 21ms 3.062 MiB
#2 Wrong Answer 20ms 3.105 MiB

Code

def optimal_strategy(s):
  """
  This function implements an optimal strategy for a game where two players
  alternately choose an index in a string and flip the value at that index.
  The goal is to maximize the number of consecutive '1's in the final string.

  Args:
    s: The initial string.

  Returns:
    A tuple containing the final string and the maximum number of consecutive
    '1's.
  """

  n = len(s)
  # Create a copy of the string as we'll be modifying it.
  s_copy = list(s)

  # Iterate through the string, flipping the values at alternating indices.
  for i in range(n):
    if i % 2 == 0:
      # Roy's turn
      s_copy[i] = str(1 - int(s_copy[i]))
    else:
      # Emon's turn
      s_copy[i] = str(1 - int(s_copy[i]))

  # Join the string back together
  final_string = "".join(s_copy)

  # Calculate the maximum number of consecutive '1's
  max_ones = 0
  current_ones = 0
  for char in final_string:
    if char == "1":
      current_ones += 1
      max_ones = max(max_ones, current_ones)
    else:
      current_ones = 0

  return final_string, max_ones

# Test the function
initial_string = "10"
final_string, max_ones = optimal_strategy(initial_string)

print(f"Final string: {final_string}")
print(f"Maximum consecutive '1's: {max_ones}")

Information

Submit By
Type
Submission
Problem
P1113 Fliping Game
Language
Python 3 (Python 3.12.3)
Submit At
2024-11-06 14:02:25
Judged At
2024-11-11 02:24:56
Judged By
Score
0
Total Time
21ms
Peak Memory
3.105 MiB