fork download
  1. # your code goes here
  2.  
  3. import sys
  4.  
  5.  
  6. def solve():
  7. input_data = sys.stdin.read().split()
  8. if not input_data:
  9. return
  10.  
  11. iterator = iter(input_data)
  12. num_test_cases = int(next(iterator))
  13.  
  14. out = []
  15. for _ in range(num_test_cases):
  16. n = int(next(iterator)) # Always 1 for Easy version
  17. m = int(next(iterator))
  18.  
  19. aarsi_cols = []
  20. krypto_cols = []
  21.  
  22. for j in range(1, m + 1):
  23. val = int(next(iterator))
  24. if val == 1:
  25. aarsi_cols.append(j)
  26. elif val == 2:
  27. krypto_cols.append(j)
  28. elif val == 3:
  29. aarsi_cols.append(j)
  30. krypto_cols.append(j)
  31.  
  32. # Helper to compute total distances sum for all target positions y in 1..M
  33. def compute_scores(cols, m):
  34. count = len(cols)
  35. if count == 0:
  36. return [0] * (m + 1)
  37.  
  38. total_sum = sum(cols)
  39. scores = [0] * (m + 1)
  40.  
  41. left_count = 0
  42. left_sum = 0
  43. col_ptr = 0
  44.  
  45. for y in range(1, m + 1):
  46. # Move pointer for columns <= y
  47. while col_ptr < count and cols[col_ptr] <= y:
  48. left_count += 1
  49. left_sum += cols[col_ptr]
  50. col_ptr += 1
  51.  
  52. right_count = count - left_count
  53. right_sum = total_sum - left_sum
  54.  
  55. # Score at bullseye column y
  56. scores[y] = (y * left_count - left_sum) + (
  57. right_sum - y * right_count
  58. )
  59.  
  60. return scores
  61.  
  62. aarsi_scores = compute_scores(aarsi_cols, m)
  63. krypto_scores = compute_scores(krypto_cols, m)
  64.  
  65. # Build answer array for each cell y from 1 to M
  66. res = []
  67. for y in range(1, m + 1):
  68. sa = aarsi_scores[y]
  69. sk = krypto_scores[y]
  70.  
  71. # Compare sa and sk depending on target question requirement
  72. # Example outputting SA and SK:
  73. res.append(f"{sa} {sk}")
  74.  
  75. out.append("\n".join(res))
  76.  
  77. print("\n".join(out))
  78.  
  79.  
  80. if __name__ == "__main__":
  81. solve()
Success #stdin #stdout 0.08s 14020KB
stdin
Standard input is empty
stdout
Standard output is empty