Created
March 13, 2026 20:08
-
-
Save maehrm/5272bc9c8ed6d2b38e8fecc5a3f30f1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_mex(a, b, c): | |
| mex = [False] * 4 | |
| mex[a] = mex[b] = mex[c] = True | |
| for i in range(4): | |
| if mex[i]: | |
| continue | |
| return i | |
| N = int(input()) | |
| A = list(map(int, input().split())) | |
| S = input() | |
| # acc_m[i][v]: Sのi文字目より左にある値vの'M'の個数 | |
| # acc_x[i][v]: Sのi文字目より左にある値vの'X'の個数 | |
| acc_m = [[0] * 3 for _ in range(N + 1)] | |
| acc_x = [[0] * 3 for _ in range(N + 1)] | |
| for i in range(N): | |
| for j in range(3): | |
| acc_m[i + 1][j] = acc_m[i][j] | |
| acc_x[i + 1][j] = acc_x[i][j] | |
| if S[i] == "M": | |
| acc_m[i + 1][A[i]] += 1 | |
| if S[i] == "X": | |
| acc_x[i + 1][A[i]] += 1 | |
| ans = 0 | |
| for i in range(N): | |
| if S[i] == "E": | |
| # 真ん中がEのとき、その左のMと右のXの組み合わせを計算 | |
| for j in range(3): | |
| for k in range(3): | |
| cnt = acc_m[i][j] * (acc_x[N][k] - acc_x[i][k]) | |
| ans += get_mex(j, A[i], k) * cnt | |
| print(ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment