description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for test in range(int(input())): n = int(input()) a = list(map(int, input().split())) cnt = [0] * (n + 10) ans = 0 for l in range(n): cur = 0 for r in range(l + 1, n): if a[l] == a[r]: ans += cur cur += cnt[a[r]] cnt[a[l]] += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline def process(): (N,) = map(int, input().split()) X = list(map(int, input().split())) dp = [([0] * (N + 1)) for _ in range(N + 1)] for i in range(1, N + 1): dp[i] = dp[i - 1][:] dp[i][X[i - 1]] = dp[i - 1][X[i - 1]] + 1 dp2 = [([0] * (N + 1)) for _ in range(N + 2)] for i in range(N, 0, -1): dp2[i] = dp2[i + 1][:] dp2[i][X[i - 1]] = dp2[i + 1][X[i - 1]] + 1 R = 0 for i in range(N): a = X[i] for j in range(i + 1, N): b = X[j] R += dp[i][b] * dp2[j + 2][a] print(R) (T,) = map(int, input().split()) for _ in range(T): process()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) arr = [int(x) for x in input().split()] left = {} sum = 0 for i in range(n - 1): right = {} for j in range(n - 1, i, -1): if arr[i] in right and arr[j] in left: sum += right[arr[i]] * left[arr[j]] if arr[j] in right: right[arr[j]] += 1 else: right[arr[j]] = 1 if arr[i] in left: left[arr[i]] += 1 else: left[arr[i]] = 1 print(sum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def make_freq_dict(n, a): d = {} for i in range(n): if a[i] not in d: d[a[i]] = 1 else: d[a[i]] += 1 return d def make_occs_dict(n, a): d = {} for i in range(n): if a[i] not in d: d[a[i]] = [i] else: d[a[i]].append(i) return d def new_freqs(a): d = {} for i in a: d[i] = 0 return d def sames_choose_4(num): if num < 4: return 0 numerator = num * (num - 1) * (num - 2) * (num - 3) denominator = 24 return numerator // denominator def solve(n, a): total = 0 og_freqs = make_freq_dict(n, a) for k in og_freqs: total += sames_choose_4(og_freqs[k]) for i in range(n - 3): freqs = new_freqs(a) ai = a[i] og_freqs[ai] -= 1 add_this = 0 subtotals = {} for j in range(i + 1, n): aj = a[j] if aj == ai: total += add_this else: freqs[aj] += 1 if aj in subtotals: add_this -= subtotals[aj] subtotals[aj] = freqs[aj] * (og_freqs[aj] - freqs[aj]) add_this += subtotals[aj] return total def main(): t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) print(solve(n, a)) main()
FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) while t > 0: t -= 1 n = int(input()) a = [int(i) for i in input().split()] total = 0 a1 = [([0] * (n + 1)) for _ in range(n + 1)] a2 = [([0] * (n + 1)) for _ in range(n + 1)] a3 = [([0] * (n + 1)) for _ in range(n + 1)] for i in a: for j in range(n + 1): total += a3[j][i] a3[i][j] += a2[j][i] a2[i][j] += a1[j][i] a1[i][j] += 1 print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) s = [(0) for _ in range(n + 1)] e = [(0) for _ in range(n + 1)] c = 0 for j in range(0, n - 2): e = [(0) for _ in range(n + 1)] for k in range(n - 1, j, -1): c += s[a[k]] * e[a[j]] e[a[k]] += 1 s[a[j]] += 1 print(c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) l = list(map(lambda x: int(x) - 1, input().split())) c = [0] * n dp = [] for i in l: c[i] += 1 dp.append(c.copy()) ans = 0 for i in range(1, n - 2): for j in range(i + 1, n - 1): bcwd = dp[-1][l[i]] - dp[j][l[i]] ans += dp[i - 1][l[j]] * bcwd print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) c = [([0] * n) for _ in range(n)] ans = 0 for j in range(1, n): for i in range(0, j): if c[a[i] - 1][a[j] - 1]: c[a[i] - 1][a[j] - 1] += 1 else: c[a[i] - 1][a[j] - 1] = 1 k = j + 1 for l in range(k + 1, n): ans += c[a[k] - 1][a[l] - 1] print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def process(A): d = {} n = len(A) for i in range(n): x = A[i] if x not in d: d[x] = [] d[x].append(i) answer = 0 for x in d: v = len(d[x]) answer += v * (v - 1) * (v - 2) * (v - 3) // 24 for x in d: for y in d: if x != y: pairs1 = {} pairs2 = {} for j in d[y]: for i in d[x]: if i > j: break if i not in pairs1: pairs1[i] = 0 if j not in pairs2: pairs2[j] = 0 pairs1[i] += 1 pairs2[j] += 1 for i in pairs1: for j in pairs2: if j < i: answer += pairs1[i] * pairs2[j] return answer t = int(input()) for i in range(t): n = int(input()) A = [int(x) for x in input().split()] print(process(A))
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in [0] * int(input()): n = int(input()) arr = list(map(int, input().split())) c = 0 bOne = [0] * (n + 2) for j in range(1, n - 2, 1): bOne[arr[j - 1]] += 1 bSec = [0] * (n + 2) added = 0 for l in range(j + 2, n, 1): v = arr[l - 1] added -= bSec[v] * bOne[v] bSec[v] += 1 added += bSec[v] * bOne[v] if arr[j] == arr[l]: c += added print(c)
FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def solve(): n = int(input()) l = list(map(int, input().split())) left = [0] * 3002 ans = 0 for i in range(n): right = [0] * 3002 for j in range(n - 1, i, -1): ans += right[l[i]] * left[l[j]] right[l[j]] += 1 left[l[i]] += 1 print(ans) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
dx = [0, 0, -1, 1] dy = [1, -1, 0, 0] def solve(n, ar): l_count = [0] * (n + 100) ans = 0 for i in range(n): r_count = [0] * (n + 100) for j in range(n - 1, i, -1): ans += l_count[ar[j]] * r_count[ar[i]] r_count[ar[j]] += 1 l_count[ar[i]] += 1 print(ans) t = int(input()) for _ in range(t): n = int(input()) ar = list(map(int, input().split())) solve(n, ar)
ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) def solve(arr, n): cntLeft = [(0) for i in range(n + 1)] cntRight = [(0) for i in range(n + 1)] res = 0 for j in range(n): cntRight = [(0) for i in range(n + 1)] for k in range(n - 1, j, -1): res += cntLeft[arr[k]] * cntRight[arr[j]] cntRight[arr[k]] += 1 cntLeft[arr[j]] += 1 return res t = inp() while t > 0: n = inp() arr = inlt() print(solve(arr, n)) t -= 1
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] values = sorted(list(set(a))) k = len(values) idx = {values[i]: i for i in range(k)} arr1 = [([0] * k) for _ in range(k)] arr2 = [([0] * k) for _ in range(k)] arr3 = [([0] * k) for _ in range(k)] tot = 0 for v in a: i = idx[v] for j in range(k): tot += arr3[j][i] arr3[i][j] += arr2[j][i] arr2[i][j] += arr1[j][i] arr1[i][j] += 1 print(tot)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
res = "" for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] c = {} r = 0 for i, row in enumerate(a): m = 0 for row2 in a[i + 1 :]: if row == row2: r += m m += c.get(row2, 0) c[row] = c.get(row, 0) + 1 res += f"{r}\n" print(res)
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) while t: t -= 1 n = int(input()) arr = list(map(int, input().split())) left = [0] * (n + 1) right = [0] * (n + 1) ans = 0 for j in range(n): for i in range(n + 1): right[i] = 0 for k in range(n - 1, j, -1): ans += left[arr[k]] * right[arr[j]] right[arr[k]] += 1 left[arr[j]] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline t = int(input()) for ii in range(t): n = int(input()) A = list(map(int, input().split())) ans = 0 l = max(A) + 1 B = [0] * l for i in A: B[i] += 1 F = [([0] * l) for i in range(l)] G = [0] * l for i in range(n): B[A[i]] -= 1 for j in range(l): ans += F[A[i]][j] * B[j] for j in range(l): F[j][A[i]] += G[j] G[A[i]] += 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) ans = 0 left = {} right = {} for i in range(n): right = {} for j in range(0, n): right[a[j]] = 0 for j in range(n - 1, i, -1): if a[j] in left: ans += right[a[i]] * left[a[j]] right[a[j]] += 1 if a[i] not in left: left[a[i]] = 1 else: left[a[i]] += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def singleElementCounts(x, l, r): if l < 0: l = 0 if r >= n: r = n - 1 if l == 0: return singleElementOccurrenceCnt[x][r] else: return singleElementOccurrenceCnt[x][r] - singleElementOccurrenceCnt[x][l - 1] t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] singleElementOccurrenceCnt = [[(0) for __ in range(n + 1)] for ___ in range(n + 1)] for x in range(1, n + 1): t = 0 for i in range(n): if a[i] == x: t += 1 singleElementOccurrenceCnt[x][i] = t totalCnts = [[(0) for __ in range(n + 1)] for ___ in range(n + 1)] for i in range(n - 1): for j in range(i + 1, n): totalCnts[a[i]][a[j]] += 1 ans = 0 cntsSoFar = [[(0) for __ in range(n + 1)] for ___ in range(n + 1)] for j in range(1, n): for i in range(j): x, y = a[i], a[j] cntsSoFar[x][y] += 1 for i in range(j): x, y = a[i], a[j] ans += ( totalCnts[x][y] - cntsSoFar[x][y] - singleElementCounts(x, 0, j) * singleElementCounts(y, j + 1, n - 1) ) print(ans)
FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) freq = {} answer = 0 for x in arr: if x not in freq: freq[x] = 0 freq[x] += 1 for i in range(n): freq[arr[i]] -= 1 equal_pairs = 0 so_far = {} for j in range(i + 1, n): freq[arr[j]] -= 1 if arr[j] in so_far: equal_pairs -= so_far[arr[j]] if arr[i] == arr[j]: answer += equal_pairs if arr[j] not in so_far: so_far[arr[j]] = 1 else: so_far[arr[j]] += 1 equal_pairs += freq[arr[j]] for j in range(i + 1, n): freq[arr[j]] += 1 print(answer)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline def before(j, a): if arr[j] == a: return prefix[j][a] - 1 else: return prefix[j][a] def after(k, b): return prefix[n - 1][b] - prefix[k][b] t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) prefix = [[(0) for i in range(n + 1)] for j in range(n)] for i in range(n): for j in range(n + 1): prefix[i][j] = prefix[i - 1][j] prefix[i][arr[i]] = prefix[i - 1][arr[i]] + 1 count = 0 for j in range(n): for k in range(j + 1, n): count += before(j, arr[k]) * after(k, arr[j]) print(count)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR VAR RETURN BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) d = [([0] * n) for _ in range(n)] for i in range(n): for j in range(i + 1, n): if a[i] == a[j]: d[i][j] = 1 for i in range(n): for j in range(n - 1): d[i][j + 1] += d[i][j] for i in range(n - 1): for j in range(n): d[i + 1][j] += d[i][j] ans = 0 for i in range(n): for j in range(i + 1, n): if a[i] == a[j]: ans += d[j - 1][n - 1] - d[j - 1][j] - d[i][n - 1] + d[i][j] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = lambda: sys.stdin.readline().rstrip() t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) accu = [([0] * (n + 1)) for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, n + 1): accu[i][j] = accu[i][j - 1] + (a[j - 1] == i) ans = 0 for j in range(2, n - 1): for k in range(j + 1, n): ans += accu[a[k - 1]][j - 1] * (accu[a[j - 1]][-1] - accu[a[j - 1]][k]) print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def main(): for _ in range(Iint()): n = Iint() array = Ilist() cntLeft = [0] * (n + 1) ans = 0 for i, Ival in enumerate(array): cntRight = [0] * (n + 1) for j in range(n - 1, i, -1): ans += cntLeft[array[j]] * cntRight[array[i]] cntRight[array[j]] += 1 cntLeft[array[i]] += 1 print(ans) def I(): return input() def Iint(): return int(input()) def Ilist(): return list(map(int, input().split())) def Imap(): return map(int, input().split()) def Plist(li, s=""): print(s.join(map(str, li))) main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) total = 0 overall = [0] * (n + 1) for x in a: overall[x] += 1 start = [0] * (n + 1) for i in range(n): start[a[i]] += 1 mid = [0] * (n + 1) mid_count = 0 for j in range(i + 1, n): if a[i] == a[j]: total += mid_count else: x = a[j] mid_count -= mid[x] * (overall[x] - mid[x] - start[x]) mid[a[j]] += 1 mid_count += mid[x] * (overall[x] - mid[x] - start[x]) for x, count in enumerate(overall): total += count * (count - 1) * (count - 2) * (count - 3) // 24 print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def compress(arr): arr2 = sorted(arr) order = dict() length = len(arr) for i in range(length): order[arr2[i]] = i arr3 = [0] * length for i in range(length): arr3[i] = order[arr[i]] return arr3 t = int(input()) for case in range(t): n = int(input()) arr = list(map(int, input().split())) arr = compress(arr) right_pairs = [0] * ((n + 1) * (n + 1)) result = 0 for i in range(n - 1, -1, -1): for j in range(i + 2, n): right_pairs[arr[i + 1] * n + arr[j]] += 1 for j in range(0, i): result += right_pairs[arr[j] * n + arr[i]] print(result)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def fun(): n = int(input()) a = list(map(int, input().split(" "))) prefix = [[(0) for i in range(n + 1)] for j in range(n + 1)] ans22 = 0 for i in range(n): for j in range(1, n + 1): prefix[i + 1][j] += prefix[i][j] prefix[i + 1][a[i]] += 1 for i in range(n): has = [(0) for i in range(n + 1)] ans = 0 for j in range(i + 1, n): if a[j] == a[i]: ans22 += ans if has[a[j]] > 0 and a[j] == a[i] and prefix[n][a[j]] - prefix[j][a[j]] > 0: ans22 -= has[a[j]] * (prefix[n][a[j]] - prefix[j][a[j]]) ans22 += has[a[j]] * (prefix[n][a[j]] - prefix[j][a[j]] - 1) if has[a[j]] == 0: ans += prefix[n][a[j]] - prefix[j + 1][a[j]] elif prefix[n][a[j]] - prefix[j][a[j]] > 0: ans -= has[a[j]] * (prefix[n][a[j]] - prefix[j][a[j]]) ans += (has[a[j]] + 1) * (prefix[n][a[j]] - prefix[j][a[j]] - 1) has[a[j]] += 1 return ans22 def solve(): t = int(input()) for i in range(t): print(fun()) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = [[(0) for i in range(n + 1)] for j in range(n)] c = [[(0) for i in range(n + 1)] for j in range(n)] for i in range(n - 1): b[i][a[i]] += 1 b[i + 1] = b[i][:] i = n - 1 while i > 0: c[i][a[i]] += 1 c[i - 1] = c[i][:] i -= 1 i, u, v, add = 1, 0, 0, 0 j = n - 2 for i in range(1, n - 2): j = n - 2 while j > i: u = c[j + 1][a[i]] v = b[i - 1][a[j]] add += u * v j -= 1 print(add)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) arr = [int(i) for i in input().split()] col = [0] * (n + 1) ans = 0 col[arr[0]] += 1 for j in range(1, n - 2): c = col[arr[j + 1]] val = arr[j] for l in range(j + 2, n): if arr[l] == val: ans += c c += col[arr[l]] col[arr[j]] += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def bins(l, n): low = -1 high = len(l) while low + 1 < high: avg = (low + high) // 2 if l[avg] < n: low = avg else: high = avg return [low, high] for t in range(int(input())): n = int(input()) l = list(map(int, input().split())) inds = {} for i in range(len(l)): if l[i] in inds: inds[l[i]].append(i) else: inds[l[i]] = [i] bl = [] for i in inds: if len(inds[i]) > 1: bl.append(inds[i]) ans = 0 for i in bl: z = len(i) ans += z * (z - 1) * (z - 2) * (z - 3) // 24 for i in range(len(bl)): for j in range(len(bl)): if i != j: a = bl[i] b = bl[j] if len(a) == 2 and len(b) == 2: if a[0] < b[0] and b[0] < a[1] and a[1] < b[1]: ans += 1 elif len(b) == 2: ai = bins(a, b[0]) aj = bins(a, b[1]) ans += ai[1] * (aj[0] - ai[0]) else: binss = [(0) for i in range(len(a))] for asdf in range(len(a)): binss[asdf] = bins(b, a[asdf]) for x in range(len(a)): for y in range(x + 1, len(a)): lessthan = binss[x] morethan = binss[y] between = morethan[0] - lessthan[0] bigger = len(b) - morethan[1] ans += between * bigger print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN LIST VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) ans = 0 cntL = [0] * (n + 1) cntR = [0] * (n + 1) for j in range(1, n - 2): cntR = [0] * (n + 1) cntL[arr[j - 1]] += 1 for k in range(n - 2, j, -1): cntR[arr[k + 1]] += 1 ans += cntL[arr[k]] * cntR[arr[j]] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) while t > 0: t -= 1 n = int(input()) lst = list(map(int, input().split())) M = [([0] * (n + 1)) for x in range(0, n + 1)] for i in range(0, n): for j in range(i + 1, n): M[lst[i]][lst[j]] += 1 ans = 0 for i in range(0, n): for j in range(i + 1, n): M[lst[i]][lst[j]] -= 1 for j in range(0, i): ans += M[lst[j]][lst[i]] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def check(s, n, v): for i in range(n): if s[i : i + n].count(v) == 0: return False return True def solve(): n = mint() a = list(mints()) dp = [([0] * (n + 1)) for i in range(n)] for i in range(n): d = dp[i] for j in range(n): d[j + 1] = d[j] + (a[j] == a[i]) r = 0 for i in range(1, n): for j in range(i + 1, n): r += dp[j][i] * (dp[i][n] - dp[i][j + 1]) print(r) for i in range(mint()): solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) def solve(arr, n): cntLeft = dict() cntRight = dict() res = 0 for j in range(n): cntRight = dict() for k in range(n - 1, j, -1): res += cntLeft.get(arr[k], 0) * cntRight.get(arr[j], 0) if arr[k] not in cntRight: cntRight[arr[k]] = 1 else: cntRight[arr[k]] += 1 if arr[j] not in cntLeft: cntLeft[arr[j]] = 1 else: cntLeft[arr[j]] += 1 return res t = inp() while t > 0: n = inp() arr = inlt() print(solve(arr, n)) t -= 1
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.buffer.readline def main(): t = int(input()) for _ in range(t): n = int(input()) A = list(map(int, input().split())) A = [(a - 1) for a in A] X = [([0] * n) for i in range(n)] for l in range(n - 1): for r in range(l + 1, n): if A[l] == A[r]: X[l][r] += 1 s = [([0] * (n + 1)) for _ in range(n + 1)] for i in range(n): for j in range(n): s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + X[i][j] ans = 0 for l in range(n - 3): for r in range(l + 1, n - 1): if A[l] == A[r]: x1 = l + 1 x2 = r y1 = r + 1 y2 = n ans += s[x2][y2] - s[x1][y2] - s[x2][y1] + s[x1][y1] print(ans) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) s = list(map(int, input().split())) cnt = [0] * (n + 1) ans = out = 0 for i in range(0, n - 1): cnt_tmp = [0] * (n + 1) ans = 0 for j in range(i + 1, n): temp = ans ans -= cnt_tmp[s[j]] * cnt[s[j]] cnt_tmp[s[j]] += 1 ans += cnt_tmp[s[j]] * cnt[s[j]] temp = ans - temp if s[i] == s[j]: out += ans - temp cnt[s[i]] += 1 print(out)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def bin(place, a): l = -1 r = len(place) while l < r - 1: m = (l + r) // 2 if place[m] >= a: r = m else: l = m return l def bin2(place, a): l = -1 r = len(place) while l < r - 1: m = (l + r) // 2 if place[m] > a: r = m else: l = m return r def main(): t = int(input()) for i in range(t): n = int(input()) values = list(map(int, input().split())) place = [[] for i in range(n)] answer = 0 for i in range(n): values[i] -= 1 place[values[i]].append(i) for j in range(1, n): for k in range(j + 1, n): count_i = bin(place[values[k]], j) + 1 count_l = len(place[values[j]]) - bin2(place[values[j]], k) answer += max(0, count_i * count_l) print(answer) main()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) for ii in range(t): n = int(input()) a = list(map(int, input().split())) total = {} for i in a: if i not in total: total[i] = 1 else: total[i] += 1 before = [] for i in range(n): before.append([0] * n) for i in range(n): element = i + 1 cnt = 0 for j in range(n): if a[j] == element: cnt += 1 before[i][j] = cnt tup = 0 for i in range(1, n - 2): for j in range(i + 1, n - 1): q = a[i] r = a[j] if q != r: tup += (total[q] - before[q - 1][j]) * before[r - 1][i] else: tup += (total[q] - before[q - 1][j]) * (before[r - 1][i] - 1) print(tup)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, ("0 " + input()).split())) c = [[(0) for _ in range(n + 1)] for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, n + 1): if j == a[i]: c[i][j] = c[i - 1][j] + 1 else: c[i][j] = c[i - 1][j] res = 0 for j in range(1, n + 1): for k in range(j + 1, n + 1): res += c[j - 1][a[k]] * (c[n][a[j]] - c[k][a[j]]) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL BIN_OP STRING FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
from sys import stdin input = stdin.buffer.readline for _ in range(int(input())): n = int(input()) (*a,) = map(int, input().split()) pref = [[(0) for j in range(n + 1)] for i in range(n)] ans = 0 for i in range(n): pref[i][a[i]] += 1 for i in range(1, n): for j in range(n + 1): pref[i][j] += pref[i - 1][j] for i in range(n): for j in range(i): if not j or i == n - 1: continue ans += pref[j - 1][a[i]] * (pref[n - 1][a[j]] - pref[i][a[j]]) print(ans)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) left = [(0) for i in range(n + 1)] right = [(0) for i in range(n + 1)] state = [(0) for i in range(n)] k = n - 1 while k >= 0: state[k] = right[:] right[arr[k]] += 1 k -= 1 ans = 0 k = 0 for j in range(0, n - 2): k = n - 1 while k > j: ans += left[arr[k]] * state[k][arr[j]] k -= 1 left[arr[j]] += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) cl = [0] * (n + 1) cr = [0] * (n + 1) for i in a: cr[i] += 1 ans = 0 for i in range(n - 3): cl[a[i]] += 1 now = a[i] nr = [(cr[j] - cl[j]) for j in range(n + 1)] cand = 0 cn = [0] * (n + 1) for j in range(i + 1, n - 1): y = a[j] if y != now: cand += (nr[y] - cn[y] - 1) * (cn[y] + 1) - (nr[y] - cn[y]) * cn[y] cn[y] += 1 else: ans += cand if nr[y] - cn[y] - 1 > 0: cand += (nr[y] - cn[y] - 2) * (cn[y] + 1) - ( nr[y] - cn[y] - 1 ) * cn[y] cn[y] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) ar = list(map(int, input().split())) dic = {} for i in range(1, n + 1): dic[i] = 0 ans = 0 for i in range(n): dic1 = {} for j in range(n - 1, i, -1): ans += dic[ar[j]] * dic1.get(ar[i], 0) dic1[ar[j]] = dic1.get(ar[j], 0) + 1 dic[ar[i]] += 1 print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) arr = [int(x) for x in input().split()] count = 0 left = dict() for i in range(n): right = dict() for j in range(n - 1, i, -1): try: count += left[arr[j]] * right[arr[i]] except: pass try: right[arr[j]] += 1 except: right[arr[j]] = 1 try: left[arr[i]] += 1 except: left[arr[i]] = 1 print(count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) a = [*map(int, input().split())] ans = 0 for i in range(n): f, s = [0] * (n + 1), [0] * (n + 1) pa = 0 for j in range(i + 2, n): s[a[j]] += 1 for j in range(i + 2, n): pa -= s[a[j]] * f[a[j]] if a[j] != a[j - 1]: pa -= f[a[j - 1]] * s[a[j - 1]] f[a[j - 1]] += 1 s[a[j]] -= 1 pa += f[a[j]] * s[a[j]] if a[j] != a[j - 1]: pa += f[a[j - 1]] * s[a[j - 1]] if a[i] == a[j]: ans += pa print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) a = [] id = [] for _ in range(t): n = int(input()) A = map(int, input().split()) a = [] for x in A: a.append(x) cnt = [[(0) for i in range(n + 1)] for j in range(n + 1)] ans = 0 for j in range(n): for i in range(j): cnt[a[i]][a[j]] += 1 k = j + 1 for l in range(k + 1, n): ans += cnt[a[k]][a[l]] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys def input(): return sys.stdin.readline()[:-1] def solve(): N = int(input()) A = list(map(int, input().split())) B = [None for i in range(N + 1)] b = [0] * 3001 B[0] = list(b) for i in range(N): b[A[i]] += 1 B[i + 1] = list(b) ans = 0 for i in range(N - 2): C = [0] * 3001 D = [0] * 3001 v = 0 for j in range(3001): C[j] = B[N][j] - B[i + 2][j] for j in range(i + 2, N): C[A[j]] -= 1 D[A[j - 1]] += 1 if A[j] == A[j - 1]: v += C[A[j]] * D[A[j]] - (C[A[j]] + 1) * (D[A[j]] - 1) else: v -= D[A[j]] v += C[A[j - 1]] if A[i] == A[j]: ans += v print(ans) def main(): T = int(input()) for i in range(T): solve() main()
IMPORT FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) arr = [*map(int, input().split())] max_ele = max(arr) + 1 counter = [([0] * max_ele) for _ in range(n)] counter[0][arr[0]] += 1 for i in range(1, n): for j in range(n): counter[i][arr[j]] = counter[i - 1][arr[j]] counter[i][arr[i]] += 1 ans = 0 for i in range(1, n - 1): for j in range(i + 1, n - 1): ans += counter[i - 1][arr[j]] * (counter[-1][arr[i]] - counter[j][arr[i]]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
from sys import stdin input = stdin.readline def nc2(n): return n * (n - 1) // 2 def nc4(n): return n * (n - 1) * (n - 2) * (n - 3) // 24 def answer(): count = [[(0) for i in range(n + 1)] for i in range(n + 1)] for i in range(1, n + 1): for j in range(n + 1): count[i][j] = count[i - 1][j] count[i][a[i - 1]] += 1 ans = 0 for j in range(1, n + 1): for k in range(j + 1, n + 1): x1 = count[j - 1][a[k - 1]] x2 = count[n][a[j - 1]] - count[k][a[j - 1]] ans += x1 * x2 return ans for T in range(int(input())): n = int(input()) a = list(map(int, input().split())) print(answer())
ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) while t: t -= 1 n = int(input()) l = list(map(int, input().split())) count = [0] * n ans = 0 for i, a in enumerate(l): cur = 0 for j in l[i + 1 :]: if j == a: ans += cur cur += count[j - 1] count[a - 1] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def Zigzag(Numbers, n): Ocurrences = dict() Ocurrences[Numbers[0]] = 1 ZigzagsCounter = 0 for j in range(1, n - 2): InternalCounter = 0 for l in range(j + 1, n): if Numbers[j] == Numbers[l]: ZigzagsCounter += InternalCounter InternalCounter += Ocurrences.get(Numbers[l], 0) Ocurrences[Numbers[j]] = Ocurrences.get(Numbers[j], 0) + 1 return ZigzagsCounter NTesters = int(input()) while NTesters > 0: n = int(input()) Numbers = list(map(int, input().split())) print(Zigzag(Numbers, n)) NTesters -= 1
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) for j in range(t): n = int(input()) m = list(map(int, input().split())) x = [(0) for k in range(n + 1)] xy = [[(0) for k in range(n + 1)] for t in range(n + 1)] xyx = [[(0) for k in range(n + 1)] for t in range(n + 1)] xyxy = [[(0) for k in range(n + 1)] for t in range(n + 1)] for a in m: for b in range(1, n + 1): if a != b: xyxy[b][a] += xyx[b][a] xyx[a][b] += xy[a][b] xy[b][a] += x[b] x[a] += 1 ans = sum(map(sum, xyxy)) for p in x: ans += p * (p - 1) * (p - 2) * (p - 3) // 24 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
from sys import stdin, stdout def zigzags(n, a_a): ans = 0 freq = [(0) for _ in range((n + 1) * (n + 1))] for j in range(n - 3, 0, -1): k = j + 1 for l in range(k + 1, n): freq[a_a[k] * n + a_a[l]] += 1 for i in range(j): ans += freq[a_a[i] * n + a_a[j]] return ans t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) a_a = list(map(int, stdin.readline().split())) ans = zigzags(n, a_a) stdout.write(str(ans) + "\n")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) s = list(map(int, input().split())) count = [([0] * (n + 1)) for _ in range(n + 1)] ans = 0 for i in range(n): for j in range(i): count[s[j]][s[i]] += 1 for j in range(i + 2, n): ans += count[s[i + 1]][s[j]] print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] for _ in range(II()): n = II() aa = LI1() cnt = [0] * n ans = 0 for i, a in enumerate(aa): cur = 0 for a2 in aa[i + 1 :]: if a2 == a: ans += cur cur += cnt[a2] cnt[a] += 1 print(ans)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) a = list(map(lambda x: x - 1, a)) acc = [[0] for _ in range(n)] for i in range(n): for j in range(n): acc[j].append(acc[j][-1] + (1 if a[i] == j else 0)) ans = 0 for i in range(n): for j in range(i + 1, n): ans += acc[a[j]][i] * (acc[a[i]][n] - acc[a[i]][j + 1]) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline def f(n): return n * (n - 1) * (n - 2) * (n - 3) // 4 // 3 // 2 for kek in range(int(input())): n = int(input()) a = list(map(int, input().split())) ans = 0 total = dict() left = dict() for i in a: if i in total: total[i] += 1 else: total[i] = 1 left[i] = 0 for l in range(0, len(a) - 1): d = dict() t = 0 for r in range(l + 1, len(a)): if a[l] == a[r]: ans += t if a[r] in d: t -= d[a[r]] * left[a[r]] d[a[r]] += 1 t += d[a[r]] * left[a[r]] else: d[a[r]] = 1 t += d[a[r]] * left[a[r]] left[a[l]] += 1 print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) pre = [([0] * (max(a) + 1)) for i in range(n + 1)] for i in range(n): pre[i + 1][a[i]] += 1 for i in range(n): for j in range(max(a) + 1): pre[i + 1][j] += pre[i][j] ans = 0 for j in range(2, n - 1): for k in range(j + 1, n): ans += pre[j - 1][a[k - 1]] * (pre[n][a[j - 1]] - pre[k][a[j - 1]]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline def solve(): n = int(input()) a = list(map(int, input().split())) ans = 0 cntL = [0] * (n + 1) cntL[a[0]] += 1 for j in range(1, n - 2): cntR = [0] * (n + 1) cntR[a[-1]] += 1 for k in range(n - 2, j, -1): ans += cntL[a[k]] * cntR[a[j]] cntR[a[k]] += 1 cntL[a[j]] += 1 print(ans) t = int(input()) for _ in range(t): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) matrix = [[] for j in range(n)] for j in range(n): for k in range(j + 2, n): if arr[j] == arr[k]: matrix[j].append(k) matrix1 = [([0] * n) for j in range(n)] matrix2 = [([0] * n) for j in range(n)] matrix3 = [([0] * n) for j in range(n)] for j in range(n): a = len(matrix[j]) for k in range(a): matrix1[matrix[j][k]][j] += 1 for j in range(n): matrix2[0][j] = matrix1[0][j] for j in range(n): for k in range(1, n): matrix2[k][j] = matrix2[k - 1][j] + matrix1[k][j] for j in range(n): matrix3[j][0] = matrix2[j][0] for j in range(n): for k in range(1, n): matrix3[j][k] = matrix3[j][k - 1] + matrix2[j][k] ans = 0 for j in range(n): a = len(matrix[j]) for k in range(a): ans += matrix3[matrix[j][k]][j] + matrix3[-1][matrix[j][k] - 1] ans -= matrix3[-1][j] + matrix3[matrix[j][k]][matrix[j][k] - 1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n = int(input()) A = list(map(int, input().split())) DP1 = [0] * (n + 1) DP2 = [([0] * (n + 1)) for i in range(n + 1)] DP3 = [([0] * (n + 1)) for i in range(n + 1)] DP4 = [([0] * (n + 1)) for i in range(n + 1)] for a in A: for i in range(1, n + 1): DP4[i][a] += DP3[i][a] for i in range(1, n + 1): DP3[a][i] += DP2[a][i] for i in range(1, n + 1): DP2[i][a] += DP1[i] DP1[a] += 1 ANS = 0 for i in range(n + 1): ANS += sum(DP4[i]) print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.buffer.readline def print(val): sys.stdout.write(str(val) + "\n") def prog(): for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) prefix = [0] * (n + 1) tuples = 0 for i in range(1, n - 2): prefix[a[i - 1]] += 1 i_after_j = 0 for j in range(n - 2, i, -1): i_after_j += a[j + 1] == a[i] tuples += prefix[a[j]] * i_after_j print(tuples) prog()
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) base = n + 1 d = {} cnt = 0 for i in range(n - 2, 0, -1): for j in range(i): cnt += d.get(a[j] + a[i] * base, 0) for k in range(i + 1, n): x = a[i] + a[k] * base d[x] = d.get(x, 0) + 1 print(cnt)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
from sys import stdin def inp(): return stdin.buffer.readline().rstrip().decode("utf8") def itg(): return int(stdin.buffer.readline()) def mpint(): return map(int, stdin.buffer.readline().split()) for __ in range(itg()): n = itg() arr = tuple(mpint()) ans = 0 cnt1 = [0] * 3007 for i in range(n): cnt2 = [0] * 3007 count = 0 for j in range(i + 1, n): if arr[i] == arr[j]: ans += count count += cnt1[arr[j]] cnt2[arr[j]] += 1 cnt1[arr[i]] += 1 print(ans)
FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
from sys import stdin input = stdin.readline q = int(input()) def po2(x): return x * (x - 1) // 2 for _ in range(q): n = int(input()) l = list(map(int, input().split())) for i in range(n): l[i] -= 1 pref = [([0] * (n + 1)) for i in range(n)] for i in range(1, n + 1): for j in range(n): if j == l[i - 1]: pref[j][i] = pref[j][i - 1] + 1 else: pref[j][i] = pref[j][i - 1] count = [pref[i][n] for i in range(n)] wynik = 0 pary = [([0] * (n + 1)) for i in range(n)] for i in range(n): if i == 0: continue for j in range(i + 1, n + 1): if j == i + 1: pary[i][j] = 0 else: cu = l[j - 1] pary[i][j] = pary[i][j - 1] + pref[cu][i] wyn = 0 for i in range(n): for j in range(i + 1, n): if l[i] != l[j]: continue wyn += pary[i][j] print(wyn)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def HalfDead(): n = int(input()) a = list(map(int, input().split())) dp1 = [([0] * (n + 1)) for _ in range(n + 1)] dp2 = [([0] * (n + 1)) for _ in range(n + 1)] dp3 = [([0] * (n + 1)) for _ in range(n + 1)] res = 0 for v in a: for w in range(n + 1): res += dp3[w][v] dp3[v][w] += dp2[w][v] dp2[v][w] += dp1[w][v] dp1[v][w] += 1 print(res) for _ in range(int(input())): HalfDead()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) ans = 0 ptn = [([0] * n) for _ in range(n)] cnt = [0] * n for i, r_val in enumerate(a): r_val -= 1 for l_val in range(n): ptn[l_val][r_val] += cnt[l_val] cnt[r_val] += 1 if i + 1 < n: l_val = a[i + 1] - 1 for j in range(i + 2, n): r_val = a[j] - 1 ans += ptn[l_val][r_val] print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
from itertools import accumulate as acm from itertools import compress as cp from sys import stdin def inp(): return stdin.buffer.readline().rstrip().decode("utf8") def itg(): return int(stdin.buffer.readline()) def mpint(): return map(int, stdin.buffer.readline().split()) for __ in range(itg()): n = itg() arr = tuple(mpint()) ans = 0 count = [0] * 3001 for i in range(n): ans += sum( cp( acm(map(count.__getitem__, arr[i + 1 :])), map(lambda x: arr[i] == x, arr[i + 2 :]), ) ) count[arr[i]] += 1 print(ans)
FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def solve(): n = int(input()) v = [0] * (n + 2) a = list(map(int, input().split())) ans = 0 for i in range(n): cur = 0 for j in range(i + 1, n): if a[i] == a[j]: ans += cur cur += v[a[j]] v[a[i]] += 1 print(ans) T = int(input()) while T > 0: T -= 1 solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.buffer.readline T = int(input()) for testcase in range(T): n = int(input()) a = list(map(int, input().split())) res = 0 dg = 10000 for i in range(n - 3): c = [0] * (n + 1) s = 0 c[a[i + 1]] += 1 for j in range(i + 3, n): c[a[j]] += dg s += c[a[i + 1]] // dg if a[i] == a[i + 2]: res += s for k in range(i + 2, n - 1): if k == i + 2: continue if a[k - 1] != a[k]: rr = c[a[k - 1]] q, p = divmod(rr, dg) s += q c[a[k - 1]] += 1 rr = c[a[k]] q, p = divmod(rr, dg) s -= p c[a[k]] -= dg else: rr = c[a[k]] q, p = divmod(rr, dg) s -= p * q s += (p + 1) * (q - 1) c[a[k]] += 1 - dg if a[i] == a[k]: res += s print(res)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) for case in range(t): n = int(input()) a = list(map(int, input().split())) occurrences = {} contributions = {} tot = 0 for i in range(n): if a[i] in contributions: tot += contributions[a[i]] if a[i] in occurrences: occurrences[a[i]].append(i) passed = 1 for j in range(occurrences[a[i]][0] + 1, i): if a[j] in contributions: contributions[a[j]] += passed else: contributions[a[j]] = passed if a[j] == a[i]: passed += 1 else: occurrences[a[i]] = [i] print(tot)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) seen_left = {} ans = 0 for i in range(n - 1): seen_right = {} for j in reversed(range(i + 1, n)): if a[j] in seen_left and a[i] in seen_right: ans += seen_left[a[j]] * seen_right[a[i]] seen_right[a[j]] = seen_right.get(a[j], 0) + 1 seen_left[a[i]] = seen_left.get(a[i], 0) + 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
from itertools import accumulate as acm from itertools import compress as cp for __ in range(int(input())): n = int(input()) arr = tuple(map(int, input().split())) ans = 0 count = [0] * 3001 for i in range(n): ans += sum( cp( acm(map(count.__getitem__, arr[i + 1 :])), map(lambda x: arr[i] == x, arr[i + 2 :]), ) ) count[arr[i]] += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def calcCntAtPrefix(a): cntAtPrefix = [[0] * (len(a) + 1)] for i, x in enumerate(a): cntAtPrefix.append(cntAtPrefix[-1][:]) cntAtPrefix[-1][x] += 1 return cntAtPrefix def solve(): n = int(input()) a = list(map(int, input().split())) cntAtPrefix = calcCntAtPrefix(a) cntAtSuffix = calcCntAtPrefix(a[::-1]) ans = 0 for j in range(n): for k in range(j + 1, n): ans += cntAtPrefix[j][a[k]] * cntAtSuffix[n - 1 - k][a[j]] print(ans) for t in range(int(input())): solve()
FUNC_DEF ASSIGN VAR LIST BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = [(int(item) - 1) for item in input().split()] cnt = [([0] * n) for _ in range(n + 1)] for i, item in enumerate(a): for val in range(n): cnt[i + 1][val] = cnt[i][val] cnt[i + 1][item] += 1 ans = 0 for i in range(n): for j in range(i + 1, n): left = cnt[i][a[j]] right = cnt[n][a[i]] - cnt[j + 1][a[i]] ans += left * right print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split(" ")] b = {val: i for i, val in enumerate(sorted(set(a)))} a = [b[x] for x in a] cntLeft = {i: (0) for i in range(n)} cntRight = {i: (0) for i in range(n)} ans = 0 for j in range(n): cntRight = {i: (0) for i in range(n)} for k in range(n - 1, j, -1): ans += cntLeft[a[k]] * cntRight[a[j]] cntRight[a[k]] += 1 cntLeft[a[j]] += 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline def solution(): n = int(input()) a = list(map(int, input().split())) ans = 0 cnt_L = [0] * (n + 1) for j in range(1, n - 2): cnt_L[a[j - 1]] += 1 current_sum = 0 cnt_R = [0] * (n + 1) for l in range(j + 2, n): current_sum -= cnt_L[a[l - 1]] * cnt_R[a[l - 1]] cnt_R[a[l - 1]] += 1 current_sum += cnt_L[a[l - 1]] * cnt_R[a[l - 1]] if a[j] == a[l]: ans += current_sum print(ans) T = int(input()) for _ in range(T): solution()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
gans = [] for _ in range(int(input())): n = int(input()) s = list(map(lambda x: int(x) - 1, input().split())) u = [] cnt = [] k = 1 for i in range(1, n): if s[i] == s[i - 1]: k += 1 else: u.append(s[i - 1]) cnt.append(k) k = 1 u.append(s[-1]) m = len(u) cnt.append(k) dp = [] ans = 0 for cl in range(n): dp.append([0] * (m + 1)) for i in range(m): dp[cl][i] = dp[cl][i - 1] if u[i] == cl: dp[cl][i] += cnt[i] cur = dp[cl][m - 1] if cur >= 4: ans += cur * (cur - 1) * (cur - 2) * (cur - 3) // (2 * 3 * 4) d = dict() for i in range(m - 2): ansi = 0 d = dict() for k in range(i + 1, m - 1): if u[i] == u[k]: ans += ansi * cnt[i] * cnt[k] continue if u[k] in d.keys(): cur = d[u[k]][:] ansi -= cur[0] * cur[1] cur[0] += cnt[k] cur[1] -= cnt[k] ansi += cur[0] * cur[1] d[u[k]][0] += cnt[k] d[u[k]][1] -= cnt[k] else: d[u[k]] = [cnt[k], dp[u[k]][m - 1] - dp[u[k]][k]] ansi += (dp[u[k]][m - 1] - dp[u[k]][k]) * cnt[k] gans.append(ans) print("\n".join(map(str, gans)))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
t = int(input()) while t: t = t - 1 n = int(input()) a = list(map(int, input().split())) ans = 0 l = [0] * 3007 for i in range(n): r = [0] * 3007 for j in range(n - 1, i, -1): ans = ans + l[a[j]] * r[a[i]] r[a[j]] = r[a[j]] + 1 l[a[i]] = l[a[i]] + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
def getcnt(x, l, r): if r < l: return 0 if l == 0: return p[x][r] else: return p[x][r] - p[x][l - 1] t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] p = [[(0) for _a in range(n)] for _b in range(n + 1)] for x in range(1, n + 1): cnt = 0 for i in range(n): if a[i] == x: cnt += 1 p[x][i] = cnt ans = 0 for i in range(n - 1): y = a[i] for j in range(i + 1, n): x = a[j] ans += getcnt(x, 0, i - 1) * getcnt(y, j + 1, n - 1) print(ans)
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
n, k = list(map(int, input().split())) mat = [] for i in range(n): mat.append(list(input())) prefr = [[(0) for i in range(n)] for j in range(n)] prefc = [[(0) for i in range(n)] for j in range(n)] done = 0 for i in range(n): for j in range(n): if j == 0 and mat[i][j] == "B": prefr[i][j] = 1 else: prefr[i][j] = prefr[i][j - 1] if mat[i][j] == "B": prefr[i][j] += 1 if i == 0 and mat[i][j] == "B": prefc[i][j] = 1 else: prefc[i][j] = prefc[i - 1][j] if mat[i][j] == "B": prefc[i][j] += 1 for i in range(n): if prefc[-1][i] == 0: done += 1 if prefr[i][-1] == 0: done += 1 dr, dc = [[(0) for i in range(n)] for j in range(n)], [ [(0) for i in range(n)] for j in range(n) ] for j in range(n - k + 1): count = [] s = 0 for i in range(k): if j == 0: if prefr[i][-1] - prefr[i][j + k - 1] == 0 and prefr[i][-1] != 0: count.append(1) s += 1 else: count.append(0) elif ( prefr[i][j - 1] + (prefr[i][-1] - prefr[i][j + k - 1]) == 0 and prefr[i][-1] != 0 ): count.append(1) s += 1 else: count.append(0) dr[0][j] = s for i in range(1, n - k + 1): if j == 0: if ( prefr[i + k - 1][-1] - prefr[i + k - 1][j + k - 1] == 0 and prefr[i + k - 1][-1] != 0 ): count.append(1) s += 1 else: count.append(0) elif ( prefr[i + k - 1][j - 1] + (prefr[i + k - 1][-1] - prefr[i + k - 1][j + k - 1]) == 0 and prefr[i + k - 1][-1] != 0 ): count.append(1) s += 1 else: count.append(0) if count[i - 1] == 1 and prefr[i - 1][-1] != 0: s -= 1 dr[i][j] = s for i in range(n - k + 1): count = [] s = 0 for j in range(k): if i == 0: if prefc[-1][j] - prefc[i + k - 1][j] == 0 and prefc[-1][j] != 0: count.append(1) s += 1 else: count.append(0) elif ( prefc[i - 1][j] + (prefc[-1][j] - prefc[i + k - 1][j]) == 0 and prefc[-1][j] != 0 ): count.append(1) s += 1 else: count.append(0) dc[i][0] = s for j in range(1, n - k + 1): if i == 0: if ( prefc[-1][j + k - 1] - prefc[i + k - 1][j + k - 1] == 0 and prefc[-1][j + k - 1] != 0 ): count.append(1) s += 1 else: count.append(0) elif ( prefc[i - 1][j + k - 1] + (prefc[-1][j + k - 1] - prefc[i + k - 1][j + k - 1]) == 0 and prefc[-1][j + k - 1] != 0 ): count.append(1) s += 1 else: count.append(0) if count[j - 1] == 1 and prefc[-1][j - 1] != 0: s -= 1 dc[i][j] = s ans = 0 for i in range(n): for j in range(n): ans = max(ans, dr[i][j] + dc[i][j]) print(ans + done)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
def main(): n, k = list(map(int, input().split())) ss = [] tate = [[(0) for _ in range(n + 1)] for _ in range(n + 1)] yoko = [[(0) for _ in range(n + 1)] for _ in range(n + 1)] for i in range(n): s = input().strip() for j, _s in enumerate(s): tate[i + 1][j + 1] = tate[i][j + 1] + (1 if _s == "B" else 0) yoko[i + 1][j + 1] = yoko[i + 1][j] + (1 if _s == "B" else 0) lc = 0 for i in range(n): if tate[n][i + 1] == 0: lc += 1 if yoko[i + 1][n] == 0: lc += 1 yans = [[(0) for _ in range(n + 1)] for _ in range(n + 1)] tans = [[(0) for _ in range(n + 1)] for _ in range(n + 1)] for i in range(n): l, r = 0, k while r <= n: yans[i + 1][l + 1] = yans[i][l + 1] if ( yoko[i + 1][n] != 0 and yoko[i + 1][r] - yoko[i + 1][l] == yoko[i + 1][n] ): yans[i + 1][l + 1] += 1 l += 1 r += 1 for i in range(n): l, r = 0, k while r <= n: tans[l + 1][i + 1] = tans[l + 1][i] if ( tate[n][i + 1] != 0 and tate[r][i + 1] - tate[l][i + 1] == tate[n][i + 1] ): tans[l + 1][i + 1] += 1 l += 1 r += 1 ans = lc for i in range(n - k + 1): for j in range(n - k + 1): ans = max( ans, lc + yans[i + k][j + 1] - yans[i][j + 1] + tans[i + 1][j + k] - tans[i + 1][j], ) print(ans) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR STRING NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
ac = [[(0) for i in range(2010)] for j in range(2010)] n, k = map(int, input().split()) v = [] for i in range(n): v.append(input()) extra = 0 for i in range(n): L = n R = -1 for j in range(n): if v[i][j] == "B": L = min(L, j) R = max(R, j) if L > R: extra += 1 elif R - L + 1 <= k: minx = max(0, i - k + 1) maxx = i miny = max(0, R - k + 1) maxy = L ac[minx][miny] += 1 ac[maxx + 1][maxy + 1] += 1 ac[maxx + 1][miny] -= 1 ac[minx][maxy + 1] -= 1 for j in range(n): L = n R = -1 for i in range(n): if v[i][j] == "B": L = min(L, i) R = max(R, i) if L > R: extra += 1 elif R - L + 1 <= k: minx = max(0, R - k + 1) maxx = L miny = max(0, j - k + 1) maxy = j ac[minx][miny] += 1 ac[maxx + 1][maxy + 1] += 1 ac[maxx + 1][miny] -= 1 ac[minx][maxy + 1] -= 1 for i in range(n): for j in range(n): if i > 0: ac[i][j] += ac[i - 1][j] if j > 0: ac[i][j] += ac[i][j - 1] if i > 0 and j > 0: ac[i][j] -= ac[i - 1][j - 1] ans = 0 for i in range(n - k + 1): for j in range(n - k + 1): ans = max(ans, ac[i][j]) print(ans + extra)
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
n, k = [int(i) for i in input().split()] sz = n - k + 1 cnt = [] for i in range(sz): cnt.append([0] * sz) data = [] extra = 0 for i in range(n): data.append(input()) for r in range(n): row = data[r] li = row.find("B") if li == -1: extra += 1 continue ri = row.rfind("B") for i in range(max(ri - k + 1, 0), min(li + 1, sz)): for j in range(max(0, r + 1 - k), min(sz, r + 1)): cnt[j][i] += 1 for c in range(n): row = "".join([data[i][c] for i in range(n)]) li = row.find("B") if li == -1: extra += 1 continue ri = row.rfind("B") for i in range(max(ri - k + 1, 0), min(li + 1, sz)): for j in range(max(0, c + 1 - k), min(sz, c + 1)): cnt[i][j] += 1 mx = 0 for i in range(sz): for j in range(sz): if cnt[i][j] > mx: mx = cnt[i][j] print(mx + extra)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
n, k = map(int, input().split()) s = [input() for _ in range(n)] g_row = [[] for _ in range(n)] g_col = [[] for _ in range(n)] already_white_line_nums = 0 for i in range(n): for j in range(n): if s[i][j] == "B": g_row[i].append(j) g_col[j].append(i) for i in range(n): if not g_row[i]: already_white_line_nums += 1 if not g_col[i]: already_white_line_nums += 1 r = 0 new_white_line_nums_col = [[[0] for _ in range(n - k + 1)] for _ in range(n - k + 1)] for i in range(n - k + 1): new_white_line_nums = 0 for l in range(0, k): if g_col[l] and i <= g_col[l][0] and g_col[l][-1] < i + k: new_white_line_nums += 1 new_white_line_nums_col[i][0] = new_white_line_nums for old_col in range(n - k): if g_col[old_col] and i <= g_col[old_col][0] and g_col[old_col][-1] < i + k: new_white_line_nums -= 1 new_col = old_col + k if g_col[new_col] and i <= g_col[new_col][0] and g_col[new_col][-1] < i + k: new_white_line_nums += 1 new_white_line_nums_col[i][old_col + 1] = new_white_line_nums for i in range(n - k + 1): new_white_line_nums = 0 for l in range(0, k): if g_row[l] and i <= g_row[l][0] and g_row[l][-1] < i + k: new_white_line_nums += 1 r = max(r, new_white_line_nums_col[0][i] + new_white_line_nums) for old_row in range(n - k): if g_row[old_row] and i <= g_row[old_row][0] and g_row[old_row][-1] < i + k: new_white_line_nums -= 1 new_row = old_row + k if g_row[new_row] and i <= g_row[new_row][0] and g_row[new_row][-1] < i + k: new_white_line_nums += 1 r = max(r, new_white_line_nums_col[old_row + 1][i] + new_white_line_nums) print(r + already_white_line_nums)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
from sys import setrecursionlimit as SRL from sys import stdin SRL(10**7) rd = stdin.readline rrd = lambda: list(map(int, rd().strip().split())) n, k = rrd() s = [] cal = [([0] * (n + 10)) for _i in range(n + 10)] for i in range(n): s.append(str(rd())) ans = 0 for i in range(n): j = 0 while j < n and s[i][j] == "W": j += 1 l = j if j >= n: ans += 1 continue j = n - 1 while j >= 0 and s[i][j] == "W": j -= 1 r = j if r - l + 1 > k: continue l1 = max(0, i - k + 1) r1 = i + 1 l2 = max(0, r - k + 1) r2 = l + 1 cal[l1][l2] += 1 cal[r1][l2] -= 1 cal[l1][r2] -= 1 cal[r1][r2] += 1 for i in range(n): j = 0 while j < n and s[j][i] == "W": j += 1 l = j if j >= n: ans += 1 continue j = n - 1 while j >= 0 and s[j][i] == "W": j -= 1 r = j if r - l + 1 > k: continue l1 = max(0, i - k + 1) r1 = i + 1 l2 = max(0, r - k + 1) r2 = l + 1 cal[l2][l1] += 1 cal[l2][r1] -= 1 cal[r2][l1] -= 1 cal[r2][r1] += 1 for i in range(n): for j in range(n): if j: cal[i][j] += cal[i][j - 1] pans = 0 for j in range(n): for i in range(n): if i: cal[i][j] += cal[i - 1][j] pans = max(pans, cal[i][j]) print(ans + pans)
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
n, k = map(int, input().split()) a = [input() for _ in range(n)] m = n - k + 1 def solve(a): p = [(r.find("B"), r.rfind("B")) for r in a] get = lambda i, j: j <= p[i][0] and p[i][1] <= j + k - 1 res = [([0] * m) for i in range(m)] for j in range(m): res[0][j] = cnt = sum(x == -1 for x, _ in p) + sum(get(i, j) for i in range(k)) for i in range(k, n): res[i - k + 1][j] = cnt = cnt + get(i, j) - get(i - k, j) return res x = [v for r in solve(a) for v in r] y = [v for r in list(zip(*solve(list("".join(y) for y in zip(*a))))) for v in r] print(max(u + v for u, v in zip(x, y)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
[n, k] = [int(x) for x in input().split()] c_p1 = [([-1] * 2010) for x in range(2010)] c_p2 = [([-1] * 2010) for x in range(2010)] r_p1 = [([-1] * 2010) for x in range(2010)] r_p2 = [([-1] * 2010) for x in range(2010)] cells = [] for i in range(n): cells.append(input()) bonus = 0 for y in range(n): earliest = -1 latest = -1 for x in range(n): if cells[y][x] == "B": earliest = x break for x in range(n): if cells[y][n - x - 1] == "B": latest = n - x - 1 break if earliest == -1: bonus += 1 for x in range(n - k + 1): r_p1[y][x] = int(earliest >= x and x + k - 1 >= latest) for x in range(n - k + 1): sum = 0 for y in range(n): sum += r_p1[y][x] if y - k >= 0: sum -= r_p1[y - k][x] if y - k + 1 >= 0: r_p2[y - k + 1][x] = sum for x in range(n): earliest = -1 latest = -1 for y in range(n): if cells[y][x] == "B": earliest = y break for y in range(n): if cells[n - y - 1][x] == "B": latest = n - y - 1 break if earliest == -1: bonus += 1 for y in range(n - k + 1): c_p1[y][x] = int(earliest >= y and y + k - 1 >= latest) for y in range(n - k + 1): sum = 0 for x in range(n): sum += c_p1[y][x] if x - k >= 0: sum -= c_p1[y][x - k] if x - k + 1 >= 0: c_p2[y][x - k + 1] = sum ans = 0 for y in range(n - k + 1): for x in range(n - k + 1): ans = max(ans, c_p2[y][x] + r_p2[y][x]) print(ans + bonus)
ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
import sys from sys import stdin n, k = map(int, stdin.readline().split()) S = [stdin.readline()[:-1] for i in range(n)] orians = 0 lis = [([0] * (n + 1)) for i in range(n + 1)] for i in range(n): minj = float("inf") maxj = float("-inf") for j in range(n): if S[i][j] == "B": minj = min(minj, j) maxj = max(maxj, j) if minj == float("inf"): orians += 1 continue if maxj - minj + 1 <= k: x1 = max(0, i - k + 1) x2 = i + 1 y1 = max(0, maxj - k + 1) y2 = minj + 1 lis[x1][y1] += 1 lis[x1][y2] -= 1 lis[x2][y1] -= 1 lis[x2][y2] += 1 for j in range(n): mini = float("inf") maxi = float("-inf") for i in range(n): if S[i][j] == "B": mini = min(mini, i) maxi = max(maxi, i) if mini == float("inf"): orians += 1 continue if maxi - mini + 1 <= k: x1 = max(0, maxi - k + 1) x2 = mini + 1 y1 = max(0, j - k + 1) y2 = j + 1 lis[x1][y1] += 1 lis[x1][y2] -= 1 lis[x2][y1] -= 1 lis[x2][y2] += 1 ans = 0 for i in range(n): for j in range(n - 1): lis[i][j + 1] += lis[i][j] for i in range(n - 1): for j in range(n): lis[i + 1][j] += lis[i][j] for i in range(n): for j in range(n): ans = max(ans, lis[i][j]) print(ans + orians)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
n, k = map(int, input().split()) s = [] for i in range(n): s += [input()] y = [[n, 0] for i in range(n)] z = [[n, 0] for i in range(n)] r = 0 for i in range(n): ind1 = n ind2 = -1 for j in range(n): if s[i][j] == "B": ind1 = min(j, ind1) ind2 = max(j, ind2) if ind1 != n: y[i] = [max(0, ind2 + 1 - k), ind1] else: r += 1 for j in range(n): ind1 = n ind2 = -1 for i in range(n): if s[i][j] == "B": ind1 = min(i, ind1) ind2 = max(i, ind2) if ind1 != n: z[j] = [max(0, ind2 + 1 - k), ind1] else: r += 1 x = [[(0) for i in range(n)] for i in range(n + 1)] x2 = [[(0) for i in range(n + 1)] for i in range(n)] for a in range(n): for b in range(n): i = n - 1 - a j = n - 1 - b x[i][j] += x[i + 1][j] if y[i][0] <= j <= y[i][1]: x[i][j] += 1 if i + k <= n - 1 and y[i + k][0] <= j <= y[i + k][1]: x[i][j] -= 1 for b in range(n): for a in range(n): i = n - 1 - a j = n - 1 - b x2[i][j] += x2[i][j + 1] if z[j][0] <= i <= z[j][1]: x2[i][j] += 1 if j + k <= n - 1 and z[j + k][0] <= i <= z[j + k][1]: x2[i][j] -= 1 m = 0 for i in range(n): for j in range(n): c = x[i][j] + x2[i][j] if c > m: m = c print(m + r)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
import sys input = sys.stdin.readline n, k = map(int, input().split()) MAP = [input().strip() for i in range(n)] MINR = [1 << 30] * n MAXR = [-1] * n MINC = [1 << 30] * n MAXC = [-1] * n for i in range(n): for j in range(n): if MAP[i][j] == "B": MINR[i] = min(MINR[i], j) MAXR[i] = max(MAXR[i], j) MINC[j] = min(MINC[j], i) MAXC[j] = max(MAXC[j], i) ALWAYS = 0 for i in range(n): if MINR[i] == 1 << 30 and MAXR[i] == -1: ALWAYS += 1 if MINC[i] == 1 << 30 and MAXC[i] == -1: ALWAYS += 1 ANS = [([0] * (n - k + 1)) for i in range(n - k + 1)] for j in range(n - k + 1): NOW = 0 for i in range(k): if ( j <= MINR[i] and j + k - 1 >= MAXR[i] and MINR[i] != 1 << 30 and MAXR[i] != -1 ): NOW += 1 ANS[0][j] += NOW for i in range(n - k): if ( j <= MINR[i] and j + k - 1 >= MAXR[i] and MINR[i] != 1 << 30 and MAXR[i] != -1 ): NOW -= 1 if ( j <= MINR[i + k] and j + k - 1 >= MAXR[i + k] and MINR[i + k] != 1 << 30 and MAXR[i + k] != -1 ): NOW += 1 ANS[i + 1][j] += NOW for i in range(n - k + 1): NOW = 0 for j in range(k): if ( i <= MINC[j] and i + k - 1 >= MAXC[j] and MINC[j] != 1 << 30 and MAXC[j] != -1 ): NOW += 1 ANS[i][0] += NOW for j in range(n - k): if ( i <= MINC[j] and i + k - 1 >= MAXC[j] and MINC[j] != 1 << 30 and MAXC[j] != -1 ): NOW -= 1 if ( i <= MINC[j + k] and i + k - 1 >= MAXC[j + k] and MINC[j + k] != 1 << 30 and MAXC[j + k] != -1 ): NOW += 1 ANS[i][j + 1] += NOW print(max([max(a) for a in ANS]) + ALWAYS)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
import sys input = lambda: sys.stdin.readline().strip() n, k = map(int, input().split()) arr = [] for i in range(n): arr.append(list(input())) extra = 0 res = [] for i in range(n - k + 1): res.append([]) for j in range(n - k + 1): res[-1].append(0) l = {} r = {} for i in range(n): for j in range(n): if arr[i][j] == "B": l[i] = j break for j in range(n - 1, -1, -1): if arr[i][j] == "B": r[i] = j break else: l[i] = None r[i] = None extra += 1 for j in range(n - k + 1): tmp = 0 for i in range(k): if l[i] is not None and l[i] >= j and r[i] <= j + k - 1: tmp += 1 res[0][j] += tmp for i in range(1, n - k + 1): if l[i - 1] is not None and l[i - 1] >= j and r[i - 1] <= j + k - 1: tmp -= 1 if l[i + k - 1] is not None and l[i + k - 1] >= j and r[i + k - 1] <= j + k - 1: tmp += 1 res[i][j] += tmp l = {} r = {} for j in range(n): for i in range(n): if arr[i][j] == "B": l[j] = i break for i in range(n - 1, -1, -1): if arr[i][j] == "B": r[j] = i break else: l[j] = None r[j] = None extra += 1 for i in range(n - k + 1): tmp = 0 for j in range(k): if l[j] is not None and l[j] >= i and r[j] <= i + k - 1: tmp += 1 res[i][0] += tmp for j in range(1, n - k + 1): if l[j - 1] is not None and l[j - 1] >= i and r[j - 1] <= i + k - 1: tmp -= 1 if l[j + k - 1] is not None and l[j + k - 1] >= i and r[j + k - 1] <= i + k - 1: tmp += 1 res[i][j] += tmp print(max(max(i) for i in res) + extra)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR NONE ASSIGN VAR VAR NONE VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NONE VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NONE VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER NONE VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR NONE ASSIGN VAR VAR NONE VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NONE VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NONE VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER NONE VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
import sys def count(n, k, field): blank = 0 cnt = [([0] * (n - k + 1)) for _ in range(n)] for i, row in enumerate(field): l = row.find("B") r = row.rfind("B") if l == r == -1: blank += 1 continue if r - l + 1 > k: continue kl = max(0, r - k + 1) kr = min(l + 1, n - k + 1) cnt[i][kl:kr] = [1] * (kr - kl) acc = [([0] * (n - k + 1)) for _ in range(n - k + 1)] t_cnt = list(zip(*cnt)) for i, col in enumerate(t_cnt): aci = acc[i] tmp = sum(col[n - k :]) aci[n - k] = tmp for j in range(n - k - 1, -1, -1): tmp += col[j] tmp -= col[j + k] aci[j] = tmp return blank, acc n, k = map(int, input().split()) field = [line.strip() for line in sys.stdin] bh, hor = count(n, k, field) t_field = ["".join(col) for col in zip(*field)] bv, t_var = count(n, k, t_field) var = list(zip(*t_var)) print(bh + bv + max(h + v for rh, rv in zip(hor, var) for h, v in zip(rh, rv)))
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
n, k = list(map(int, input().split())) it = [([0] * n) for i in range(n)] t = [[-1, -1] for i in range(n)] tt = [[-1, -1] for i in range(n)] for i in range(n): s = input() c = -1 cc = -1 for j, ii in enumerate(s): if ii == "B": it[i][j] = 1 if c == -1: c = j cc = j t[i] = [c, cc] for i in range(n): p = [it[j][i] for j in range(n)] c = -1 cc = -1 for j in range(n): if p[j]: if c == -1: c = j cc = j tt[i] = [c, cc] row_max = [([0] * (n - k + 1)) for i in range(n - k + 1)] col_max = [([0] * (n - k + 1)) for i in range(n - k + 1)] for col in range(n - k + 1): s = [0] * n co = 0 for i in range(k): if t[i] != [-1, -1]: if ( t[i][0] >= col and t[i][0] <= col + k - 1 and t[i][1] >= col and t[i][1] <= col + k - 1 ): co += 1 s[i] = 1 row_max[col][0] = co for row in range(1, n - k + 1): if s[row - 1]: co -= 1 i = row + k - 1 if t[i] != [-1, -1]: if ( t[i][0] >= col and t[i][0] <= col + k - 1 and t[i][1] >= col and t[i][1] <= col + k - 1 ): co += 1 s[i] = 1 row_max[col][row] = co for row in range(n - k + 1): s = [0] * n co = 0 for i in range(k): if tt[i] != [-1, -1]: if ( tt[i][0] >= row and tt[i][0] <= row + k - 1 and tt[i][1] >= row and tt[i][1] <= row + k - 1 ): co += 1 s[i] = 1 col_max[0][row] = co for col in range(1, n - k + 1): if s[col - 1]: co -= 1 i = col + k - 1 if tt[i] != [-1, -1]: if ( tt[i][0] >= row and tt[i][0] <= row + k - 1 and tt[i][1] >= row and tt[i][1] <= row + k - 1 ): co += 1 s[i] = 1 col_max[col][row] = co ma = 0 for i in range(n - k + 1): for j in range(n - k + 1): ma = max(ma, row_max[i][j] + col_max[i][j]) ma += t.count([-1, -1]) ma += tt.count([-1, -1]) print(ma)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR LIST NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR LIST NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR LIST NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR LIST NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of $n$ rows and $n$ columns of square cells. The rows are numbered from $1$ to $n$, from top to bottom, and the columns are numbered from $1$ to $n$, from left to right. The position of a cell at row $r$ and column $c$ is represented as $(r, c)$. There are only two colors for the cells in cfpaint — black and white. There is a tool named eraser in cfpaint. The eraser has an integer size $k$ ($1 \le k \le n$). To use the eraser, Gildong needs to click on a cell $(i, j)$ where $1 \le i, j \le n - k + 1$. When a cell $(i, j)$ is clicked, all of the cells $(i', j')$ where $i \le i' \le i + k - 1$ and $j \le j' \le j + k - 1$ become white. In other words, a square with side equal to $k$ cells and top left corner at $(i, j)$ is colored white. A white line is a row or a column without any black cells. Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the number of rows and columns, and the size of the eraser. The next $n$ lines contain $n$ characters each without spaces. The $j$-th character in the $i$-th line represents the cell at $(i,j)$. Each character is given as either 'B' representing a black cell, or 'W' representing a white cell. -----Output----- Print one integer: the maximum number of white lines after using the eraser exactly once. -----Examples----- Input 4 2 BWWW WBBW WBBW WWWB Output 4 Input 3 1 BWB WWB BWB Output 2 Input 5 3 BWBBB BWBBB BBBBB BBBBB WBBBW Output 2 Input 2 2 BW WB Output 4 Input 2 1 WW WW Output 4 -----Note----- In the first example, Gildong can click the cell $(2, 2)$, then the working screen becomes: BWWW WWWW WWWW WWWB Then there are four white lines — the $2$-nd and $3$-rd row, and the $2$-nd and $3$-rd column. In the second example, clicking the cell $(2, 3)$ makes the $2$-nd row a white line. In the third example, both the $2$-nd column and $5$-th row become white lines by clicking the cell $(3, 2)$.
n, k = map(int, input().split()) board = [input() for _ in range(n)] columns = [([0] * n) for _ in range(n)] rows = [([0] * n) for _ in range(n)] whites = 0 for i in range(n): first = -1 last = 0 flag = False for pos in range(n): if flag == False: if board[i][pos] == "B": first = pos flag = True elif board[i][pos] == "B": last = pos if first == -1: whites += 1 continue if last == 0: last = first if last - first < k: for j in range(max(0, last - k + 1), first + 1): columns[i][j] += 1 for i in range(n): first = -1 last = 0 flag = False for pos in range(n): if flag == False: if board[pos][i] == "B": first = pos flag = True elif board[pos][i] == "B": last = pos if first == -1: whites += 1 continue if last == 0: last = first if last - first < k: for j in range(max(0, last - k + 1), first + 1): rows[j][i] += 1 ccounts = [([0] * n) for _ in range(n)] rcounts = [([0] * n) for _ in range(n)] for i in range(n): tmp = 0 for j in range(k): tmp += columns[j][i] ccounts[0][i] = tmp for j in range(1, n - k + 1): tmp += columns[j + k - 1][i] tmp -= columns[j - 1][i] ccounts[j][i] = tmp for i in range(n): tmp = 0 for j in range(k): tmp += rows[i][j] rcounts[i][0] = tmp for j in range(1, n - k + 1): tmp += rows[i][j + k - 1] tmp -= rows[i][j - 1] rcounts[i][j] = tmp ans = whites for x in range(n): for y in range(n): tmp = whites tmp += ccounts[x][y] tmp += rcounts[x][y] ans = max(ans, tmp) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note that B could be any subarray of A, including the entire array A.) Given an array A of integers, return the length of the longest mountain.  Return 0 if there is no mountain. Example 1: Input: [2,1,4,7,3,2,5] Output: 5 Explanation: The largest mountain is [1,4,7,3,2] which has length 5. Example 2: Input: [2,2,2] Output: 0 Explanation: There is no mountain. Note: 0 <= A.length <= 10000 0 <= A[i] <= 10000 Follow up: Can you solve it using only one pass? Can you solve it in O(1) space?
class Solution: def longestMountain(self, A: List[int]) -> int: res = cur = 1 desc = False for i in range(1, len(A)): if A[i - 1] == A[i]: if desc: res = max(res, cur) desc = False cur = 1 elif A[i - 1] < A[i]: if not desc: cur += 1 else: res = max(res, cur) cur = 2 desc = False elif desc: cur += 1 elif cur > 1: desc = True cur += 1 else: cur = 1 desc = False if desc: res = max(res, cur) return 0 if res < 3 and not desc else res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER VAR NUMBER VAR VAR
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note that B could be any subarray of A, including the entire array A.) Given an array A of integers, return the length of the longest mountain.  Return 0 if there is no mountain. Example 1: Input: [2,1,4,7,3,2,5] Output: 5 Explanation: The largest mountain is [1,4,7,3,2] which has length 5. Example 2: Input: [2,2,2] Output: 0 Explanation: There is no mountain. Note: 0 <= A.length <= 10000 0 <= A[i] <= 10000 Follow up: Can you solve it using only one pass? Can you solve it in O(1) space?
class Solution: def longestMountain(self, A: List[int]) -> int: long_mont = 0 i, j = 0, 0 while j < len(A): incr = False while j != len(A) - 1 and A[j] < A[j + 1]: incr = True j += 1 if not incr: j += 1 i = j continue decr = False while j != len(A) - 1 and A[j] > A[j + 1]: decr = True j += 1 if not decr: j += 1 i = j continue curr_len = j - i + 1 if curr_len >= 3: long_mont = max(long_mont, curr_len) i = j return long_mont
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note that B could be any subarray of A, including the entire array A.) Given an array A of integers, return the length of the longest mountain.  Return 0 if there is no mountain. Example 1: Input: [2,1,4,7,3,2,5] Output: 5 Explanation: The largest mountain is [1,4,7,3,2] which has length 5. Example 2: Input: [2,2,2] Output: 0 Explanation: There is no mountain. Note: 0 <= A.length <= 10000 0 <= A[i] <= 10000 Follow up: Can you solve it using only one pass? Can you solve it in O(1) space?
class Solution: def longestMountain(self, A: List[int]) -> int: i = 0 answer = 0 while i < len(A) - 1: j = i + 1 visited_top = False while j < len(A): print(i, j) if A[j] > A[j - 1]: if visited_top: break if A[j] < A[j - 1]: if not visited_top: if j - i == 1: break visited_top = True if A[j] == A[j - 1]: break j += 1 if visited_top and j - i > 2: answer = max(answer, j - i) i = j if visited_top: i -= 1 return answer
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER RETURN VAR VAR
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note that B could be any subarray of A, including the entire array A.) Given an array A of integers, return the length of the longest mountain.  Return 0 if there is no mountain. Example 1: Input: [2,1,4,7,3,2,5] Output: 5 Explanation: The largest mountain is [1,4,7,3,2] which has length 5. Example 2: Input: [2,2,2] Output: 0 Explanation: There is no mountain. Note: 0 <= A.length <= 10000 0 <= A[i] <= 10000 Follow up: Can you solve it using only one pass? Can you solve it in O(1) space?
class Solution: def longestMountain(self, A: List[int]) -> int: if not A or len(A) == 1: return 0 m = len(A) up, down = [(0) for i in range(m)], [(0) for i in range(m)] up[0], down[-1] = 0, 0 for i in range(1, m): j = m - 1 - i if A[i] > A[i - 1]: up[i] = up[i - 1] + 1 else: up[i] = 0 if A[j] > A[j + 1]: down[j] = down[j + 1] + 1 else: down[j] = 0 ans = 0 for i in range(m): if up[i] and down[i]: ans = max(ans, up[i] + down[i] + 1) return ans
CLASS_DEF FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note that B could be any subarray of A, including the entire array A.) Given an array A of integers, return the length of the longest mountain.  Return 0 if there is no mountain. Example 1: Input: [2,1,4,7,3,2,5] Output: 5 Explanation: The largest mountain is [1,4,7,3,2] which has length 5. Example 2: Input: [2,2,2] Output: 0 Explanation: There is no mountain. Note: 0 <= A.length <= 10000 0 <= A[i] <= 10000 Follow up: Can you solve it using only one pass? Can you solve it in O(1) space?
class Solution: def longestMountain(self, A: List[int]) -> int: is_peak = False index = 1 longest = 0 while index < len(A) - 1: if A[index] > A[index - 1] and A[index] > A[index + 1]: is_peak = True if not is_peak: index += 1 continue left = index - 1 while left - 1 >= 0 and A[left] > A[left - 1]: left -= 1 right = index + 1 while right + 1 < len(A) and A[right] > A[right + 1]: right += 1 longest = max(longest, right - left + 1) index = right is_peak = False return longest
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR