description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() c = 0 d = {} res = 0 d[0] = 1 for i in s: if i == "1": c += 1 res += d.get(c - k, 0) d[c] = d.get(c, 0) + 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) l = input() if k == 0: c = 0 ans = 0 for i in range(len(l)): if l[i] == "0": c += 1 else: temp = c * (c + 1) // 2 ans += temp c = 0 ans += c * (c + 1) // 2 print(ans) else: ans = 0 ar = [] a = -1 for i in range(len(l)): if l[i] == "1": ar.append(i - a - 1) a = i ar.append(len(l) - a - 1) p = len(ar) - 1 for i in range(len(ar)): if i + k > p: break ans += ar[i] * ar[i + k] + ar[i] + ar[i + k] + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) string = input().strip() dp = [(0) for a in range(0, len(string))] if string[0] == "0": dp[0] = 0 else: dp[0] = 1 for a in range(1, len(string)): if string[a] == "0": dp[a] = dp[a - 1] else: dp[a] = dp[a - 1] + 1 hashmap = [(0) for a in range(0, max(dp) + 1)] for a in range(0, len(dp)): hashmap[dp[a]] += 1 try: if k == 0: ans = hashmap[k] for d in range(k, len(hashmap)): ans += hashmap[d] * (hashmap[d - k] - 1) // 2 else: ans = hashmap[k] for d in range(k, len(hashmap)): ans += hashmap[d] * hashmap[d - k] print(ans) except IndexError: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input().strip() s = list(s) dp = [(0) for i in range(len(s))] cnt = 0 for i in range(len(s)): if s[i] == "1": cnt += 1 dp[i] = cnt ans = 0 m = {} lb = 0 for i in range(len(s)): if dp[i] >= k: lb = i break m[dp[i]] = m.get(dp[i], 0) + 1 for i in range(lb, len(s)): p = dp[i] - k if p == 0: ans += 1 ans += m.get(p, 0) m[dp[i]] = m.get(dp[i], 0) + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
from itertools import accumulate k = int(input()) pre_sum = list(accumulate([int(i) for i in input()])) seen = {(0): 1} ans = 0 for i in pre_sum: a = i - k if a in seen: ans += seen[a] if i in seen: seen[i] += 1 else: seen[i] = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() t = list(map(len, s.split("1"))) ans = 0 if k >= len(t): ans = 0 elif k == 0: for i in t: ans += i * (i + 1) // 2 else: for i in range(len(t) - k): ans += (t[i] + 1) * (t[k + i] + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) l = [0] * (k + 1) l[0] = 1 s = input() u = 1 if s.count("1") < k: print(0) exit() else: x = [] z, y = 0, 0 ans = 0 t = 0 g = 0 r = 0 for i in s: if i == "0": z += 1 if r == 1: ans += 1 if i == "1" and k != 0: ans = ans + g * z x.append(z) z = 0 y += 1 if y % k == 0: ans = ans + x[t] + 1 g = x[t] t += 1 r = 1 elif y > k: ans = ans + x[t] + 1 g = x[t] t += 1 r = 1 if i == "1" and k == 0: ans += z * (z + 1) // 2 z = 0 if k != 0: print(ans + g * z) if k == 0: print(ans + z * (z + 1) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() n = len(s) one_idx = [] for i in range(n): if s[i] == "1": one_idx.append(i) m = len(one_idx) if k == 0: if one_idx: sol = one_idx[0] * (one_idx[0] + 1) // 2 for i in range(m - 1): zero_len = one_idx[i + 1] - one_idx[i] - 1 sol += zero_len * (zero_len + 1) // 2 zero_len = n - one_idx[m - 1] - 1 sol += zero_len * (zero_len + 1) // 2 else: sol = n * (n + 1) // 2 elif k > m: sol = 0 else: sol = 0 for i in range(m - k + 1): l = one_idx[i] r = one_idx[i + k - 1] if i == 0: ll = -1 else: ll = one_idx[i - 1] if i == m - k: rr = n else: rr = one_idx[i + k] a = l - ll b = rr - r sol += a * b print(sol)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
def fun(s): aa = c = 0 for i in range(len(s)): if s[i] == "0": c += 1 else: aa += c * (c + 1) // 2 c = 0 aa += c * (c + 1) // 2 return aa def check(mid): one = pre[mid] - pre[i - 1] return one > k def check1(r): return pre[r] - pre[i - 1] == k k = int(input()) s = input() n = len(s) if k == 0: print(fun(s)) exit() pre = [0] * (n + 3) zer = [0] * (n + 3) for i in range(n): if s[i] == "1": pre[i] = pre[i - 1] + 1 zer[i] = 1 else: pre[i] = pre[i - 1] zer[i] = zer[i - 1] + 1 ans = 0 for i in range(n): l = i r = n while l <= r: mid = l + (r - l) // 2 if check(mid): r = mid - 1 else: l = mid + 1 if i <= r: if check1(r): ans += zer[r] elif check1(r - 1): ans += zer[r - 1] print(ans)
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR 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 FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
n = int(input()) s = input() i, j, pre, aft = 0, 0, 0, 0 num, ans = 0, 0 while i < len(s) and s[i] != "1": i += 1 pre += 1 j = i while num != n and j < len(s): if s[j] == "1": num += 1 j += 1 while j < len(s) and s[j] != "1": j += 1 aft += 1 if i != j: ans += (pre + 1) * (aft + 1) else: ans += int(pre * (pre + 1) / 2) if num != n: ans = 0 else: while j < len(s): pre = 0 aft = 0 i += 1 j += 1 while i < len(s) and s[i] != "1": i += 1 pre += 1 while j < len(s) and s[j] != "1": j += 1 aft += 1 if i != j: ans += (pre + 1) * (aft + 1) else: ans += int(pre * (pre + 1) / 2) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() ones = [i for i in range(len(s)) if s[i] == "1"] start = 0 end = k - 1 ans = 0 if k == 0: ones.insert(0, -1) ones.append(len(s)) for i in range(1, len(ones)): n = ones[i] - ones[i - 1] - 1 ans += n * (n + 1) // 2 else: while end < len(ones): left = 0 right = 0 if start > 0: left = ones[start] - ones[start - 1] - 1 else: left = ones[start] if end < len(ones) - 1: right = ones[end + 1] - ones[end] - 1 else: right = len(s) - 1 - ones[end] ans += (right + 1) * (left + 1) start += 1 end += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) cnt = 0 current = 0 d = {(0): 1} for char in input(): current += int(char) cnt += d.get(current - k, 0) d[current] = 1 + d.get(current, 0) print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
import sys def sumofn(n): if n % 2 == 0: return (n + 1) * (n // 2) else: return n * (n // 2) + n def answer(k, s): n1 = s.count("1") if k > n1: return 0 addr = [-1] for i, char in enumerate(s): if char == "1": addr.append(i) addr.append(len(s)) cnt = 0 if k == 0: for i in range(1, n1 + 1 + 1, 1): cnt += sumofn(addr[i] - addr[i - 1] - 1) return cnt for i in range(1, n1 - k + 1 + 1, 1): cnt += (addr[i] - addr[i - 1]) * (addr[i + k] - addr[i + k - 1]) return cnt def main(): k = int(sys.stdin.readline()) s = sys.stdin.readline().rstrip() print(answer(k, s)) return main()
IMPORT FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR RETURN NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() n = len(s) dp = [0] * n cnt = 0 for i in range(n): cnt += int(s[i]) dp[i] = cnt d = {(0): 1} ans = 0 for i in dp: a = i - k ans += d.get(a, 0) d[i] = d.get(i, 0) + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) d, c, r = {(0): 1}, 0, 0 for x in input(): c += x == "1" r += d.get(c - k, 0) d[c] = d.get(c, 0) + 1 print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR DICT NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
def main(): k = int(input()) s = "1" + input() + "1" ans = 0 q = [] for i in range(len(s)): if s[i] == "1": q.append(i) for i in range(len(q) - k - 1): x, y = q[i], q[i + k + 1] l, r = q[i + 1], q[i + k] if k == 0: a = y - x ans += a * (a - 1) // 2 else: ans += (l - x) * (y - r) print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
def _bs(e, arr, le): high = l low = le mid = (high + low) // 2 while high - low > 1: if e >= arr[mid]: low = mid elif e < arr[mid]: high = mid mid = (low + high) // 2 if arr[high] == e: return high return low def bs(e, arr): low = 0 high = l mid = high // 2 while high - low > 1: if e > arr[mid]: low = mid elif e <= arr[mid]: high = mid mid = (low + high) // 2 if arr[low] == e: return low, _bs(e, arr, low) return high, _bs(e, arr, high) k = int(input()) s = input() l = len(s) dp = [0] * (l + 1) for i in range(l): if s[i] == "1": dp[i + 1] = dp[i] + 1 else: dp[i + 1] = dp[i] summ = 0 for i in range(1, l + 1): c = dp[i] + k if dp[i] != dp[i - 1]: c -= 1 x, y = bs(c, dp) if dp[y] == c: summ += y - max(x, i) + 1 print(summ)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
import itertools k = int(input()) s = "1" + input() + "1" if k == 0: ans = 0 for k, v in itertools.groupby(s): if k == "0": l = len(list(v)) ans += l * (l + 1) // 2 print(ans) exit() cnt = 0 d = [] for i, c in enumerate(s, start=1): if c == "1": d.append(i) ans = 0 l = 1 r = 1 + k - 1 while r < len(d) - 1: ans += (d[l] - d[l - 1]) * (d[r + 1] - d[r]) l += 1 r += 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
j, k, t = 0, int(input()), input() p = [1] + [0] * t.count("1") for i in t: if i == "1": j += 1 p[j] += 1 print( sum(p[i] * p[i + k] if p[i] > 1 else p[i + k] for i in range(len(p) - k)) if k else sum(i * (i - 1) for i in p if i > 1) // 2 )
ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER FUNC_CALL VAR STRING FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
from sys import stdin, stdout k = int(stdin.readline()) s = stdin.readline().strip() values = list(map(int, list(s))) cnt = [values[0]] for i in range(1, len(s)): cnt.append(cnt[-1] + values[i]) cnt.append(0) ans = 0 for i in range(len(s) - k + 1): if cnt[-2] - cnt[i - 1] < k: continue l, r = i - 1, len(s) while r - l > 1: m = (r + l) // 2 if cnt[m] - cnt[i - 1] > k: r = m else: l = m right = l if l == i - 1: continue l, r = i - 1, len(s) - 1 while r - l > 1: m = (r + l) // 2 if cnt[m] - cnt[i - 1] >= k: r = m else: l = m left = r ans += right - left + 1 stdout.write(str(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
n = int(input()) s = str(input()) c = s.count("1") l = [] if n > c: print(0) elif n == 0: f = 0 count = 0 for i in range(len(s)): if s[i] == "1": l.append(i) if len(l) == 0: count += len(s) * (len(s) + 1) // 2 else: for i in range(len(l)): if i == len(l) - 1: bb = len(s) - l[i] - 1 bb = bb * (bb + 1) // 2 count += bb zz = l[i] - f count += zz * (zz + 1) // 2 f = l[i] + 1 print(count) else: f = 0 count = 0 for i in range(len(s)): if s[i] == "1": l.append(i) for i in range(c - n + 1): count += l[i] - f + 1 w = i + n if i == c - n: w = len(s) - l[-1] - 1 count += w else: w = l[w] - l[w - 1] - 1 count += w if i != 0 and i != c - n: zz = (l[i + n] - l[i + n - 1] - 1) * (l[i] - l[i - 1] - 1) count += zz elif c == n: zz = l[i] * (len(s) - l[i + n - 1] - 1) count += zz elif i == 0: zz = (l[i + n] - l[i + n - 1] - 1) * l[i] count += zz else: zz = (l[i] - l[i - 1] - 1) * (len(s) - l[i + n - 1] - 1) count += zz f = l[i] + 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
I = lambda: int(input()) IL = lambda: list(map(int, input().split())) k = I() s = [len(i) for i in input().split("1")] if k: print(sum((i + 1) * (j + 1) for i, j in zip(s[:-k], s[k:]))) else: print(sum(i * (i + 1) // 2 for i in s))
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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() n = len(s) i1 = 0 dp = [0] * n cnt = 0 ind = [0] if k == 0: ocnt = 0 ans = 0 for i in range(n): if s[i] == "0": ocnt += 1 else: ans += max(ocnt * (ocnt + 1) // 2, 0) ocnt = 0 ans += max(ocnt * (ocnt + 1) // 2, 0) print(ans) else: for i in range(n): if s[i] == "1": cnt += 1 ind.append(i + 1) if cnt >= k: dp[i] = ind[-k] - ind[-k - 1] else: dp[i] = dp[i - 1] print(sum(dp))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
n = int(input()) s = input() l, q, w = [], 0, 0 for i in s: if i == "0": w += 1 else: l.append(w) w = 0 l += [w] if n > 0: for i in range(n, len(l)): q += (l[i] + 1) * (l[i - n] + 1) else: for i in l: q += i * (i + 1) // 2 print(q)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR LIST VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent. For each photo it is known which orientation is intended for it β€” horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo. Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos. Help Vasya find the maximum number of photos he is able to watch during T seconds. -----Input----- The first line of the input contains 4 integers n, a, b, T (1 ≀ n ≀ 5Β·10^5, 1 ≀ a, b ≀ 1000, 1 ≀ T ≀ 10^9) β€” the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo. Second line of the input contains a string of length n containing symbols 'w' and 'h'. If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation. If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation. -----Output----- Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds. -----Examples----- Input 4 2 3 10 wwhw Output 2 Input 5 2 4 13 hhwhh Output 4 Input 5 2 4 1000 hhwhh Output 5 Input 3 1 100 10 whw Output 0 -----Note----- In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds. Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
def main(): n, a, b, t = list(map(int, input().split())) b += 1 l = [(b if char == "w" else 1) for char in input()] t -= sum(l) - a * (n + 2) hi, n2 = n, n * 2 n3 = n2 + 1 lo = res = 0 l *= 2 while lo <= n and hi < n2: t -= l[hi] hi += 1 while (hi - lo + (hi if hi < n3 else n3)) * a > t: t += l[lo] lo += 1 n3 -= 1 if res < hi - lo: res = hi - lo if res >= n: res = n break print(res) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent. For each photo it is known which orientation is intended for it β€” horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo. Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos. Help Vasya find the maximum number of photos he is able to watch during T seconds. -----Input----- The first line of the input contains 4 integers n, a, b, T (1 ≀ n ≀ 5Β·10^5, 1 ≀ a, b ≀ 1000, 1 ≀ T ≀ 10^9) β€” the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo. Second line of the input contains a string of length n containing symbols 'w' and 'h'. If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation. If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation. -----Output----- Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds. -----Examples----- Input 4 2 3 10 wwhw Output 2 Input 5 2 4 13 hhwhh Output 4 Input 5 2 4 1000 hhwhh Output 5 Input 3 1 100 10 whw Output 0 -----Note----- In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds. Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
read = lambda: list(map(int, input().split())) per = lambda L, R: R - L - 1 + min(R - n - 1, n - L) n, a, b, T = read() f = [(1 + (i == "w") * b) for i in input()] * 2 L, R = 0, n ans = 0 cur = sum(f) // 2 while L <= n and R < n * 2: cur += f[R] R += 1 while R - L > n or cur + per(L, R) * a > T: cur -= f[L] L += 1 ans = max(ans, R - L) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR STRING VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent. For each photo it is known which orientation is intended for it β€” horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo. Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos. Help Vasya find the maximum number of photos he is able to watch during T seconds. -----Input----- The first line of the input contains 4 integers n, a, b, T (1 ≀ n ≀ 5Β·10^5, 1 ≀ a, b ≀ 1000, 1 ≀ T ≀ 10^9) β€” the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo. Second line of the input contains a string of length n containing symbols 'w' and 'h'. If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation. If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation. -----Output----- Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds. -----Examples----- Input 4 2 3 10 wwhw Output 2 Input 5 2 4 13 hhwhh Output 4 Input 5 2 4 1000 hhwhh Output 5 Input 3 1 100 10 whw Output 0 -----Note----- In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds. Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
def main(): n, a, b, t = list(map(int, input().split())) a1 = a + 1 b += a1 l, res = [(b if c == "w" else a1) for c in input()], [] l[0] = x = l[0] - a if t <= x: print(int(t == x)) return f = res.append for dr in (0, 1): if dr: l[1:] = l[-1:-n:-1] tot = t for hi, x in enumerate(l): tot -= x if tot < 0: break else: print(n) return f(hi) tot += x hi -= 1 tot -= hi * a lo = n while True: while lo > 0 <= tot: lo -= 1 tot -= l[lo] f(n + hi - lo) if not (lo and hi): break while tot <= 0 < hi: tot += l[hi] + a hi -= 1 print(max(res)) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR STRING VAR VAR VAR FUNC_CALL VAR LIST ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR FOR VAR NUMBER NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR WHILE VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent. For each photo it is known which orientation is intended for it β€” horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo. Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos. Help Vasya find the maximum number of photos he is able to watch during T seconds. -----Input----- The first line of the input contains 4 integers n, a, b, T (1 ≀ n ≀ 5Β·10^5, 1 ≀ a, b ≀ 1000, 1 ≀ T ≀ 10^9) β€” the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo. Second line of the input contains a string of length n containing symbols 'w' and 'h'. If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation. If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation. -----Output----- Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds. -----Examples----- Input 4 2 3 10 wwhw Output 2 Input 5 2 4 13 hhwhh Output 4 Input 5 2 4 1000 hhwhh Output 5 Input 3 1 100 10 whw Output 0 -----Note----- In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds. Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
n, a, b, t = map(int, input().split()) b += 1 l = [(b if char == "w" else 1) for char in input()] t -= sum(l) - a * (n + 2) hi = n n2 = n * 2 n3 = n2 + 1 lo = 0 res = 0 l *= 2 while lo <= n and hi < n2: t -= l[hi] hi += 1 while (hi - lo + (hi if hi < n3 else n3)) * a > t or lo < hi - n: t += l[lo] lo += 1 n3 -= 1 if res < hi - lo: res = hi - lo if res == n: break print(res)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent. For each photo it is known which orientation is intended for it β€” horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo. Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos. Help Vasya find the maximum number of photos he is able to watch during T seconds. -----Input----- The first line of the input contains 4 integers n, a, b, T (1 ≀ n ≀ 5Β·10^5, 1 ≀ a, b ≀ 1000, 1 ≀ T ≀ 10^9) β€” the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo. Second line of the input contains a string of length n containing symbols 'w' and 'h'. If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation. If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation. -----Output----- Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds. -----Examples----- Input 4 2 3 10 wwhw Output 2 Input 5 2 4 13 hhwhh Output 4 Input 5 2 4 1000 hhwhh Output 5 Input 3 1 100 10 whw Output 0 -----Note----- In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds. Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
n, a, b, T = map(int, input().split()) s = input() prefsum = [0] * n suffsum = [0] * n for i in range(n): prefsum[i] = 1 if s[i] == "h" else b + 1 if i > 0: prefsum[i] += a + prefsum[i - 1] suffsum[-1] = a for i in range(n - 1, -1, -1): suffsum[i] += 1 if s[i] == "h" else b + 1 if i + 1 < n: suffsum[i] += a + suffsum[i + 1] res = 0 l = 1 for r in range(n): l = max(l, r + 1) while l < n and suffsum[l] + r * a + prefsum[r] > T: l += 1 if prefsum[r] > T: break res = max(res, r + 1 + n - l) l = 1 for r in range(n): l = max(l, r + 1) while l < n and suffsum[l] + (n - l) * a + prefsum[r] > T: l += 1 if prefsum[r] > T: break res = max(res, r + 1 + n - l) for r in range(n): if prefsum[r] <= T: res = max(res, r + 1) if r > 0 and suffsum[r] + prefsum[0] <= T: res = max(res, n - r + 1) print(res)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR STRING NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR STRING NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent. For each photo it is known which orientation is intended for it β€” horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo. Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos. Help Vasya find the maximum number of photos he is able to watch during T seconds. -----Input----- The first line of the input contains 4 integers n, a, b, T (1 ≀ n ≀ 5Β·10^5, 1 ≀ a, b ≀ 1000, 1 ≀ T ≀ 10^9) β€” the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo. Second line of the input contains a string of length n containing symbols 'w' and 'h'. If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation. If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation. -----Output----- Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds. -----Examples----- Input 4 2 3 10 wwhw Output 2 Input 5 2 4 13 hhwhh Output 4 Input 5 2 4 1000 hhwhh Output 5 Input 3 1 100 10 whw Output 0 -----Note----- In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds. Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, stdin.readline().split())) for _ in range(1): n, swipe, rotate, t = lst() s = input() ans = 0 pre = [0] * n if s[0] == "w": pre[0] = rotate + 1 else: pre[0] = 1 for i in range(1, n): pre[i] = pre[i - 1] + 1 + swipe if s[i] == "w": pre[i] += rotate if pre[i] <= t: ans = max(ans, i + 1) suf = [0] * (1 + n) for i in range(n - 1, -1, -1): suf[i] = suf[i + 1] + 1 + swipe if s[i] == "w": suf[i] += rotate for i in range(n): l = i + 1 r = n - 1 while l <= r: mid = l + r >> 1 if pre[i] + suf[mid] + min(i, n - mid) * swipe <= t: r = mid - 1 else: l = mid + 1 if pre[i] + suf[l] + min(i, n - l) * swipe <= t: ans = max(ans, i + 1 + n - l) print(ans)
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 NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR STRING VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR STRING VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) mod = 998244353 sol = 1 i = n - 1 j = m - 1 while sol > 0 and j >= 0: goal = b[j] s = 0 r = False while i >= 0 and a[i] >= goal: if r: s += 1 elif a[i] == goal: r = True s = 1 i -= 1 if j == 0: s = min(s, 1) if i >= 0: s = 0 sol = sol * s sol = sol % mod j -= 1 print(sol)
ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
import sys mod = 998244353 eps = 10**-9 def main(): import sys input = sys.stdin.readline N, M = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) A_unique = sorted(list(set(A))) A_set = set(A) B_set = set(B) if A_unique[0] not in B_set: print(0) exit() for b in B: if b not in A_set: print(0) exit() B_set.remove(A_unique[0]) pos = {} for i, a in enumerate(A): pos[a] = i ans = 1 r = -1 for a in A_unique: if a in B_set: i = pos[a] if i < r: print(0) exit() ans = ans * (i - r) % mod r = max(r, pos[a]) print(ans) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
mod = 998244353 n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) s = [] d = {} for i, x in enumerate(a): if x not in d: d[x] = [] d[x].append(i) while len(s) > 0 and x <= a[s[-1]]: s.pop() s.append(i) idx = [] i, j = 0, 0 while j < len(s) and i < m: if a[s[j]] == b[i]: idx.append(j) i += 1 j += 1 if a[s[0]] != b[0] or len(idx) < m: print(0) else: ans = 1 for i in range(1, m): p = idx[i] pre = a[s[p - 1]] ans = ans * (d[b[i]][-1] - d[pre][-1]) % mod print(ans)
ASSIGN VAR NUMBER ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
a, b = map(int, input().split()) answer = 1 mod = 998244353 z = list(map(int, input().split())) x = list(map(int, input().split())) i = a - 1 j = b - 1 while answer and j >= 0: c = 0 while i >= 0 and z[i] >= x[j]: if c: c += 1 elif z[i] == x[j]: c = 1 i -= 1 if j == 0: c = min(c, 1) if i >= 0: c = 0 answer = answer * c % mod j -= 1 print(answer)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL 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 VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
n, m = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) MOD = 998244353 out = 1 first = -1 curr = m - 1 for i in range(n - 1, -1, -1): while a[i] < b[curr]: if first > -1: out *= first - i out %= MOD else: out = 0 first = -1 curr -= 1 if curr < 0: out = 0 break if a[i] == b[curr] and first == -1: first = i if curr == 0 and first != -1: print(out) else: print(0)
ASSIGN VAR VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
import sys input = sys.stdin.readline n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) mod = 998244353 a = a[::-1] b = b[::-1] i, j = 0, 0 ans = 1 while ans and j < m: cnt = 0 while i < n and a[i] >= b[j]: if cnt: cnt += 1 elif a[i] == b[j]: cnt = 1 i += 1 if j == m - 1: cnt = min(cnt, 1) if i <= n - 1: cnt = 0 ans = ans * cnt % mod j += 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
def main(): n, m = map(int, input().split()) aa, bb = (list(map(int, input().split())) for _ in "ab") res, term, i, start = 1, bb[0], n - 1, 0 for b in reversed(bb): while i >= 0 and aa[i] > b: i -= 1 if i >= 0 and aa[i] == b: start = i else: res = 0 break while i >= 0 and aa[i] >= b: i -= 1 if b == term: break res = res * (start - i) % 998244353 print(res * (i < 0)) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
alen, blen = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) minIndex = [-1] * blen curMinIndex = blen - 1 failCount = 0 MOD = 998244353 for i in range(alen - 1, -1, -1): if curMinIndex >= 0 and a[i] == b[curMinIndex]: minIndex[curMinIndex] = i curMinIndex -= 1 elif curMinIndex >= 0 and a[i] < b[curMinIndex]: failCount = 1 break elif curMinIndex == -1 and a[i] < b[0]: failCount = 1 break for val in minIndex: if val == -1: failCount = 1 if failCount == 1: print(0) elif blen == 1: print(1) else: rightExtensionAmount = [0] * (blen - 1) curMinIndex = 0 for i in range(alen): if rightExtensionAmount[curMinIndex] == 0 and a[i] >= b[curMinIndex + 1]: rightExtensionAmount[curMinIndex] = minIndex[curMinIndex + 1] - i + 1 if a[i] < b[curMinIndex + 1]: rightExtensionAmount[curMinIndex] = 0 if i >= minIndex[curMinIndex + 1]: curMinIndex += 1 if curMinIndex == blen - 1: break ans = 1 for val in rightExtensionAmount: ans = ans * val % MOD print(ans)
ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) n -= 1 m -= 1 while n >= 0 and b[m] != a[n]: if b[m] > a[n]: print(0) exit(0) n -= 1 if n < 0 < m: print(0) exit(0) ans = 1 mod = 998244353 while m >= 0 and n >= 0: sana = 0 while n >= 0 and a[n] >= b[m]: sana += 1 n -= 1 if m: ans = ans * sana % mod m -= 1 while n >= 0 and b[m] != a[n]: if b[m] > a[n]: print(0) exit(0) n -= 1 if n < 0 < m: print(0) exit(0) print(ans if n < 0 and m < 0 else 0)
ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
from sys import gettrace, stdin if gettrace(): inputi = input else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def modInt(mod): class ModInt: def __init__(self, value): self.value = value % mod def __int__(self): return self.value def __eq__(self, other): return self.value == other.value def __hash__(self): return hash(self.value) def __add__(self, other): return ModInt(self.value + int(other)) def __sub__(self, other): return ModInt(self.value - int(other)) def __mul__(self, other): return ModInt(self.value * int(other)) def __floordiv__(self, other): return ModInt(self.value // int(other)) def __truediv__(self, other): return ModInt(self.value * pow(int(other), mod - 2, mod)) def __pow__(self, exp): return pow(self.value, int(exp), mod) def __str__(self): return str(self.value) return ModInt def main(): ModInt = modInt(998244353) n, m = map(int, inputi().split()) aa = [int(a) for a in inputi().split()] bb = [int(a) for a in inputi().split()] ap = n - 1 bp = m - 1 mn = 10000000000 prod = ModInt(1) while ap >= 0 and bp >= 0: b = bb[bp] mn = min(mn, aa[ap]) while mn > b and ap >= 0: ap -= 1 mn = min(mn, aa[ap]) if mn != b: print(0) return bpc = 0 while mn == b and ap >= 0: bpc += 1 ap -= 1 mn = min(mn, aa[ap]) if bp != 0: prod *= bpc bp -= 1 if ap != -1 or bp != -1: print(0) return print(prod) main()
IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR FUNC_DEF RETURN VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
inp = lambda: map(int, input().split(" ")) m, n = inp() arr1 = tuple(inp()) arr2 = tuple(inp()) ans = 1 i = m - 1 j = n - 1 while j >= 0 and ans: cnt = 0 seen = 0 if j == 0: if i >= 0 and min(arr1[0 : i + 1]) == arr2[j]: cnt = 1 else: while arr1[i] >= arr2[j] and i >= 0: if arr1[i] == arr2[j]: seen = 1 if seen: cnt += 1 i -= 1 ans = ans * cnt % 998244353 j -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
import sys input = lambda: sys.stdin.readline().rstrip() n, m = map(int, input().split()) A = [int(i) for i in input().split()][::-1] B = [int(i) for i in input().split()][::-1] mod = 998244353 cur = 0 ct = 0 Chk = False Ans = [0] * (m - 1) ans = 1 for i in range(n): if A[i] > B[cur] and Chk == False: continue elif A[i] == B[cur] and Chk == False: Chk = True ct = 1 elif A[i] < B[cur] and Chk == False: ans = 0 elif A[i] >= B[cur]: ct += 1 elif A[i] < B[cur] and cur == m - 1: ans = 0 else: Ans[cur] = ct ct = 0 Chk = False if cur + 1 <= m - 1 and A[i] == B[cur + 1]: ct = 1 Chk = True elif A[i] < B[cur + 1]: ans = 0 cur += 1 if not Chk: print(0) elif m == 1: if min(A) == B[0]: print(1) else: print(0) else: for a in Ans: ans = ans * a % mod print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
MOD = 998244353 n, m = map(int, input().split()) d = dict() a = [0] + list(map(int, input().split())) b = list(map(int, input().split())) for i in range(m): d[b[i]] = i c = [-1] * m for i, x in enumerate(a): if x in d: c[d[x]] = i idx = n ans = 1 for i in range(m - 1, -1, -1): while b[i] <= a[idx]: idx -= 1 if c[i] <= idx: print(0) exit(0) if i > 0: ans *= c[i] - idx elif idx > 0: print(0) exit(0) ans %= MOD print(ans)
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
import sys 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 LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] md = 998244353 n, m = MI() aa = LI() bb = LI() mn = [0] * n s = aa[-1] for i in range(n - 1, -1, -1): s = min(s, aa[i]) mn[i] = s r = n ans = 1 l = 0 for b in bb[::-1]: while r - 1 >= 0 and mn[r - 1] > b: r -= 1 if r == 0 or mn[r - 1] != b: ans = 0 break l = r - 1 while l - 1 >= 0 and mn[l - 1] == b: l -= 1 if l: ans = ans * (r - l) % md r = l if l: ans = 0 print(ans)
IMPORT 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 VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 1 m = a[-1] mod = 998244353 ml = [0] * n for i in range(n - 1, -1, -1): m = min(m, a[i]) ml[i] = m r = n l = 0 for temp in b[::-1]: while r - 1 >= 0 and ml[r - 1] > temp: r -= 1 if r == 0 or ml[r - 1] != temp: ans = 0 break l = r - 1 while l - 1 >= 0 and ml[l - 1] == temp: l -= 1 if l: ans = ans * (r - l) % mod r = l if l: ans = 0 print(ans)
ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
mod = 998244353 def solve(a, b): j = len(a) - 1 pos = 1 for i in reversed(range(1, len(b))): while j >= 0 and a[j] != b[i]: if a[j] < b[i]: return 0 j -= 1 if j < 0: return 0 x = j small = None y = None while j >= 0 and a[j] >= b[i]: j -= 1 if a[j] < b[i - 1]: return 0 if a[j] == b[i - 1]: y = j small = j while j >= 0 and y is None: j -= 1 if a[j] < b[i - 1]: return 0 if a[j] == b[i - 1]: y = j if y is None: return 0 pos *= (x - small) % mod x = None while j >= 0: if a[j] < b[0]: return 0 if a[j] == b[0]: x = j j -= 1 if x is None: return 0 return pos % mod input() a = list(map(int, input().split())) b = list(map(int, input().split())) print(solve(a, b))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NONE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NONE RETURN NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NONE WHILE VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NONE RETURN NUMBER RETURN BIN_OP VAR VAR EXPR 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 VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
import sys input = sys.stdin.readline t = 1 while t > 0: t -= 1 n, m = map(int, input().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] if min(a) != b[0]: print(0) exit(0) ff = {} for i in range(n - 1, -1, -1): if a[i] not in ff: ff[a[i]] = i d = [(0) for i in range(n + 1)] st = [] for i in range(n - 1, -1, -1): if len(st) == 0: st.append([a[i], i]) else: while len(st) > 0 and st[-1][0] > a[i]: d[st[-1][1]] = i st.pop() st.append([a[i], i]) while len(st): d[st[-1][1]] = -1 st.pop() mod = 998244353 g = {} for i in b: g[i] = 1 for i in range(n): if len(st) == 0: st.append([a[i], i]) else: while len(st) > 0 and st[-1][0] > a[i]: if st[-1][0] in g and ff[st[-1][0]] == st[-1][1]: print(0) exit(0) st.pop() st.append([a[i], i]) c = 1 for i in range(1, m): if b[i] not in ff: print(0) exit(0) c = c * (-d[ff[b[i]]] + ff[b[i]]) % mod print(c)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR IF VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) M = [a[i] for i in range(n)] for i in range(n - 2, -1, -1): M[i] = min(M[i], M[i + 1]) res = 1 mod = 998244353 val = b[m - 1] id = n - 1 count = m - 1 l, r = -1, -1 while count and id > -1: if M[id] > val: id -= 1 elif M[id] == val: if r == -1: r = id id -= 1 elif val > M[id]: l = id + 1 res *= max(r - l + 1, 0) res %= mod count -= 1 r = -1 l = -1 val = b[count] if count: res = 0 print(res) else: check = min(a[i] for i in range(id + 1)) if check == b[0]: print(res) else: print(0)
ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
mod = 998244353 def solv(A, B): ans = 1 ai, bi = len(A) - 1, len(B) - 1 cnt = 0 match = False while ai >= 0 and bi >= 0: if match: if A[ai] >= B[bi]: cnt += 1 ai -= 1 else: ans = ans * cnt % mod match = False bi -= 1 cnt = 0 else: if A[ai] < B[bi]: return 0 if A[ai] == B[bi]: match = True cnt = 1 ai -= 1 return ans if match and bi == 0 else 0 _ = input() A = list(map(int, list(input().split()))) B = list(map(int, list(input().split()))) print(solv(A, B))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
def binaryfun(x, arr): low = 0 high = len(arr) - 1 mid = 0 while low <= high: mid = (high + low) // 2 if arr[mid] < x: low = mid + 1 elif arr[mid] > x: high = mid - 1 else: return mid return -1 inp = list(map(int, input().split())) n = inp[0] m = inp[1] list1 = list(map(int, input().split())) list2 = list(map(int, input().split())) if min(list1) < list2[0]: print(0) else: ans = 0 check = [-1] * m j = 0 for i in range(n): index = binaryfun(list1[i], list2) if index != -1: check[index] = i ans = 0 if -1 in check: print(0) else: if check == sorted(check): ans = 1 for i in range(m - 1): temp = check[i] for j in range(check[i] + 1, check[i + 1]): if list1[check[i]] < list1[j] < list1[check[i + 1]]: temp = j elif list1[j] < list1[check[i + 1]]: ans = 0 break if ans == 0: break check[i] = temp ans = ans * (check[i + 1] - check[i]) templist = list1[check[-1] :] for i in templist: if i < list1[check[-1]]: ans = 0 break print(ans % 998244353)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL 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 VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) mod = 998244353 indexOf = {} for i in range(n): indexOf[a[i]] = i for elem in b: if elem not in indexOf: print(0) exit() for i in range(m - 1): if indexOf[b[i]] > indexOf[b[i + 1]]: print(0) exit() if min(a) < b[0]: print(0) exit() for i in range(indexOf[b[-1]], n): if a[i] < b[-1]: print(0) exit() ans = 1 for i in range(m - 1): possible = True x = indexOf[b[i]] y = indexOf[b[i + 1]] lastLower = x for index in range(x + 1, y): if a[index] < b[i]: possible = False break if a[index] < b[i + 1]: lastLower = index if not possible: print(0) exit() ans *= indexOf[b[i + 1]] - lastLower ans %= mod print(ans)
ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
import sys input = sys.stdin.buffer.readline def getAnswer(a, b): a = list(reversed(a)) b = list(reversed(b)) if len(b) == 1: if min(a) == b[0]: return 1 else: return 0 res = 1 x = 0 y = 0 j = 0 while True: found = False for x in range(y, len(a)): if a[x] < b[j]: return 0 if a[x] == b[j]: found = True break if found == False: return 0 found = False for y in range(x, len(a)): if a[y] < b[j + 1]: return 0 if a[y] == b[j + 1]: found = True break if found == False: return 0 for mid in range(x, y + 1): if a[mid] < b[j]: break res *= mid - x res %= 998244353 j += 1 if j == len(b) - 1: break if min(a[y:]) < b[-1]: return 0 return res n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] print(getAnswer(a, b) % 998244353)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
import sys input = sys.stdin.buffer.readline def prog(): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) R = [(0) for i in range(m - 1)] L = [(0) for i in range(m - 1)] idx = n - 1 fail = False i = 1 for i in range(m - 1, 0, -1): right = b[i] left = b[i - 1] while a[idx] != right: if idx == 0 or a[idx] < right: fail = True break idx -= 1 if fail: break else: idx -= 1 if idx == -1: fail = True break R[i - 1] = idx while a[idx] >= right: if idx == 0: fail = True break idx -= 1 if a[idx] < left: fail = True if fail: break else: L[i - 1] = idx if fail: break if i == 1 and fail == False: fail = True last = b[0] for i in range(idx, -1, -1): if a[i] == last: fail = False elif a[i] < last: fail = True break if fail: print(0) else: total = 1 for i in range(m - 1): total = total * (R[i] - L[i] + 1) % 998244353 print(total) prog()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR 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 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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
import sys pprint = lambda s: print(" ".join(map(str, s))) input = lambda: sys.stdin.readline().strip() ipnut = input n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) mod = 998244353 suf_min = [(10000000000, -1)] a_r = list(reversed(a)) for i in range(n): if a_r[i] < suf_min[-1][0]: suf_min.append((a_r[i], n - i - 1)) else: suf_min.append(suf_min[-1]) suf_min.reverse() if suf_min[0][0] != b[0]: print(0) exit(0) ans = 1 def bin(a): l = 0 r = n + 1 while r - l > 1: m = (r + l) // 2 if suf_min[m][0] > a: r = m else: l = m return l for k in range(m - 1): j = bin(b[k + 1]) if suf_min[j][0] != b[k + 1]: print(0) exit(0) ans = ans * (suf_min[j][1] - suf_min[bin(b[k + 1] - 1)][1]) % mod print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
from sys import stdin input = stdin.buffer.readline n, m = map(int, input().split()) (*a,) = map(int, input().split()) (*b,) = map(int, input().split()) p = n - 1 mn = 10**9 + 7 ans = 1 for i in range(m - 1, 0, -1): if mn < b[i]: exit(print(0)) idx = -1 while p >= 0: if idx == -1 and a[p] == b[i]: idx = p if a[p] < b[i]: break p -= 1 mn = min(mn, a[p]) if idx == -1 or p == -1: exit(print(0)) ans *= idx - p ans %= 998244353 if min(a[: p + 1]) == b[0]: print(ans) else: print(0)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
n, m = map(int, input().split()) ai = list(map(int, input().split())) maxn = 10**9 + 1 bi = list(map(int, input().split())) + [maxn] mod = 998244353 bi2 = [0] * m bi3 = [0] * m j = m - 1 temp = 0 for i in range(n - 1, -1, -1): if ai[i] == bi[j]: temp = 1 bi2[j] = i j -= 1 if j == -1: break continue elif ai[i] < bi[j]: j = -2 break if ai[i] >= bi[j + 1] and temp == 1: bi3[j] += 1 else: temp = 0 if j != -1 or min(bi) > min(ai): print(0) else: ans = 1 for i in range(m - 1): ans *= bi3[i] + 1 ans %= mod print(ans)
ASSIGN VAR 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 BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
from sys import stdin input = stdin.readline n, m = [int(x) for x in input().strip().split()] a = [int(x) for x in input().strip().split()] b = [int(x) for x in input().strip().split()] MOD = 998244353 ans = 1 ai, bi = n - 1, m - 1 while bi >= 0: while ai >= 0 and a[ai] != b[bi]: if a[ai] < b[bi]: ans = 0 bi -= 1 break ai -= 1 else: cur_start = ai while ai >= 0 and a[ai] >= b[bi]: ai -= 1 cur_end = ai if bi: ans *= cur_start - cur_end elif ai >= 0 or cur_start - cur_end == 0: ans = 0 ans %= MOD bi -= 1 print(ans)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
def getint(): return int(input()) def getint2(): return map(int, input().split()) def getintlist(): return map(int, input().split()) def getstring(): return input() n, m = getint2() a = list(getintlist()) b = list(getintlist()) for i in range(n - 2, -1, -1): if a[i] > a[i + 1]: a[i] = a[i + 1] ans = 1 mod = 998244353 if a[0] != b[0]: print(0) else: r, ans, l = n, 1, 0 for b in b[::-1]: while r - 1 >= 0 and a[r - 1] > b: r -= 1 if r == 0 or a[r - 1] != b: ans = 0 break l = r - 1 while l - 1 >= 0 and a[l - 1] == b: l -= 1 if l: ans = ans * (r - l) % mod r = l if l: ans = 0 print(ans)
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 VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
import sys input = sys.stdin.readline I = lambda: list(map(int, input().split())) n, m = I() a = I() b = I() an = 1 md = 998244353 suf = [0] * n suf[-1] = a[n - 1] for i in range(n - 2, -1, -1): suf[i] = min(suf[i + 1], a[i]) suf = [0] + suf cr = m - 1 ct = 0 for i in range(n, -1, -1): if suf[i] < b[cr]: if ct == 0 or i < n and suf[i + 1] != b[cr]: an = 0 break if i == 0: cr -= 1 break cr -= 1 an = an * ct % md ct = 0 if suf[i] == b[cr]: ct += 1 if ct == 0 or cr != -1: an = 0 print(an)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
n, m = map(int, input().split()) lstn = list(map(int, input().split())) b = list(map(int, input().split())) hr = [(0) for i in range(m)] hhr = [(-1) for i in range(m)] hhl = [(-1) for i in range(m)] def bnp(lst, elem, pos): l = -1 r = len(lst) while r - l > 1: h = (r + l) // 2 if lst[h] > elem: r = h else: l = h if l == -1: print(0) exit() if lst[l] == elem: hr[l] = 1 hhr[l] = pos if l != len(hhl) - 1: hhl[l + 1] = pos + 1 return l mod = 998244353 bnpp = [bnp(b, lstn[i], i) for i in range(n)] num = 0 if sum(hr) != m: print(0) exit() ans = 1 for i in range(1, m): if hhr[i] - hhl[i] + 1 <= 0: print(0) exit() ans *= hhr[i] - hhl[i] + 1 ans %= mod print(ans)
ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
import sys input = sys.stdin.readline mod = 998244353 N, M = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) bind = M - 1 defined = [-1] * M ans = 1 for i in reversed(range(N)): a = A[i] if defined[bind] == -1: if B[bind] > a: ans = 0 break elif B[bind] == a: defined[bind] = i elif B[bind] > a: if bind > 0 and B[bind - 1] <= a: ans = ans * (defined[bind] - i) % mod if B[bind - 1] == a: defined[bind - 1] = i bind -= 1 else: ans = 0 break if bind != 0 or defined[bind] == -1: ans = 0 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) it, ans, c, d, M = m - 1, 0, [0] * m, [[0, 0] for i in range(m)], 998244353 for i in range(n - 1, -1, -1): if it <= 0: break if a[i] < b[it]: if c[it]: d[it][0], d[it - 1][1] = i + 1, i it -= 1 else: print(ans) exit() if a[i] == b[it]: c[it] += 1 d[-1][-1], ans, ANS = n - 1, [1] * m, 1 for i in range(m): if min(a[d[i][0] : d[i][1] + 1]) != b[i]: print("0") exit() for i in range(m - 1): for j in range(d[i][1] + 1, d[i + 1][1]): if a[j] == b[i + 1]: c[i + 1] -= 1 if c[i + 1]: ans[i] += 1 else: break ANS = ANS % M * (ans[i] % M) % M print(ANS)
ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP LIST NUMBER VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
n, m = input().split() n = int(n) m = int(m) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] MOD = 998244353 a.reverse() b.reverse() def Solve(a, b, n, m): if m > n: return 0 men = a[0] pos = 0 while pos < n and men > b[0]: pos += 1 if pos >= n: break men = min(men, a[pos]) if pos == n or men < b[0]: return 0 result = 1 count = 0 while count != m - 1: newPos = pos mid = True while newPos < n and men != b[count + 1]: newPos += 1 if newPos >= n: break men = min(men, a[newPos]) if mid and a[newPos] < b[count]: mid = False result = result * (newPos - pos) % MOD if newPos == n or men != b[count + 1]: return 0 count += 1 pos = newPos for i in range(pos, n): if a[i] < men: men = a[i] if men == b[m - 1]: return result else: return 0 print(Solve(a, b, n, m))
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
from sys import stdin, stdout def two_arrasy(n, m, a, b): r = n - 1 res = 1 for i in range(m - 1, -1, -1): while r >= 0 and a[r] > b[i]: r -= 1 if r < 0 or b[i] != a[r]: return 0 l = r while l >= 0 and a[l] >= b[i]: l -= 1 if l < 0 and i != 0: return 0 if i == 0: if l >= 0 and a[l] < b[i]: return 0 else: break res *= r - l res %= 998244353 r = l return res n, m = list(map(int, stdin.readline().split())) a = list(map(int, stdin.readline().split())) b = list(map(int, stdin.readline().split())) res = two_arrasy(n, m, a, b) stdout.write(str(res) + "\n")
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
import sys n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) last = [] i = n - 1 j = m - 1 MOD = 998244353 failflag = 0 while j >= 0 and i >= 0: r = b[j] while i >= 0: if a[i] == r: last.append(i) break if a[i] < r: failflag = 1 break i -= 1 if i < 0: break j -= 1 last.reverse() if len(last) != m: print(0) return for i in range(last[0]): if a[i] < b[0]: failflag = 1 if failflag == 1: print(0) return ans = 1 for i in range(1, m): for j in range(last[i], last[i - 1] - 1, -1): if a[j] < b[i]: break ans = (last[i] - j) * ans % MOD print(ans)
IMPORT ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
n, m = map(int, input().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] c = [(0) for _ in range(n)] mn = a[-1] for i in range(n - 1, -1, -1): mn = min(mn, a[i]) c[i] = mn j = n - 1 r = 1 for i in range(m - 1, -1, -1): k = 0 while j >= 0 and c[j] > b[i]: j -= 1 if j < 0 or c[j] != b[i]: r = 0 break while j >= 0 and c[j] == b[i]: k += 1 j -= 1 if i != 0: r = r * k % 998244353 if j != -1: print(0) else: print(r)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
mod = 998244353 n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 1 i = n - 1 for j in range(m - 1, -1, -1): l, r = -1, -1 while i != -1: if a[i] < b[j]: print(0) exit() if a[i] == b[j]: r = i break i -= 1 else: print(0) exit() while i != -1: if a[i] < b[j]: l = i + 1 if j == 0: print(0) exit() break i -= 1 else: if j != 0: print(0) exit() if j != 0: ans *= r - l + 1 ans %= mod print(ans)
ASSIGN VAR NUMBER ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
n, m = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) def solve(): M = 998244353 res = 1 i = n - 1 j = m - 1 while j >= 0 and res: count = 0 while i >= 0 and a[i] >= b[j]: if count > 0: count += 1 elif a[i] == b[j]: count = 1 i -= 1 if j == 0: count = min(count, 1) if i >= 0: count = 0 res *= count res %= M j -= 1 return res print(solve())
ASSIGN VAR VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$)Β β€” the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$)Β β€” the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$)Β β€” the array $b$. -----Output----- In only line print one integer β€” the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
import sys def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + "\n") def wi(n): sys.stdout.write(str(n) + "\n") def wia(a): sys.stdout.write(" ".join([str(x) for x in a]) + "\n") MOD = 998244353 def solve(n, m, a, b): a.reverse() b.reverse() ad = {} for i in range(n): if a[i] not in ad: ad[a[i]] = [] ad[a[i]].append(i) cnt = 1 if b[0] not in ad: return 0 x = ad[b[0]][0] min_ax = min(a[: x + 1]) if min_ax < b[0]: return 0 for i in range(1, m): if b[i] not in ad: return 0 k = 0 yy = ad[b[i]] while k < len(yy) and yy[k] < x: k += 1 if k > len(yy) - 1: return 0 y = yy[k] min_xy = min(a[x : y + 1]) if min_xy < b[i]: return 0 mid = x + 1 while mid <= y and a[mid] >= b[i - 1]: mid += 1 cnt = cnt * (mid - x) % MOD x = y min_last = min(a[x:]) if min_last < b[m - 1]: return 0 return cnt def main(): n, m = ria() a = ria() b = ria() wi(solve(n, m, a, b)) main()
IMPORT FUNC_DEF RETURN FUNC_CALL 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 EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
You are given a sequence $a$ of length $n$ consisting of $0$s and $1$s. You can perform the following operation on this sequence: Pick an index $i$ from $1$ to $n-2$ (inclusive). Change all of $a_{i}$, $a_{i+1}$, $a_{i+2}$ to $a_{i} \oplus a_{i+1} \oplus a_{i+2}$ simultaneously, where $\oplus$ denotes the bitwise XOR operation Find a sequence of at most $n$ operations that changes all elements of $a$ to $0$s or report that it's impossible. We can prove that if there exists a sequence of operations of any length that changes all elements of $a$ to $0$s, then there is also such a sequence of length not greater than $n$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case contains a single integer $n$ ($3 \le n \le 2\cdot10^5$) β€” the length of $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i = 0$ or $a_i = 1$) β€” elements of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case, do the following: if there is no way of making all the elements of $a$ equal to $0$ after performing the above operation some number of times, print "NO". otherwise, in the first line print "YES", in the second line print $k$ ($0 \le k \le n$) β€” the number of operations that you want to perform on $a$, and in the third line print a sequence $b_1, b_2, \dots, b_k$ ($1 \le b_i \le n - 2$) β€” the indices on which the operation should be applied. If there are multiple solutions, you may print any. -----Examples----- Input 3 3 0 0 0 5 1 1 1 1 0 4 1 0 0 1 Output YES 0 YES 2 3 1 NO -----Note----- In the first example, the sequence contains only $0$s so we don't need to change anything. In the second example, we can transform $[1, 1, 1, 1, 0]$ to $[1, 1, 0, 0, 0]$ and then to $[0, 0, 0, 0, 0]$ by performing the operation on the third element of $a$ and then on the first element of $a$. In the third example, no matter whether we first perform the operation on the first or on the second element of $a$ we will get $[1, 1, 1, 1]$, which cannot be transformed to $[0, 0, 0, 0]$.
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) AA = now = 0 for i in range(n): now ^= a[i] if not now and not i % 2: AA = i + 1 if not AA or now: print("NO") else: print("YES") an = [] for i in range(1, AA, 2): an.append(str(i)) for i in range(AA + 1, n - 1, 2): an.append(str(i)) an.extend(an[::-1]) print(len(an)) print(" ".join(an))
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 VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a sequence $a$ of length $n$ consisting of $0$s and $1$s. You can perform the following operation on this sequence: Pick an index $i$ from $1$ to $n-2$ (inclusive). Change all of $a_{i}$, $a_{i+1}$, $a_{i+2}$ to $a_{i} \oplus a_{i+1} \oplus a_{i+2}$ simultaneously, where $\oplus$ denotes the bitwise XOR operation Find a sequence of at most $n$ operations that changes all elements of $a$ to $0$s or report that it's impossible. We can prove that if there exists a sequence of operations of any length that changes all elements of $a$ to $0$s, then there is also such a sequence of length not greater than $n$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case contains a single integer $n$ ($3 \le n \le 2\cdot10^5$) β€” the length of $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i = 0$ or $a_i = 1$) β€” elements of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case, do the following: if there is no way of making all the elements of $a$ equal to $0$ after performing the above operation some number of times, print "NO". otherwise, in the first line print "YES", in the second line print $k$ ($0 \le k \le n$) β€” the number of operations that you want to perform on $a$, and in the third line print a sequence $b_1, b_2, \dots, b_k$ ($1 \le b_i \le n - 2$) β€” the indices on which the operation should be applied. If there are multiple solutions, you may print any. -----Examples----- Input 3 3 0 0 0 5 1 1 1 1 0 4 1 0 0 1 Output YES 0 YES 2 3 1 NO -----Note----- In the first example, the sequence contains only $0$s so we don't need to change anything. In the second example, we can transform $[1, 1, 1, 1, 0]$ to $[1, 1, 0, 0, 0]$ and then to $[0, 0, 0, 0, 0]$ by performing the operation on the third element of $a$ and then on the first element of $a$. In the third example, no matter whether we first perform the operation on the first or on the second element of $a$ we will get $[1, 1, 1, 1]$, which cannot be transformed to $[0, 0, 0, 0]$.
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) r = 0 mid = -1 for i, c in enumerate(arr): r ^= c if r == 0 and i % 2 == 0: mid = i if r: print("NO") elif n % 2 == 1: print("YES") ans = [] ans.extend(list(range(1, n - 1, 2))) ans.extend(list(range(n - 2, 0, -2))) print(len(ans)) print(*ans) elif n % 2 == 0: if mid == -1: print("NO") else: print("YES") ans = [] ans.extend(list(range(1, mid, 2))) ans.extend(list(range(mid - 1, 0, -2))) ans.extend(list(range(mid + 2, n - 1, 2))) ans.extend(list(range(n - 2, mid + 1, -2))) print(len(ans)) 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 NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The only difference between the two versions is the constraint on n. You can make hacks only if all versions of the problem are solved. A forest is an undirected graph without cycles (not necessarily connected). Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from 1 to n, and they would like to add edges to their forests such that: * After adding edges, both of their graphs are still forests. * They add the same edges. That is, if an edge (u, v) is added to Mocha's forest, then an edge (u, v) is added to Diana's forest, and vice versa. Mocha and Diana want to know the maximum number of edges they can add, and which edges to add. Input The first line contains three integers n, m_1 and m_2 (1 ≀ n ≀ 10^5, 0 ≀ m_1, m_2 < n) β€” the number of nodes and the number of initial edges in Mocha's forest and Diana's forest. Each of the next m_1 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Mocha's forest. Each of the next m_2 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Diana's forest. Output The first line contains only one integer h, the maximum number of edges Mocha and Diana can add. Each of the next h lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edge you add each time. If there are multiple correct answers, you can print any one of them. Examples Input 3 2 2 1 2 2 3 1 2 1 3 Output 0 Input 5 3 2 5 4 2 1 4 3 4 3 1 4 Output 1 2 4 Input 8 1 2 1 7 2 6 1 5 Output 5 5 2 2 3 3 4 4 7 6 8 Note In the first example, we cannot add any edge. In the second example, the initial forests are as follows. <image> We can add an edge (2, 4). <image>
n, m1, m2 = map(int, input().split()) def getfa(fa, x): if fa[x] == x: return x else: fa[x] = getfa(fa, fa[x]) return fa[x] fa1 = [i for i in range(n + 1)] fa2 = [i for i in range(n + 1)] for i in range(m1): u, v = map(int, input().split()) t1 = getfa(fa1, u) t2 = getfa(fa1, v) if t1 < t2: t1, t2 = t2, t1 fa1[t1] = t2 for i in range(m2): u, v = map(int, input().split()) t1 = getfa(fa2, u) t2 = getfa(fa2, v) if t1 < t2: t1, t2 = t2, t1 fa2[t1] = t2 res = [] for i in range(1, n + 1): t1 = getfa(fa1, i) t2 = getfa(fa2, i) if t1 != 1 and t2 != 1: res.append((1, i)) fa1[t1] = 1 fa2[t2] = 1 al = [0] * (n + 1) ar = [0] * (n + 1) bl = [0] * (n + 1) br = [0] * (n + 1) cl = [] cr = [] for i in range(1, n + 1): if getfa(fa1, i) == 1: al[i] = 1 else: ar[i] = 1 for i in range(1, n + 1): if getfa(fa2, i) == 1: bl[i] = 1 if ar[i] == 1: cl.append(i) else: br[i] = 1 if al[i] == 1: cr.append(i) while cl and cr: if getfa(fa1, cl[-1]) == 1: if getfa(fa2, cl[-1]) != 1: cr.append(cl[-1]) ar[cl[-1]] = 0 cl.pop() continue if getfa(fa2, cr[-1]) == 1: if al[cr[-1]] == 1: cl.append(cr[-1]) br[cl[-1]] = 0 cr.pop() continue t1 = getfa(fa1, cl[-1]) t2 = getfa(fa1, cr[-1]) t3 = getfa(fa2, cl[-1]) t4 = getfa(fa2, cr[-1]) res.append((cl[-1], cr[-1])) fa1[t1] = 1 fa1[t2] = 1 fa2[t3] = 1 fa2[t4] = 1 cl.pop() cr.pop() print(len(res)) for i in range(len(res)): print(res[i][0], res[i][1])
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
This is the hard version of the problem. The only difference between the two versions is the constraint on n. You can make hacks only if all versions of the problem are solved. A forest is an undirected graph without cycles (not necessarily connected). Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from 1 to n, and they would like to add edges to their forests such that: * After adding edges, both of their graphs are still forests. * They add the same edges. That is, if an edge (u, v) is added to Mocha's forest, then an edge (u, v) is added to Diana's forest, and vice versa. Mocha and Diana want to know the maximum number of edges they can add, and which edges to add. Input The first line contains three integers n, m_1 and m_2 (1 ≀ n ≀ 10^5, 0 ≀ m_1, m_2 < n) β€” the number of nodes and the number of initial edges in Mocha's forest and Diana's forest. Each of the next m_1 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Mocha's forest. Each of the next m_2 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Diana's forest. Output The first line contains only one integer h, the maximum number of edges Mocha and Diana can add. Each of the next h lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edge you add each time. If there are multiple correct answers, you can print any one of them. Examples Input 3 2 2 1 2 2 3 1 2 1 3 Output 0 Input 5 3 2 5 4 2 1 4 3 4 3 1 4 Output 1 2 4 Input 8 1 2 1 7 2 6 1 5 Output 5 5 2 2 3 3 4 4 7 6 8 Note In the first example, we cannot add any edge. In the second example, the initial forests are as follows. <image> We can add an edge (2, 4). <image>
class DisjointSet: def __init__(self, size): self.U = [[i, 1] for i in range(size)] def root(self, e): u = self.U[e] if u[0] != e: r = self.U[e] = self.root(u[0]) return r return u def union(self, first, second): uFirst, uSecond = self.root(first), self.root(second) if uFirst[1] < uSecond[1]: uFirst, uSecond = uSecond, uFirst uSecond[0] = uFirst[0] uFirst[1] += uSecond[1] def size(e): u = self.U[e] if u[0] != e: r = self.U[e] = self.root(u[0]) return r[1] return u[1] n, m1, m2 = map(int, input().split(" ")) DS1 = DisjointSet(n) for _ in range(m1): s, e = map(int, input().split(" ")) DS1.union(s - 1, e - 1) DS2 = DisjointSet(n) for _ in range(m2): s, e = map(int, input().split(" ")) DS2.union(s - 1, e - 1) V1, V2 = list(range(n)), list(range(n)) U = [] while len(V1) > 0 and len(V2) > 0: x = V1[0] while len(V1) > 1 and DS1.root(V1[-1]) == DS1.root(x): V1.pop() if DS1.root(V1[-1]) != DS1.root(x): y = V1[-1] else: break if DS2.root(y) != DS2.root(x): DS1.union(x, y) DS2.union(x, y) U.append((x, y)) V1.pop() else: u = V2[0] while len(V2) > 1 and DS2.root(V2[-1]) == DS2.root(x): V2.pop() if DS2.root(V2[-1]) != DS2.root(u): v = V2[-1] else: break if DS2.root(x) != DS2.root(u): if DS1.root(x) != DS1.root(u): DS1.union(x, u) DS2.union(x, u) U.append((x, u)) else: DS1.union(y, u) DS2.union(y, u) U.append((y, u)) elif DS1.root(x) != DS1.root(v): DS1.union(x, v) DS2.union(x, v) U.append((x, v)) else: DS1.union(y, v) DS2.union(y, v) U.append((y, v)) print(len(U)) for s, e in U: print("{} {}".format(s + 1, e + 1))
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER
This is the hard version of the problem. The only difference between the two versions is the constraint on n. You can make hacks only if all versions of the problem are solved. A forest is an undirected graph without cycles (not necessarily connected). Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from 1 to n, and they would like to add edges to their forests such that: * After adding edges, both of their graphs are still forests. * They add the same edges. That is, if an edge (u, v) is added to Mocha's forest, then an edge (u, v) is added to Diana's forest, and vice versa. Mocha and Diana want to know the maximum number of edges they can add, and which edges to add. Input The first line contains three integers n, m_1 and m_2 (1 ≀ n ≀ 10^5, 0 ≀ m_1, m_2 < n) β€” the number of nodes and the number of initial edges in Mocha's forest and Diana's forest. Each of the next m_1 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Mocha's forest. Each of the next m_2 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Diana's forest. Output The first line contains only one integer h, the maximum number of edges Mocha and Diana can add. Each of the next h lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edge you add each time. If there are multiple correct answers, you can print any one of them. Examples Input 3 2 2 1 2 2 3 1 2 1 3 Output 0 Input 5 3 2 5 4 2 1 4 3 4 3 1 4 Output 1 2 4 Input 8 1 2 1 7 2 6 1 5 Output 5 5 2 2 3 3 4 4 7 6 8 Note In the first example, we cannot add any edge. In the second example, the initial forests are as follows. <image> We can add an edge (2, 4). <image>
import sys input = sys.stdin.readline class Node: def __init__(self, index, p=None, rank=None): self.index = index self.p = p self.rank = rank def make_set(self): self.p = self self.rank = 0 def find_set(self): if self != self.p: self.p = self.p.find_set() return self.p def link(self, x): if self.rank > x.rank: x.p = self else: self.p = x if self.rank == x.rank: x.rank += 1 def union(self, x): return self.find_set().link(x.find_set()) n, m1, m2 = list(map(int, input().split())) inp1 = [list(map(int, input().split())) for _ in range(m1)] inp2 = [list(map(int, input().split())) for _ in range(m2)] arr1 = [Node(i) for i in range(n)] for i in arr1: i.make_set() arr2 = [Node(i) for i in range(n)] queue1 = list(range(n - 1, -1, -1)) queue2 = list(range(n - 1, -1, -1)) for i in arr2: i.make_set() for i, j in inp1: arr1[i - 1].union(arr1[j - 1]) for i, j in inp2: arr2[i - 1].union(arr2[j - 1]) ans1 = 0 ans2 = [] end = 0 while len(queue1) >= 2 and len(queue2) >= 2: end = 0 while True: if len(queue1) >= 2: i1, i2 = queue1[-1], queue1[-2] x1, x2 = arr1[i1].find_set(), arr1[i2].find_set() if x1 is x2: queue1.pop(-1) continue else: i1, i2 = x1.index, x2.index break else: end = 1 break while True: if len(queue2) >= 2: i3, i4 = queue2[-1], queue2[-2] x3, x4 = arr2[i3].find_set(), arr2[i4].find_set() if x3 is x4: queue2.pop(-1) continue else: i3, i4 = x3.index, x4.index break else: end = 1 break if end: break for x in (i1, i2, i3, i4): for y in (i1, i2, i3, i4): if x != y: if arr1[x].find_set() is arr1[y].find_set(): continue if arr2[x].find_set() is arr2[y].find_set(): continue end = 1 break if end: break arr1[x].union(arr1[y]) arr2[x].union(arr2[y]) ans1 += 1 ans2.append((x + 1, y + 1)) print(ans1) for i, j in ans2: print(str(i) + " " + str(j))
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF NONE NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
This is the hard version of the problem. The only difference between the two versions is the constraint on n. You can make hacks only if all versions of the problem are solved. A forest is an undirected graph without cycles (not necessarily connected). Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from 1 to n, and they would like to add edges to their forests such that: * After adding edges, both of their graphs are still forests. * They add the same edges. That is, if an edge (u, v) is added to Mocha's forest, then an edge (u, v) is added to Diana's forest, and vice versa. Mocha and Diana want to know the maximum number of edges they can add, and which edges to add. Input The first line contains three integers n, m_1 and m_2 (1 ≀ n ≀ 10^5, 0 ≀ m_1, m_2 < n) β€” the number of nodes and the number of initial edges in Mocha's forest and Diana's forest. Each of the next m_1 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Mocha's forest. Each of the next m_2 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Diana's forest. Output The first line contains only one integer h, the maximum number of edges Mocha and Diana can add. Each of the next h lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edge you add each time. If there are multiple correct answers, you can print any one of them. Examples Input 3 2 2 1 2 2 3 1 2 1 3 Output 0 Input 5 3 2 5 4 2 1 4 3 4 3 1 4 Output 1 2 4 Input 8 1 2 1 7 2 6 1 5 Output 5 5 2 2 3 3 4 4 7 6 8 Note In the first example, we cannot add any edge. In the second example, the initial forests are as follows. <image> We can add an edge (2, 4). <image>
import sys class UnionFind: def __init__(self, n): self.parent = list(range(n)) def find(self, a): acopy = a while a != self.parent[a]: a = self.parent[a] while acopy != a: self.parent[acopy], acopy = a, self.parent[acopy] return a def union(self, a, b): self.parent[self.find(b)] = self.find(a) input = sys.stdin.readline n, m1, m2 = map(int, input().split()) uf1 = UnionFind(n) uf2 = UnionFind(n) out = [] for _ in range(m1): u, v = map(int, input().split()) uf1.union(u - 1, v - 1) for _ in range(m2): u, v = map(int, input().split()) uf2.union(u - 1, v - 1) t10 = set() t01 = set() for i in range(1, n): f0 = uf1.find(0) == uf1.find(i) s0 = uf2.find(0) == uf2.find(i) if f0 and s0: pass elif f0: t01.add(i) elif s0: t10.add(i) else: out.append((0, i)) uf1.union(0, i) uf2.union(0, i) a = b = -1 while t01 and a == -1: a = t01.pop() if uf2.find(a) == uf2.find(0): a = -1 while t10 and b == -1: b = t10.pop() if uf1.find(b) == uf1.find(0): b = -1 if a != -1 and b != -1: out.append((a, b)) uf1.union(a, b) uf2.union(a, b) else: if a != -1: t01.add(a) if b != -1: t10.add(b) print(len(out)) l = [] for u, v in out: l.append(str(u + 1) + " " + str(v + 1)) print("\n".join(l))
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is the hard version of the problem. The only difference between the two versions is the constraint on n. You can make hacks only if all versions of the problem are solved. A forest is an undirected graph without cycles (not necessarily connected). Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from 1 to n, and they would like to add edges to their forests such that: * After adding edges, both of their graphs are still forests. * They add the same edges. That is, if an edge (u, v) is added to Mocha's forest, then an edge (u, v) is added to Diana's forest, and vice versa. Mocha and Diana want to know the maximum number of edges they can add, and which edges to add. Input The first line contains three integers n, m_1 and m_2 (1 ≀ n ≀ 10^5, 0 ≀ m_1, m_2 < n) β€” the number of nodes and the number of initial edges in Mocha's forest and Diana's forest. Each of the next m_1 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Mocha's forest. Each of the next m_2 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Diana's forest. Output The first line contains only one integer h, the maximum number of edges Mocha and Diana can add. Each of the next h lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edge you add each time. If there are multiple correct answers, you can print any one of them. Examples Input 3 2 2 1 2 2 3 1 2 1 3 Output 0 Input 5 3 2 5 4 2 1 4 3 4 3 1 4 Output 1 2 4 Input 8 1 2 1 7 2 6 1 5 Output 5 5 2 2 3 3 4 4 7 6 8 Note In the first example, we cannot add any edge. In the second example, the initial forests are as follows. <image> We can add an edge (2, 4). <image>
import sys input = sys.stdin.buffer.readline On, Pig, Cow = map(int, input().split()) class Farm: def __init__(self): self.Fa = [i for i in range(On)] def fa(self, x): if self.Fa[x] == x: return x self.Fa[x] = self.fa(self.Fa[x]) return self.Fa[x] def merge(self, a, b): if self.fa(a) < self.fa(b): a, b = b, a self.Fa[self.fa(a)] = self.fa(b) PigFarm = Farm() CowFarm = Farm() for i in range(Pig): a, b = map(int, input().split()) PigFarm.merge(a - 1, b - 1) for i in range(Cow): a, b = map(int, input().split()) CowFarm.merge(a - 1, b - 1) ans = [] for i in range(1, On): Piggy = PigFarm.fa(i) Cowwy = CowFarm.fa(i) if Piggy and Cowwy: ans.append((0, i)) PigFarm.merge(0, i) CowFarm.merge(0, i) LonelyAnimals = {"Pig": [], "Cow": []} for i in range(1, On): Piggy = PigFarm.fa(i) Cowwy = CowFarm.fa(i) if not Piggy and Cowwy == i: LonelyAnimals["Cow"].append(Cowwy) elif not Cowwy and Piggy == i: LonelyAnimals["Pig"].append(Piggy) for i in range(On): if len(LonelyAnimals["Cow"]) == i or len(LonelyAnimals["Pig"]) == i: break ans.append((LonelyAnimals["Cow"][i], LonelyAnimals["Pig"][i])) print(len(ans)) for i, j in ans: print(i + 1, j + 1)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR DICT STRING STRING LIST LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR STRING VAR VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
This is the hard version of the problem. The only difference between the two versions is the constraint on n. You can make hacks only if all versions of the problem are solved. A forest is an undirected graph without cycles (not necessarily connected). Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from 1 to n, and they would like to add edges to their forests such that: * After adding edges, both of their graphs are still forests. * They add the same edges. That is, if an edge (u, v) is added to Mocha's forest, then an edge (u, v) is added to Diana's forest, and vice versa. Mocha and Diana want to know the maximum number of edges they can add, and which edges to add. Input The first line contains three integers n, m_1 and m_2 (1 ≀ n ≀ 10^5, 0 ≀ m_1, m_2 < n) β€” the number of nodes and the number of initial edges in Mocha's forest and Diana's forest. Each of the next m_1 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Mocha's forest. Each of the next m_2 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Diana's forest. Output The first line contains only one integer h, the maximum number of edges Mocha and Diana can add. Each of the next h lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edge you add each time. If there are multiple correct answers, you can print any one of them. Examples Input 3 2 2 1 2 2 3 1 2 1 3 Output 0 Input 5 3 2 5 4 2 1 4 3 4 3 1 4 Output 1 2 4 Input 8 1 2 1 7 2 6 1 5 Output 5 5 2 2 3 3 4 4 7 6 8 Note In the first example, we cannot add any edge. In the second example, the initial forests are as follows. <image> We can add an edge (2, 4). <image>
class UnoinFind(object): def __init__(self, n): self.__findset = [i for i in range(n + 1)] self.__rank = [(1) for i in range(n + 1)] def find(self, x): if self.__findset[x] != x: self.__findset[x] = self.find(self.__findset[x]) return self.__findset[x] def union(self, p, q): p_root = self.find(p) q_root = self.find(q) if self.__rank[p_root] < self.__rank[q_root]: self.__findset[p_root] = q_root elif self.__rank[p_root] > self.__rank[q_root]: self.__findset[q_root] = p_root else: self.__findset[p_root] = q_root self.__rank[q_root] += 1 def is_connected(self, p, q): if self.find(p) == self.find(q): return True return False n, m1, m2 = map(int, input().split()) U1 = UnoinFind(n) U2 = UnoinFind(n) for _ in range(m1): u, v = map(int, input().split()) U1.union(u, v) for _ in range(m2): u, v = map(int, input().split()) U2.union(u, v) ans = [] stack1 = [] stack2 = [] for i in range(2, n + 1): flag1 = U1.is_connected(1, i) flag2 = U2.is_connected(1, i) if not (flag1 or flag2): ans.append((1, i)) U1.union(1, i) U2.union(1, i) elif flag1: stack1.append(i) elif flag2: stack2.append(i) while stack1 != [] and stack2 != []: if U1.is_connected(1, stack2[-1]): stack2.pop() continue if U2.is_connected(1, stack1[-1]): stack1.pop() continue ans.append((stack1[-1], stack2[-1])) U1.union(stack1[-1], stack2[-1]) U2.union(stack1[-1], stack2[-1]) stack1.pop() stack2.pop() print(len(ans)) for pair in ans: print(*pair)
CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR WHILE VAR LIST VAR LIST IF FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The only difference between the two versions is the constraint on n. You can make hacks only if all versions of the problem are solved. A forest is an undirected graph without cycles (not necessarily connected). Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from 1 to n, and they would like to add edges to their forests such that: * After adding edges, both of their graphs are still forests. * They add the same edges. That is, if an edge (u, v) is added to Mocha's forest, then an edge (u, v) is added to Diana's forest, and vice versa. Mocha and Diana want to know the maximum number of edges they can add, and which edges to add. Input The first line contains three integers n, m_1 and m_2 (1 ≀ n ≀ 10^5, 0 ≀ m_1, m_2 < n) β€” the number of nodes and the number of initial edges in Mocha's forest and Diana's forest. Each of the next m_1 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Mocha's forest. Each of the next m_2 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edges in Diana's forest. Output The first line contains only one integer h, the maximum number of edges Mocha and Diana can add. Each of the next h lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” the edge you add each time. If there are multiple correct answers, you can print any one of them. Examples Input 3 2 2 1 2 2 3 1 2 1 3 Output 0 Input 5 3 2 5 4 2 1 4 3 4 3 1 4 Output 1 2 4 Input 8 1 2 1 7 2 6 1 5 Output 5 5 2 2 3 3 4 4 7 6 8 Note In the first example, we cannot add any edge. In the second example, the initial forests are as follows. <image> We can add an edge (2, 4). <image>
from sys import stdin, stdout def get_list(): return list(map(int, stdin.readline().strip().split())) def get_int(): return int(stdin.readline()) def get_ints(): return map(int, stdin.readline().strip().split()) def get_string(): return stdin.readline() def printn(n): stdout.write(str(n) + "\n") class DSU: def __init__(self, n): self.par = list(range(0, n + 1)) self.sz = [1] * n def find(self, x): if x == self.par[x]: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def union(self, u, v): u = self.find(u) v = self.find(v) if u == v: return if u > v: u, v = v, u self.par[v] = u def check(self, u, v): u = self.find(u) v = self.find(v) if v == u: return False return True def solve(): n, m1, m2 = get_ints() g1 = DSU(n + 2) g2 = DSU(n + 2) while m1: m1 -= 1 u, v = get_ints() g1.union(u, v) while m2: m2 -= 1 u, v = get_ints() g2.union(u, v) tup = [] p1 = [] p2 = [] for i in range(2, n + 1): if g1.check(1, i) and g2.check(1, i): tup.append((1, i)) g1.union(1, i) g2.union(1, i) if g1.find(i) != 1: p1.append(i) if g2.find(i) != 1: p2.append(i) while p1 and p2: if g1.find(p1[-1]) == 1 and g2.find(p1[-1]) == 1: p1.pop(-1) continue if g1.find(p2[-1]) == 1 and g2.find(p2[-1]) == 1: p2.pop(-1) continue tup.append((p1[-1], p2[-1])) g1.union(p1[-1], p2[-1]) g2.union(p1[-1], p2[-1]) continue printn(len(tup)) for ans in tup: print(ans[0], ans[1]) solve()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
ChthollyNotaSeniorious received a special gift from AquaMoon: $n$ binary arrays of length $m$. AquaMoon tells him that in one operation, he can choose any two arrays and any position $pos$ from $1$ to $m$, and swap the elements at positions $pos$ in these arrays. He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $1$s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it! If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output $-1$. -----Input----- The first line of the input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $2 \leq m \leq 10^5$). The $i$-th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ β€” the elements of the $i$-th array. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$. -----Output----- For each test case, if the objective is not achievable, output $-1$. Otherwise, in the first line output $k$ $(0 \le k \le mn)$ β€” the minimum number of operations required. The $i$-th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$, which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$: swap the $z_i$-th number of the $x_i$-th and $y_i$-th arrays. -----Examples----- Input 3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1 Output 1 2 1 1 1 4 2 2 -1 -----Note----- In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become $[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]$, each of them contains exactly two $1$s.
t = int(input()) for i in range(t): n, m = [int(j) for j in input().split()] lst = [[int(k) for k in input().split()] for j in range(n)] sums = [] for l in lst: sums.append(sum(l)) if sum(sums) % n != 0: print(-1) continue even_sum = sum(sums) // n ops = [] for j in range(m): ones = [] for k in range(n): if sums[k] > even_sum and lst[k][j] == 1: ones.append(k) for k in range(n): if sums[k] < even_sum and lst[k][j] == 0 and len(ones) > 0: pos = ones.pop(0) sums[pos] -= 1 sums[k] += 1 lst[k][j], lst[pos][j] = 1, 0 ops.append(f"{pos + 1} {k + 1} {j + 1}") print(len(ops)) for j in ops: print(j)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
ChthollyNotaSeniorious received a special gift from AquaMoon: $n$ binary arrays of length $m$. AquaMoon tells him that in one operation, he can choose any two arrays and any position $pos$ from $1$ to $m$, and swap the elements at positions $pos$ in these arrays. He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $1$s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it! If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output $-1$. -----Input----- The first line of the input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $2 \leq m \leq 10^5$). The $i$-th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ β€” the elements of the $i$-th array. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$. -----Output----- For each test case, if the objective is not achievable, output $-1$. Otherwise, in the first line output $k$ $(0 \le k \le mn)$ β€” the minimum number of operations required. The $i$-th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$, which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$: swap the $z_i$-th number of the $x_i$-th and $y_i$-th arrays. -----Examples----- Input 3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1 Output 1 2 1 1 1 4 2 2 -1 -----Note----- In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become $[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]$, each of them contains exactly two $1$s.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = [] d = {} ts = 0 for i in range(n): a.append(list(map(int, input().split()))) d[i] = sum(a[i]) ts += d[i] if ts % n != 0: print(-1) continue am = ts // n ans = [] for i in range(m): mx = [] mn = [] for j in range(n): if d[j] > am and a[j][i] == 1: mx.append(j) elif d[j] < am and a[j][i] == 0: mn.append(j) for k in range(min(len(mx), len(mn))): ans.append((mx[k] + 1, mn[k] + 1, i + 1)) d[mx[k]] -= 1 d[mn[k]] += 1 print(len(ans)) for i in ans: print(*i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
ChthollyNotaSeniorious received a special gift from AquaMoon: $n$ binary arrays of length $m$. AquaMoon tells him that in one operation, he can choose any two arrays and any position $pos$ from $1$ to $m$, and swap the elements at positions $pos$ in these arrays. He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $1$s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it! If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output $-1$. -----Input----- The first line of the input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $2 \leq m \leq 10^5$). The $i$-th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ β€” the elements of the $i$-th array. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$. -----Output----- For each test case, if the objective is not achievable, output $-1$. Otherwise, in the first line output $k$ $(0 \le k \le mn)$ β€” the minimum number of operations required. The $i$-th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$, which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$: swap the $z_i$-th number of the $x_i$-th and $y_i$-th arrays. -----Examples----- Input 3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1 Output 1 2 1 1 1 4 2 2 -1 -----Note----- In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become $[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]$, each of them contains exactly two $1$s.
for jj in range(int(input())): n, m = [int(i) for i in input().split()] tm = [] tot = 0 for i in range(n): arr = input().split() a = arr.count("1") arr = [a] + arr + [i + 1] tm.append(arr) tot += a if tot % n != 0: print(-1) continue tm.sort() i, j = 0, n - 1 ans = [] p = tot // n while i < j: a = tm[i][0] b = tm[j][0] if a == b == p: i += 1 j -= 1 continue elif a == p: i += 1 continue elif b == p: j -= 1 continue k = min(p - a, b - p) for ii in range(1, m + 1): if k == 0: break if tm[j][ii] == "1": if tm[i][ii] == "0": tm[j][ii], tm[i][ii] = tm[i][ii], tm[j][ii] tm[i][0] += 1 tm[j][0] -= 1 ans.append((tm[i][-1], tm[j][-1], ii)) k -= 1 print(len(ans)) for i in ans: print(*i)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR STRING IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
ChthollyNotaSeniorious received a special gift from AquaMoon: $n$ binary arrays of length $m$. AquaMoon tells him that in one operation, he can choose any two arrays and any position $pos$ from $1$ to $m$, and swap the elements at positions $pos$ in these arrays. He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $1$s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it! If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output $-1$. -----Input----- The first line of the input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $2 \leq m \leq 10^5$). The $i$-th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ β€” the elements of the $i$-th array. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$. -----Output----- For each test case, if the objective is not achievable, output $-1$. Otherwise, in the first line output $k$ $(0 \le k \le mn)$ β€” the minimum number of operations required. The $i$-th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$, which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$: swap the $z_i$-th number of the $x_i$-th and $y_i$-th arrays. -----Examples----- Input 3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1 Output 1 2 1 1 1 4 2 2 -1 -----Note----- In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become $[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]$, each of them contains exactly two $1$s.
t = int(input()) for k in range(t): n, m = map(int, input().split()) a = [] aux = [] up = [] down = [] ss = 0 for i in range(n): c = 0 l = list(map(int, input().split())) a.append(l) for i in range(m): if l[i] == 1: c += 1 aux.append(c) s = sum(aux) if s % n != 0: print(-1) else: for i in range(n): if aux[i] == s / n: pass elif aux[i] < s / n: down.append(i) else: ss += aux[i] - s / n up.append(i) print(int(ss)) i = 0 o = 0 while i < len(up): flag = 0 while o < len(down) and flag == 0: fflag = 0 while aux[up[i]] > int(s / n) and aux[down[o]] < int(s / n): for j in range(m): if a[up[i]][j] == 1 and a[down[o]][j] == 0: a[up[i]][j] = 0 a[down[o]][j] = 1 print(up[i] + 1, down[o] + 1, j + 1) aux[up[i]] -= 1 aux[down[o]] += 1 if aux[up[i]] == int(s / n): flag = 1 if aux[down[o]] == int(s / n): fflag = 1 if flag == 1 or fflag == 1: break if flag == 1: i += 1 if fflag == 1: o += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER
ChthollyNotaSeniorious received a special gift from AquaMoon: $n$ binary arrays of length $m$. AquaMoon tells him that in one operation, he can choose any two arrays and any position $pos$ from $1$ to $m$, and swap the elements at positions $pos$ in these arrays. He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $1$s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it! If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output $-1$. -----Input----- The first line of the input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $2 \leq m \leq 10^5$). The $i$-th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ β€” the elements of the $i$-th array. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$. -----Output----- For each test case, if the objective is not achievable, output $-1$. Otherwise, in the first line output $k$ $(0 \le k \le mn)$ β€” the minimum number of operations required. The $i$-th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$, which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$: swap the $z_i$-th number of the $x_i$-th and $y_i$-th arrays. -----Examples----- Input 3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1 Output 1 2 1 1 1 4 2 2 -1 -----Note----- In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become $[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]$, each of them contains exactly two $1$s.
t = int(input()) for _ in range(t): n, m = [int(i) for i in input().strip().split()] a = [] count = 0 stri_1 = [[0, i] for i in range(n)] for i in range(n): temp = [int(i) for i in input().strip().split()] a.append(temp) for j in range(m): if temp[j] == 1: stri_1[i][0] += 1 count += 1 if count % n != 0: print(-1) else: ans = [] val = count // n stri_1.sort(reverse=True) i = 0 j = n - 1 while j > i: for k in range(m): if stri_1[i][0] == val: i += 1 break if stri_1[j][0] == val: j -= 1 break if a[stri_1[i][1]][k] == 1 and a[stri_1[j][1]][k] == 0: ans.append([stri_1[i][1] + 1, stri_1[j][1] + 1, k + 1]) a[stri_1[i][1]][k] = 0 a[stri_1[j][1]][k] = 1 stri_1[i][0] -= 1 stri_1[j][0] += 1 if stri_1[i][0] == val: i += 1 if stri_1[j][0] == val: j -= 1 print(len(ans)) for x in ans: print(*x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
ChthollyNotaSeniorious received a special gift from AquaMoon: $n$ binary arrays of length $m$. AquaMoon tells him that in one operation, he can choose any two arrays and any position $pos$ from $1$ to $m$, and swap the elements at positions $pos$ in these arrays. He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $1$s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it! If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output $-1$. -----Input----- The first line of the input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $2 \leq m \leq 10^5$). The $i$-th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ β€” the elements of the $i$-th array. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$. -----Output----- For each test case, if the objective is not achievable, output $-1$. Otherwise, in the first line output $k$ $(0 \le k \le mn)$ β€” the minimum number of operations required. The $i$-th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$, which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$: swap the $z_i$-th number of the $x_i$-th and $y_i$-th arrays. -----Examples----- Input 3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1 Output 1 2 1 1 1 4 2 2 -1 -----Note----- In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become $[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]$, each of them contains exactly two $1$s.
for tc in range(int(input())): n, m = map(int, input().split()) l = [] for i in range(n): l.append(list(map(int, input().split()))) c1 = [] for i in range(n): c1.append(l[i].count(1)) exp1 = sum(c1) // n exp0 = m - exp1 if sum(c1) % n != 0: print(-1) continue elif c1.count(exp1) == n: print(0) continue more = [] less = [] for i in range(n): if c1[i] > exp1: more.append(i) elif c1[i] < exp1: less.append(i) k = 0 count = 0 res = [] i = 0 while i < len(more): for j in range(m): if i >= len(more): break if l[more[i]][j] == 1 and l[less[k]][j] == 0: count += 1 res.append([more[i] + 1, less[k] + 1, j + 1]) l[more[i]][j] = 0 l[less[k]][j] = 1 c1[less[k]] += 1 c1[more[i]] -= 1 if c1[less[k]] == exp1: k += 1 if c1[more[i]] == exp1: i += 1 print(count) for i in res: print(i[0], end=" ") print(i[1], end=" ") print(i[2], end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
ChthollyNotaSeniorious received a special gift from AquaMoon: $n$ binary arrays of length $m$. AquaMoon tells him that in one operation, he can choose any two arrays and any position $pos$ from $1$ to $m$, and swap the elements at positions $pos$ in these arrays. He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $1$s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it! If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output $-1$. -----Input----- The first line of the input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $2 \leq m \leq 10^5$). The $i$-th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ β€” the elements of the $i$-th array. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$. -----Output----- For each test case, if the objective is not achievable, output $-1$. Otherwise, in the first line output $k$ $(0 \le k \le mn)$ β€” the minimum number of operations required. The $i$-th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$, which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$: swap the $z_i$-th number of the $x_i$-th and $y_i$-th arrays. -----Examples----- Input 3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1 Output 1 2 1 1 1 4 2 2 -1 -----Note----- In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become $[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]$, each of them contains exactly two $1$s.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) nums = [] cnt = [] for _ in range(n): lin = list(map(int, input().split())) cnt.append(lin.count(1)) nums.append(lin) sums = sum(cnt) h = sums // n if sums % n != 0: print(-1) continue for i in range(n): cnt[i] = cnt[i] - h has = [[] for _ in range(m)] need = [[] for _ in range(m)] for i in range(n): for j in range(m): if cnt[i] > 0 and nums[i][j] == 1: has[j].append(i) elif cnt[i] < 0 and nums[i][j] == 0: need[j].append(i) cao = [] for i in range(m): bur = cur = 0 while bur < len(has[i]) and cur < len(need[i]): if cnt[has[i][bur]] == 0: bur += 1 elif cnt[need[i][cur]] == 0: cur += 1 else: cao.append([has[i][bur], need[i][cur], i]) cnt[has[i][bur]] -= 1 cnt[need[i][cur]] += 1 bur += 1 cur += 1 print(len(cao)) for i in cao: print(i[0] + 1, i[1] + 1, i[2] + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
ChthollyNotaSeniorious received a special gift from AquaMoon: $n$ binary arrays of length $m$. AquaMoon tells him that in one operation, he can choose any two arrays and any position $pos$ from $1$ to $m$, and swap the elements at positions $pos$ in these arrays. He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $1$s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it! If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output $-1$. -----Input----- The first line of the input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $2 \leq m \leq 10^5$). The $i$-th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ β€” the elements of the $i$-th array. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$. -----Output----- For each test case, if the objective is not achievable, output $-1$. Otherwise, in the first line output $k$ $(0 \le k \le mn)$ β€” the minimum number of operations required. The $i$-th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$, which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$: swap the $z_i$-th number of the $x_i$-th and $y_i$-th arrays. -----Examples----- Input 3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1 Output 1 2 1 1 1 4 2 2 -1 -----Note----- In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become $[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]$, each of them contains exactly two $1$s.
import sys def parse(): t = int(sys.stdin.readline()) for i in range(t): n, m = list(map(int, sys.stdin.readline().split())) matrix = [] count_rows = [0] * n actions_count = 0 actions = [] for j in range(n): arr = list(map(int, sys.stdin.readline().split())) matrix.append(arr) count_rows[j] = sum(arr) ones_count = sum(count_rows) if ones_count % n != 0: print("-1") continue ones_row_count = ones_count // n less_idx = 0 large_idx = 0 while True: while less_idx < n and count_rows[less_idx] >= ones_row_count: less_idx += 1 while large_idx < n and count_rows[large_idx] <= ones_row_count: large_idx += 1 if less_idx == n: break for j in range(m): if matrix[large_idx][j] == 1 and matrix[less_idx][j] == 0: matrix[large_idx][j] = 0 matrix[less_idx][j] = 1 count_rows[large_idx] -= 1 count_rows[less_idx] += 1 actions.append((large_idx + 1, less_idx + 1, j + 1)) actions_count += 1 if ( count_rows[large_idx] == ones_row_count or count_rows[less_idx] == ones_row_count ): break print(actions_count) if actions_count > 0: for action_idx in range(actions_count): print(*actions[action_idx]) parse()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
ChthollyNotaSeniorious received a special gift from AquaMoon: $n$ binary arrays of length $m$. AquaMoon tells him that in one operation, he can choose any two arrays and any position $pos$ from $1$ to $m$, and swap the elements at positions $pos$ in these arrays. He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $1$s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it! If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output $-1$. -----Input----- The first line of the input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $2 \leq m \leq 10^5$). The $i$-th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ β€” the elements of the $i$-th array. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$. -----Output----- For each test case, if the objective is not achievable, output $-1$. Otherwise, in the first line output $k$ $(0 \le k \le mn)$ β€” the minimum number of operations required. The $i$-th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$, which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$: swap the $z_i$-th number of the $x_i$-th and $y_i$-th arrays. -----Examples----- Input 3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1 Output 1 2 1 1 1 4 2 2 -1 -----Note----- In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become $[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]$, each of them contains exactly two $1$s.
for i in range(int(input())): rows, cols = map(int, input().split()) mat = [] ones_arr = [] total_ones = 0 for i in range(rows): arr = list(map(int, input().split())) mat.append(arr) ones_count = arr.count(1) ones_arr.append(ones_count) total_ones += ones_count avg = total_ones / rows if avg != int(avg): print(-1) else: ava = int(avg) ans = [] for j in range(cols): extra = [] for i in range(rows): if ones_arr[i] > avg and mat[i][j] == 1: extra.append(i) for i in range(rows): if ones_arr[i] < avg and mat[i][j] == 0 and len(extra) > 0: donor = extra.pop() ans.append([i + 1, donor + 1, j + 1]) ones_arr[i] += 1 ones_arr[donor] -= 1 print(len(ans)) for i in ans: print(*i)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
ChthollyNotaSeniorious received a special gift from AquaMoon: $n$ binary arrays of length $m$. AquaMoon tells him that in one operation, he can choose any two arrays and any position $pos$ from $1$ to $m$, and swap the elements at positions $pos$ in these arrays. He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $1$s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it! If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output $-1$. -----Input----- The first line of the input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $2 \leq m \leq 10^5$). The $i$-th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ β€” the elements of the $i$-th array. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$. -----Output----- For each test case, if the objective is not achievable, output $-1$. Otherwise, in the first line output $k$ $(0 \le k \le mn)$ β€” the minimum number of operations required. The $i$-th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$, which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$: swap the $z_i$-th number of the $x_i$-th and $y_i$-th arrays. -----Examples----- Input 3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1 Output 1 2 1 1 1 4 2 2 -1 -----Note----- In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become $[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]$, each of them contains exactly two $1$s.
for _ in range(int(input())): n, m = [int(i) for i in input().split()] a = [] num = [0] * n for i in range(n): a.append([int(i) for i in input().split()]) num[i] = a[i].count(1) count = sum(num) if count % n: print(-1) continue count //= n mm = 0 for i in num: if i - count > 0: mm += i - count print(mm) i, j = 0, 0 while True: while i < n and num[i] <= count: i += 1 while j < n and num[j] >= count: j += 1 if i >= n or j >= n: break m = min(num[i] - count, count - num[j]) num[i] -= m num[j] += m f = 0 while m: if a[i][f] == 1 and a[j][f] == 0: a[i][f] = 0 a[j][f] = 1 print(i + 1, j + 1, f + 1) m -= 1 f += 1
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER
ChthollyNotaSeniorious received a special gift from AquaMoon: $n$ binary arrays of length $m$. AquaMoon tells him that in one operation, he can choose any two arrays and any position $pos$ from $1$ to $m$, and swap the elements at positions $pos$ in these arrays. He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $1$s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it! If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output $-1$. -----Input----- The first line of the input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $2 \leq m \leq 10^5$). The $i$-th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ $(0 \le a_{i, j} \le 1)$ β€” the elements of the $i$-th array. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$. -----Output----- For each test case, if the objective is not achievable, output $-1$. Otherwise, in the first line output $k$ $(0 \le k \le mn)$ β€” the minimum number of operations required. The $i$-th of the following $k$ lines should contain $3$ integers, $x_i, y_i, z_i$ $(1 \le x_i, y_i \le n, 1 \le z_i \le m)$, which describe an operation that swap $a_{x_i, z_i}, a_{y_i, z_i}$: swap the $z_i$-th number of the $x_i$-th and $y_i$-th arrays. -----Examples----- Input 3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1 Output 1 2 1 1 1 4 2 2 -1 -----Note----- In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become $[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]$, each of them contains exactly two $1$s.
import sys input = sys.stdin.readline for _ in range(int(input())): n, m = list(map(int, input().split())) l = [] ct = [] for x in range(n): a = list(map(int, input().split())) l.append(a) cnt = 0 for y in a: if y == 1: cnt += 1 ct.append(cnt) oct = sum(ct) if oct % n != 0: print(-1) else: oct = int(oct / n) inc = dict() drc = dict() for i in range(n): if ct[i] > oct: inc[i] = ct[i] - oct elif ct[i] < oct: drc[i] = oct - ct[i] x = list(inc.keys()) y = list(drc.keys()) x1 = 0 y1 = 0 print(sum(list(inc.values()))) while x1 < len(x) and y1 < len(y): for j in range(m): if l[x[x1]][j] == 1 and l[y[y1]][j] == 0: print(x[x1] + 1, y[y1] + 1, j + 1) l[x[x1]][j] = 0 l[y[y1]][j] = 1 f = 0 inc[x[x1]] -= 1 drc[y[y1]] -= 1 if inc[x[x1]] == 0: x1 += 1 f = 1 if drc[y[y1]] == 0: y1 += 1 f = 1 if f: break if x1 == len(x) or y1 == len(y): break
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
Edo has got a collection of n refrigerator magnets! He decided to buy a refrigerator and hang the magnets on the door. The shop can make the refrigerator with any size of the door that meets the following restrictions: the refrigerator door must be rectangle, and both the length and the width of the door must be positive integers. Edo figured out how he wants to place the magnets on the refrigerator. He introduced a system of coordinates on the plane, where each magnet is represented as a rectangle with sides parallel to the coordinate axes. Now he wants to remove no more than k magnets (he may choose to keep all of them) and attach all remaining magnets to the refrigerator door, and the area of ​​the door should be as small as possible. A magnet is considered to be attached to the refrigerator door if its center lies on the door or on its boundary. The relative positions of all the remaining magnets must correspond to the plan. Let us explain the last two sentences. Let's suppose we want to hang two magnets on the refrigerator. If the magnet in the plan has coordinates of the lower left corner (x1, y1) and the upper right corner (x2, y2), then its center is located at (<image>, <image>) (may not be integers). By saying the relative position should correspond to the plan we mean that the only available operation is translation, i.e. the vector connecting the centers of two magnets in the original plan, must be equal to the vector connecting the centers of these two magnets on the refrigerator. The sides of the refrigerator door must also be parallel to coordinate axes. Input The first line contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ min(10, n - 1)) β€” the number of magnets that Edo has and the maximum number of magnets Edo may not place on the refrigerator. Next n lines describe the initial plan of placing magnets. Each line contains four integers x1, y1, x2, y2 (1 ≀ x1 < x2 ≀ 109, 1 ≀ y1 < y2 ≀ 109) β€” the coordinates of the lower left and upper right corners of the current magnet. The magnets can partially overlap or even fully coincide. Output Print a single integer β€” the minimum area of the door of refrigerator, which can be used to place at least n - k magnets, preserving the relative positions. Examples Input 3 1 1 1 2 2 2 2 3 3 3 3 4 4 Output 1 Input 4 1 1 1 2 2 1 9 2 10 9 9 10 10 9 1 10 2 Output 64 Input 3 0 1 1 2 2 1 1 1000000000 1000000000 1 3 8 12 Output 249999999000000001 Note In the first test sample it is optimal to remove either the first or the third magnet. If we remove the first magnet, the centers of two others will lie at points (2.5, 2.5) and (3.5, 3.5). Thus, it is enough to buy a fridge with door width 1 and door height 1, the area of the door also equals one, correspondingly. In the second test sample it doesn't matter which magnet to remove, the answer will not change β€” we need a fridge with door width 8 and door height 8. In the third sample you cannot remove anything as k = 0.
from sys import * def check(u, d, l, r): used = [pointsx[i][1] for i in range(l)] used += [pointsx[-1 - i][1] for i in range(r)] used += [pointsy[i][1] for i in range(u)] used += [pointsy[-1 - i][1] for i in range(d)] if len(set(used)) > k: return DOHERA dx = pointsx[-1 - r][0] - pointsx[l][0] dy = pointsy[-1 - d][0] - pointsy[u][0] dx += dx & 1 dy += dy & 1 dx = max(2, dx) dy = max(2, dy) return dx * dy n, k = map(int, input().split()) pointsx = [] pointsy = [] DOHERA = 10**228 for i in range(n): a = list(map(int, input().split())) pointsx += [(a[0] + a[2], i)] pointsy += [(a[1] + a[3], i)] pointsx, pointsy = sorted(pointsx), sorted(pointsy) ans = DOHERA for u in range(0, k + 1): for d in range(0, k + 1): for l in range(0, k + 1): for r in range(0, k + 1): if l + r <= k and u + d <= k: ans = min(ans, check(u, d, l, r)) print(ans // 4)
FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR RETURN BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST BIN_OP VAR NUMBER VAR NUMBER VAR VAR LIST BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER 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 BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This is the hard version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem. Today is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal. An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them. You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^5)$Β β€” the number of ice spheres in the shop. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$Β β€” the prices of ice spheres. -----Output----- In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them. -----Example----- Input 7 1 3 2 2 4 5 4 Output 3 3 1 4 2 4 2 5 -----Note----- In the sample it's not possible to place the ice spheres in any order so that Sage would buy $4$ of them. If the spheres are placed in the order $(3, 1, 4, 2, 4, 2, 5)$, then Sage will buy one sphere for $1$ and two spheres for $2$ each.
import sys def answer(n, a): a.sort() ah = a[n // 2 :] al = a[: n // 2] ans = [] for i in range(len(ah)): if i < len(ah): ans.append(ah[i]) if i < len(al): ans.append(al[i]) num_spheres = 0 for i in range(1, n - 1): if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]: num_spheres += 1 print(num_spheres) print(*ans) return def main(): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) answer(n, a) return main()
IMPORT FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF 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 RETURN EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem. Today is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal. An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them. You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^5)$Β β€” the number of ice spheres in the shop. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$Β β€” the prices of ice spheres. -----Output----- In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them. -----Example----- Input 7 1 3 2 2 4 5 4 Output 3 3 1 4 2 4 2 5 -----Note----- In the sample it's not possible to place the ice spheres in any order so that Sage would buy $4$ of them. If the spheres are placed in the order $(3, 1, 4, 2, 4, 2, 5)$, then Sage will buy one sphere for $1$ and two spheres for $2$ each.
import sys input = sys.stdin.readline ins = lambda: input().rstrip() ini = lambda: int(input().rstrip()) inm = lambda: map(int, input().rstrip().split()) inl = lambda: list(map(int, input().split())) out = lambda x, s="\n": print(s.join(map(str, x))) n = ini() a = sorted(inl()) ans = [] count = 0 for i in range(n // 2): ans.append(a[i + n // 2]) ans.append(a[i]) if n % 2: ans.append(a[-1]) for i in range(1, n - 1): count += 1 if ans[i] < ans[i - 1] and ans[i] < ans[i + 1] else 0 print(count) print(*ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem. Today is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal. An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them. You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^5)$Β β€” the number of ice spheres in the shop. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$Β β€” the prices of ice spheres. -----Output----- In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them. -----Example----- Input 7 1 3 2 2 4 5 4 Output 3 3 1 4 2 4 2 5 -----Note----- In the sample it's not possible to place the ice spheres in any order so that Sage would buy $4$ of them. If the spheres are placed in the order $(3, 1, 4, 2, 4, 2, 5)$, then Sage will buy one sphere for $1$ and two spheres for $2$ each.
n = int(input()) a = input().split(" ") if n <= 2: print(0) print(" ".join(a)) exit(0) a = [int(s) for s in a] a = sorted(a) b = [0] * n stepb = 1 for i in range(0, n): b[stepb] = a[i] stepb += 2 if stepb >= n: stepb = 0 cnt = 0 for i in range(1, n - 1): if b[i] < b[i - 1] and b[i] < b[i + 1]: cnt += 1 print(cnt) print(" ".join([str(s) for s in b]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
This is the hard version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem. Today is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal. An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them. You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^5)$Β β€” the number of ice spheres in the shop. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$Β β€” the prices of ice spheres. -----Output----- In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them. -----Example----- Input 7 1 3 2 2 4 5 4 Output 3 3 1 4 2 4 2 5 -----Note----- In the sample it's not possible to place the ice spheres in any order so that Sage would buy $4$ of them. If the spheres are placed in the order $(3, 1, 4, 2, 4, 2, 5)$, then Sage will buy one sphere for $1$ and two spheres for $2$ each.
from sys import * input = stdin.readline listInput = lambda: list(map(int, input().strip().split())) lineInput = lambda: map(int, input().strip().split()) sJoin = lambda a, sep: f"{sep}".join(a) arrJoin = lambda a, sep: f"{sep}".join(map(str, a)) def main(): n = int(input()) arr = sorted(listInput()) k = set(arr) a = arr[: n // 2] b = arr[n // 2 :] l = list(zip(b, a)) ans = [] j = 1 c = 0 for i in l: ans.append(i[0]) ans.append(i[1]) if n % 2 == 1: ans.append(arr[-1]) while j < n - 1: if ans[j - 1] > ans[j] < ans[j + 1]: c += 1 j += 2 print(c) print(*ans) main()
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem. Today is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal. An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them. You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^5)$Β β€” the number of ice spheres in the shop. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$Β β€” the prices of ice spheres. -----Output----- In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them. -----Example----- Input 7 1 3 2 2 4 5 4 Output 3 3 1 4 2 4 2 5 -----Note----- In the sample it's not possible to place the ice spheres in any order so that Sage would buy $4$ of them. If the spheres are placed in the order $(3, 1, 4, 2, 4, 2, 5)$, then Sage will buy one sphere for $1$ and two spheres for $2$ each.
n = int(input()) (*arr,) = map(int, input().split()) arr.sort() half = n // 2 res = [-1] * n if n % 2 == 0: res[::2] = arr[-half:] res[1::2] = arr[:-half] else: res[::2] = arr[-half - 1 :] res[1::2] = arr[: -half - 1] ans = 0 for i in range(1, n - 1): if res[i - 1] > res[i] < res[i + 1]: ans += 1 print(ans) print(*res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem. Today is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal. An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them. You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^5)$Β β€” the number of ice spheres in the shop. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$Β β€” the prices of ice spheres. -----Output----- In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them. -----Example----- Input 7 1 3 2 2 4 5 4 Output 3 3 1 4 2 4 2 5 -----Note----- In the sample it's not possible to place the ice spheres in any order so that Sage would buy $4$ of them. If the spheres are placed in the order $(3, 1, 4, 2, 4, 2, 5)$, then Sage will buy one sphere for $1$ and two spheres for $2$ each.
def reOrder(arr): resArray = [] arr = sorted(arr) mid = len(arr) // 2 i = 0 j = mid while i < mid and j < len(arr): resArray.append(arr[j]) resArray.append(arr[i]) i += 1 j += 1 while j < len(arr): resArray.append(arr[j]) j += 1 return resArray def countPeaks(arr): if len(arr) <= 2: return 0 res = 0 i = 0 j = i + 2 while j < len(arr): left = arr[i] right = arr[j] middle = arr[i + 1] if middle < left and middle < right: res += 1 i += 1 j += 1 return res n = int(input()) arr = list(map(int, input().split())) resArray = reOrder(arr) res = countPeaks(resArray) print(res) print(*resArray)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN 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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem. Today is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal. An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them. You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^5)$Β β€” the number of ice spheres in the shop. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$Β β€” the prices of ice spheres. -----Output----- In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them. -----Example----- Input 7 1 3 2 2 4 5 4 Output 3 3 1 4 2 4 2 5 -----Note----- In the sample it's not possible to place the ice spheres in any order so that Sage would buy $4$ of them. If the spheres are placed in the order $(3, 1, 4, 2, 4, 2, 5)$, then Sage will buy one sphere for $1$ and two spheres for $2$ each.
n = int(input()) A = list(map(int, input().split())) A = sorted(A) B = A[0 : n // 2] C = A[n // 2 :] a = [] for i in range(len(B)): a.append(C[i]) a.append(B[i]) if n % 2 != 0: a.append(C[-1]) ans = 0 for i in range(1, n - 1): if a[i] < a[i - 1] and a[i] < a[i + 1]: ans += 1 print(ans) for x in a: print(x, end=" ") print()
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 VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem. Today is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal. An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them. You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^5)$Β β€” the number of ice spheres in the shop. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$Β β€” the prices of ice spheres. -----Output----- In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them. -----Example----- Input 7 1 3 2 2 4 5 4 Output 3 3 1 4 2 4 2 5 -----Note----- In the sample it's not possible to place the ice spheres in any order so that Sage would buy $4$ of them. If the spheres are placed in the order $(3, 1, 4, 2, 4, 2, 5)$, then Sage will buy one sphere for $1$ and two spheres for $2$ each.
for _ in range(1): n = int(input()) l = list(map(int, input().split())) if len(l) == 1: print(0) print(l[0]) else: l.sort() m = [0] * n ind = 0 for i in range(1, n, 2): m[i] = l[ind] ind += 1 for i in range(0, n, 2): m[i] = l[ind] ind += 1 ans = 0 for i in range(1, len(m) - 1): if m[i - 1] > m[i] and m[i] < m[i + 1]: ans += 1 m1 = [0] * n ind = 0 for i in range(1, n, 2): m1[i] = l[ind] ind += 1 ind = n - 1 for i in range(0, n, 2): m1[i] = l[ind] ind -= 1 ans1 = 0 for i in range(1, len(m) - 1): if m1[i - 1] > m1[i] and m1[i] < m1[i + 1]: ans1 += 1 if ans > ans1: print(ans) print(" ".join(str(x) for x in m)) else: print(ans1) print(" ".join(str(x) for x in m1))
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
This is the hard version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem. Today is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal. An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them. You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^5)$Β β€” the number of ice spheres in the shop. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$Β β€” the prices of ice spheres. -----Output----- In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them. -----Example----- Input 7 1 3 2 2 4 5 4 Output 3 3 1 4 2 4 2 5 -----Note----- In the sample it's not possible to place the ice spheres in any order so that Sage would buy $4$ of them. If the spheres are placed in the order $(3, 1, 4, 2, 4, 2, 5)$, then Sage will buy one sphere for $1$ and two spheres for $2$ each.
import sys input = sys.stdin.buffer.readline n = int(input()) a = [int(i) for i in input().split()] a.sort() ans = [0] * n for i in range(0, n, 2): ans[i] = a.pop() for i in range(1, n, 2): ans[i] = a.pop() v = 0 flag = 0 for i in range(1, n - int(n & 1 == 0), 2): if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]: v += 1 elif n & 1 == 0: if ans[-1] < ans[i - 1] and ans[-1] < ans[i + 1]: v += 1 flag = 1 ans[-1], ans[i] = ans[i], ans[-1] print(v) print(*ans)
IMPORT ASSIGN 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 ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem. Today is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal. An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them. You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^5)$Β β€” the number of ice spheres in the shop. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$Β β€” the prices of ice spheres. -----Output----- In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them. -----Example----- Input 7 1 3 2 2 4 5 4 Output 3 3 1 4 2 4 2 5 -----Note----- In the sample it's not possible to place the ice spheres in any order so that Sage would buy $4$ of them. If the spheres are placed in the order $(3, 1, 4, 2, 4, 2, 5)$, then Sage will buy one sphere for $1$ and two spheres for $2$ each.
n = int(input()) a = [int(i) for i in input().split()] a.sort() a1 = [0] * n cnt = 0 a1[0] = a[n // 2] if n % 2 != 0: for i in range(0, n // 2): a1[i * 2 + 1] = a[i] a1[i * 2 + 2] = a[n // 2 + i + 1] else: for i in range(0, n // 2 - 1): a1[1 + i * 2] = a[i] a1[i * 2 + 2] = a[n // 2 + i + 1] a1[n - 1] = a[n // 2 - 1] if n == 1: print(0) print(a1[0]) else: s = str(a1[0]) + " " for i in range(1, n - 1): s += str(a1[i]) + " " if a1[i] < a1[i + 1] and a1[i] < a1[i - 1]: cnt += 1 s += str(a1[n - 1]) print(cnt) print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR STRING IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR