description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = [int(i) for i in input().split(" ")] arr = [(int(i) % m) for i in input().split(" ")] c = {} for i in range(m): c[i] = False stack = [0] newstack = [] for i in arr: for j in stack: arrrrgh = (i + j) % m if arrrrgh == 0: print("YES") return elif c[arrrrgh] == False: newstack.append(arrrrgh) c[arrrrgh] = True stack = stack + newstack newstack = [] print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = [int(i) for i in input().split()] r = [False] * m for x in [(int(i) % m) for i in input().split()]: c = r.copy() c[x] = True for i in range(m): if r[i]: c[(i + x) % m] = True r = c.copy() if r[0]: print("YES") exit(0) print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
def main(n, m, a): if n >= m: return True sums = [] vis = set() for i in range(0, n): if a[i] == 0: return True for j in range(0, len(sums)): _sum = (sums[j] + a[i]) % m if _sum == 0: return True if _sum not in vis: sums.append(_sum) vis.add(_sum) if a[i] not in vis: sums.append(a[i]) vis.add(a[i]) return 0 in sums n, m = map(int, input().split(" ")) a = list(map(lambda i: int(i) % m, input().split(" "))) print("YES" if main(n, m, a) else "NO")
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = map(int, input().split()) l = list(map(int, input().split())) for i in range(n): l[i] %= m l1 = [] c = 0 ans = False if n > m: ans = True if ans == True: print("YES") else: dp = [0] * m for i in range(n): dp1 = dp.copy() for j in range(m): if dp1[j] == 1: dp[(l[i] + j) % m] = 1 dp[l[i]] = 1 if dp[0] == 1: print("YES") else: print("NO")
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 FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = map(int, input().split()) if n > 2 * m: print("YES") else: t = [(0) for i in range(m)] s = input().split() for i in range(len(s)): h = int(s[i]) % m v = [(0) for i in range(m)] for j in range(m): if t[j] == 1: v[(h + j) % m] = 1 for j in range(m): if v[j] == 1: t[j] = 1 t[h] = 1 if t[0] == 1: print("YES") else: print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = map(int, input().split()) arr = list(map(int, input().split())) if n > m: print("YES") else: dp = [False] * m for i in range(n): if dp[0] == True: break temp = [False] * m for j in range(m): if dp[j] == True: temp[(j + arr[i]) % m] = True for j in range(m): dp[j] |= temp[j] dp[arr[i] % m] = True if dp[0] == True: print("YES") else: print("NO")
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 IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = (int(t) for t in input().split()) A = [int(t) for t in input().split()] makeable = set() for a in A: new_makeable = set([((a + k) % m) for k in makeable]) makeable |= new_makeable makeable.add(a % m) if 0 in makeable: print("YES") break if 0 not in makeable: print("NO")
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 FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR STRING IF NUMBER VAR EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
def solve(n, m, As): dp = [0] * m for a in As: a %= m newdp = dp[:] for i, d in enumerate(dp): if d: newdp[(i + a) % m] = 1 newdp[a] = 1 dp = newdp if dp[0]: return True return False n, m = map(int, input().split()) if n > m: print("YES") else: As = list(map(int, input().split())) if solve(n, m, As): print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = list(map(int, input().split())) def intmodm(num): return int(num) % m a = list(map(intmodm, input().split())) states = [[-1] * m] for index in range(n): states.append(states[-1][:]) num = a[index] for i in range(m): if states[-2][i] != -1: states[-1][(i + num) % m] = index if states[-1][num % m] == -1: states[-1][num % m] = index if states[-1][0] != -1: print("YES") break else: print("NO")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = map(int, input().split()) l = [int(i) for i in input().split()] if m <= n: print("YES") exit() l = [(i % m) for i in l] pssbl = [0] * m for i in range(1, n + 1): temp = [0] * m for j in range(m): if pssbl[j]: temp[(j + l[i - 1]) % m] = 1 for j in range(m): if pssbl[j] == 0 and temp[j]: pssbl[j] = 1 pssbl[l[i - 1]] = 1 print("YES" if pssbl[0] else "NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
def poss(n, m, L): if n >= m: return "YES" dp = [[(0) for _ in range(n)] for _ in range(m)] x = L[0] % m dp[x][0] = 1 for i in range(1, n): y = L[i] % m for mod in range(m): if mod == y: dp[mod][i] = 1 else: dp[mod][i] = dp[mod][i - 1] or dp[(mod - y) % m][i - 1] res = dp[0][n - 1] if res == 1: return "YES" return "NO" n, m = list(map(int, input().split())) L = list(map(int, input().split())) print(poss(n, m, L))
FUNC_DEF IF VAR VAR RETURN STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
dp = [] def possible(arr, i, finalSum, m): if finalSum != 0 and finalSum % m == 0: return True if i >= len(arr): return False if dp[i][finalSum % m] == -1: dp[i][finalSum % m] = possible( arr, i + 1, finalSum % m + arr[i], m ) or possible(arr, i + 1, finalSum, m) return dp[i][finalSum % m] def solve(): n, m = map(int, input().split()) array = list(map(int, input().split())) if n > m: return "YES" else: for x in range(n): dp.append([-1] * (m + 1)) newArray = [] for i in range(n): if array[i] % m == 0: return "YES" newArray.append(array[i] % m) if possible(newArray, 0, 0, m): return "YES" else: return "NO" print(solve())
ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR BIN_OP 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 IF VAR VAR RETURN STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR RETURN STRING RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
import sys input = sys.stdin.readline n, m = map(int, input().split()) a = list(map(int, input().split())) if n > m: print("YES") else: dp = [([0] * m) for i in range(n + 1)] dp[0][0] = 1 for i in range(n): for j in range(m): if dp[i][j]: dp[i + 1][j] = 1 dp[i + 1][(j + a[i]) % m] = 1 if (j + a[i]) % m == 0: print("YES") exit() print("NO")
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 IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = map(int, input().split()) a = list(map(int, input().split())) if n > m: print("YES") else: arr = [([0] * m) for _ in range(n)] arr[0][a[0] % m] = 1 for i in range(1, n): arr[i][a[i] % m] = 1 for j in range(m): if arr[i - 1][j] == 1: arr[i][j] = 1 arr[i][(j + a[i]) % m] = 1 found = False for i in range(n): if arr[i][0] == 1: found = True break if found: print("YES") else: print("NO")
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 IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
def gcd(a, b): return a if b == 0 else gcd(b, a % b) def good(a, m): if len(a) > m: return True b = [0] * m for e in a: b[e % m] += 1 if b[0] > 0: return True for r in range(1, (m + 1) // 2): if b[r] > 0 and b[m - r] > 0: return True for r in range(m): p = m // gcd(r, m) if b[r] >= p: return True if m % 2 == 0 and b[m // 2] > 1: return True d = 2 while d * d <= m: if m % d == 0: if b[d] >= m // d: return True if b[m // d] >= d: return True d += 1 if b[1] >= m or b[m - 1] >= m: return True can = [([False] * m) for _ in range(len(a) + 1)] for n in range(1, len(a) + 1): can[n][a[n - 1] % m] = True for r in range(m): can[n][r] = can[n][r] or can[n - 1][r] or can[n - 1][(r - a[n - 1]) % m] return can[len(a)][0] n, m = map(int, input().split()) a = list(map(int, input().split())) print("YES" if good(a, m) else "NO")
FUNC_DEF RETURN VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR VAR RETURN NUMBER VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
def solve(): n, m = map(int, input().split()) a = list(map(int, input().split())) if n >= m: return "YES" s = set() for i in a: w = set() for j in s: w.add((i + j) % m) s.update(w) s.add(i % m) if 0 in s: return "YES" return "NO" print(solve())
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 IF VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF NUMBER VAR RETURN STRING RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = map(int, input().split()) a = [(int(i) % m) for i in input().split()] if n <= m: store = set() x = 0 for i in a: b = set() if i == 0: x = 1 break if i not in b: b.add(i) for j in store: if (i + j) % m == 0: x = 1 break if (i + j) % m not in store: b.add((i + j) % m) if x == 1: break for j in b: store.add(j) if x == 1: print("YES") else: print("NO") else: print("YES")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = map(int, input().split()) a = list(map(int, input().split())) a = [(i % m) for i in a] b = [0] * 1005 c = [] for v in a: loc = len(c) if v in c: for i in range(b[v], loc): if (c[i] + v) % m not in c: c.append((c[i] + v) % m) else: c.append(v) for i in range(loc): if (c[i] + v) % m not in c: c.append((c[i] + v) % m) b[v] = loc if 0 in c: print("YES") else: print("NO")
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 VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = map(int, input().split()) a = list(map(int, input().split())) if n > m: print("YES") else: can = set() can.add(a[0] % m) newCan = set(can) for i in range(1, n): newCan.add(a[i] % m) for j in can: newCan.add((j + a[i]) % m) can = set(newCan) if 0 in can: print("YES") else: print("NO")
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 IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = map(int, input().split()) a = [int(x) for x in input().split()] if n > m or a[0] % m == 0: print("YES") exit(0) can = [] for i in range(n): can.append([0] * m) can[0][0] = 1 can[0][a[0] % m] = 1 for i in range(n - 1): for j in range(m): if can[i][j] > 0: r = (j + a[i + 1]) % m if r == 0 or a[i + 1] == 0: print("YES") exit(0) can[i + 1][r] = 1 can[i + 1][j] = 1 print("NO")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING
You are given a sequence of numbers a_1, a_2, ..., a_{n}, and a number m. Check if it is possible to choose a non-empty subsequence a_{i}_{j} such that the sum of numbers in this subsequence is divisible by m. -----Input----- The first line contains two numbers, n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ 10^3) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9). -----Output----- In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist. -----Examples----- Input 3 5 1 2 3 Output YES Input 1 6 5 Output NO Input 4 6 3 1 1 3 Output YES Input 6 6 5 5 5 5 5 5 Output YES -----Note----- In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
n, m = list(map(int, input().split())) p = [0] * m for x in map(int, input().split()): n = p[:] n[x % m] = 1 for i in range(m): if p[i] == 1: n[(i + x) % m] = 1 p = n if p[0]: print("YES") return print("NO")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
class Solution: def trap(self, height): N = len(height) valleys = [] i = 0 while i < N - 1: while i < N - 1 and height[i] <= height[i + 1]: i += 1 while i < N - 1 and height[i] > height[i + 1]: i += 1 j = i while j < N - 1 and height[j] == height[j + 1]: j += 1 if j < N - 1 and height[j] < height[j + 1]: valleys.append([i - 1, j + 1]) i = j total_water = 0 further_valleys = [] k = 0 if valleys: l = valleys[k][0] r = valleys[k][1] old_level = height[l + 1] while k < len(valleys): water = 0 while ( l >= 0 and r < N and height[l] >= height[l + 1] and height[r - 1] <= height[r] ): new_level = min(height[l], height[r]) water += (new_level - old_level) * (r - l - 1) old_level = new_level if l >= 0 and r < N: if height[l] == height[r]: l -= 1 r += 1 elif height[l] < height[r]: l -= 1 else: r += 1 while l >= 0 and height[l] == height[l + 1]: l -= 1 while r < N and height[r - 1] == height[r]: r += 1 total_water += water if l >= 0 and r < N: if height[l] > height[l + 1]: further_valleys.append([l, r]) elif ( further_valleys and height[further_valleys[-1][1] - 1] == height[l + 1] ): old_level = height[l + 1] l = further_valleys[-1][0] further_valleys.pop() continue k += 1 if k < len(valleys): l = valleys[k][0] r = valleys[k][1] old_level = height[l + 1] return total_water
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
class Solution: def get_empty_per_level(self, height, level): print(("Level: ", level)) block_start = 0 n = len(height) while block_start < n and height[block_start] < level: block_start += 1 if block_start == n: print("No start") return n block_end = n - 1 while block_end > block_start and height[block_end] < level: block_end -= 1 if block_end == block_start: print("No end") return n - 1 print("Some value") return n - (block_end - block_start + 1) def trap(self, height): if not height or len(height) == 0: return 0 levels = set(height) levels = list(levels) if 0 in levels: levels.remove(0) levels.sort() if len(levels) == 0: return 0 max_level = max(height) total_count = sum([(max_level - item) for item in height]) prev_level = levels.pop(0) missing_water_per_level = self.get_empty_per_level(height, prev_level) total_count -= prev_level * missing_water_per_level for level in levels: missing_water_per_level = self.get_empty_per_level(height, level) multi_level_count = (level - prev_level) * missing_water_per_level total_count -= multi_level_count prev_level = level return total_count
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
class Solution: def trap(self, height): n = len(height) l, r, water, minHeight = 0, n - 1, 0, 0 while l < r: while l < r and height[l] <= minHeight: water += minHeight - height[l] l += 1 while r > l and height[r] <= minHeight: water += minHeight - height[r] r -= 1 minHeight = min(height[l], height[r]) return water
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
class Solution: def trap(self, height): l_bounds = [] lb = float("-inf") for h in height: lb = max(lb, h) l_bounds.append(lb) water = 0 rb = float("-inf") for lb, h in zip(reversed(l_bounds), reversed(height)): rb = max(rb, h) water += min(lb, rb) - h return water
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
class Solution: def trap(self, height): if not height: return 0 result = 0 left = 0 right = len(height) - 1 while left < right: if height[left] <= height[right]: tmp = height[left] left += 1 while left < right and height[left] <= tmp: result += tmp - height[left] left += 1 else: tmp = height[right] right -= 1 while left < right and height[right] <= tmp: result += tmp - height[right] right -= 1 return result
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
class Solution: def trap(self, height): length = len(height) if length == 0: return 0 maxLeft = [0] * length maxRight = [0] * length result = 0 maxLeft[0] = height[0] maxRight[length - 1] = height[length - 1] for i in range(1, length): maxLeft[i] = max(maxLeft[i - 1], height[i]) for i in reversed(list(range(0, length - 1))): maxRight[i] = max(maxRight[i + 1], height[i]) for i in range(length): result += min(maxLeft[i], maxRight[i]) - height[i] return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
class Solution: def trap(self, height): ans = 0 max_left, max_right = [0] * len(height), [0] * len(height) for i in range(1, len(height)): max_left[i] = max(max_left[i - 1], height[i - 1]) for i in range(len(height) - 2, -1, -1): max_right[i] = max(max_right[i + 1], height[i + 1]) for i in range(1, len(height) - 1): ans += max(min(max_left[i], max_right[i]) - height[i], 0) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
class Solution: def trap(self, height): left, right = 0, len(height) - 1 result = 0 while left < right: mh = min(height[left], height[right]) if height[left] < height[right]: left = left + 1 while height[left] <= mh and left < right: result = result + mh - height[left] left = left + 1 else: right = right - 1 while height[right] <= mh and left < right: result = result + mh - height[right] right = right - 1 return result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
class Solution: def trap(self, barHeights): if barHeights == []: return 0 numberOfBars = len(barHeights) leftMaxima = [(0) for counter in range(numberOfBars)] rightMaxima = [(0) for counter in range(numberOfBars)] leftMaxima[0] = barHeights[0] for counter in range(1, numberOfBars): leftMaxima[counter] = max(leftMaxima[counter - 1], barHeights[counter]) rightMaxima[numberOfBars - 1] = barHeights[numberOfBars - 1] for counter in range(numberOfBars - 2, -1, -1): rightMaxima[counter] = max(rightMaxima[counter + 1], barHeights[counter]) waterTrapped = 0 for counter in range(0, numberOfBars): waterTrapped += ( min(leftMaxima[counter], rightMaxima[counter]) - barHeights[counter] ) return waterTrapped
CLASS_DEF FUNC_DEF IF VAR LIST RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
class Solution: def trap(self, height): maxRight = 0 maxLeft = 0 left = 0 right = len(height) - 1 ret = 0 while left < right: maxRight = max(maxRight, height[right]) maxLeft = max(maxLeft, height[left]) if maxLeft > maxRight: ret += maxRight - height[right] right -= 1 else: ret += maxLeft - height[left] left += 1 return ret
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
class Solution: def trap(self, height): if not height: return 0 left, right = 0, len(height) - 1 maxLeft, maxRight = height[left], height[right] maxTrap = 0 while left <= right: if height[left] <= height[right]: if height[left] > maxLeft: maxLeft = height[left] else: maxTrap += maxLeft - height[left] left += 1 else: if height[right] > maxRight: maxRight = height[right] else: maxTrap += maxRight - height[right] right -= 1 return maxTrap
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6
class Solution: def trap(self, height): leftmaxes = [] rightmaxes = [] maximum = 0 for i in range(len(height)): maximum = max(maximum, height[i]) leftmaxes.append(maximum) maximum = 0 for i in range(len(height)): maximum = max(maximum, height[len(height) - i - 1]) rightmaxes.append(maximum) water = 0 print(leftmaxes) print(rightmaxes) for i in range(len(height)): trappable = min(leftmaxes[i], rightmaxes[-i - 1]) if trappable > height[i]: water += trappable - height[i] return water
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: q_max, ans = deque([(nums[0], 0)]), nums[0] for i in range(1, len(nums)): nums[i] += max(q_max[0][0], 0) if q_max[0][1] <= i - k: q_max.popleft() while q_max and nums[i] > q_max[-1][0]: q_max.pop() q_max.append((nums[i], i)) ans = max(ans, nums[i]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: dp = [nums[0]] decrease = collections.deque([0]) for i, x in enumerate(nums[1:], 1): if decrease[0] == i - k - 1: decrease.popleft() tmp = max(x, dp[decrease[0]] + x) dp += [tmp] while decrease and dp[decrease[-1]] <= tmp: decrease.pop() decrease += [i] return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR LIST VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR LIST VAR RETURN FUNC_CALL VAR VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: q = [] res = float("-inf") for i in range(len(nums)): while q and i - q[0][0] > k: q.pop(0) temp = nums[i] if q and q[0][1] > 0: temp += q[0][1] res = max(res, temp) while q and q[-1][1] <= temp: q.pop() q.append((i, temp)) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) q = [(-nums[0], 0)] res = nums[0] dp = [0] * n for i in range(1, n): while q and i - q[0][1] > k: heapq.heappop(q) t = max(nums[i] - q[0][0], nums[i]) res = max(res, t) heapq.heappush(q, (-t, i)) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: dp, q = [nums[0]], deque() q.append((0, nums[0])) res = nums[0] for i in range(1, len(nums)): while q and i - q[0][0] > k: q.popleft() cur = nums[i] if q: cur += max(q[0][1], 0) while q and q[-1][1] < cur: q.pop() q.append((i, cur)) dp.append(cur) res = max(res, cur) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR LIST VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: dq = collections.deque() m = -sys.maxsize for i, n in enumerate(nums): fi = n if dq: fi += max(dq[0][1], 0) while dq and fi >= dq[-1][1]: dq.pop() dq.append([i, fi]) if i - dq[0][0] == k: dq.popleft() if fi > m: m = fi return m
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: deque = [] for i, num in enumerate(nums): while deque and nums[deque[-1]] < 0: deque.pop() while deque and deque[0] < i - k: deque.pop(0) if deque: nums[i] = nums[deque[0]] + num while deque and nums[deque[-1]] < nums[i]: deque.pop() deque.append(i) return max(nums)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums, k): n = len(nums) dp = [0] * n deq = deque([0]) for i in range(n): if deq and deq[0] < i - k: deq.popleft() while deq and nums[i] + dp[deq[0]] > dp[deq[-1]]: a = deq.pop() dp[i] = max(nums[i], nums[i] + dp[deq[0]] if deq else nums[i] + dp[a]) deq.append(i) return max(dp)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR WHILE VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) q = collections.deque() result = nums[0] for i in range(n): while q and q[0][1] < i - k: q.popleft() a = q[0][0] if q else 0 m = nums[i] + (a if a > 0 else 0) while q and m >= q[-1][0]: q.pop() q.append((m, i)) result = max(result, m) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: max_queue = collections.deque([0]) max_sum = nums[0] for i in range(1, len(nums)): while max(0, i - k) > max_queue[0]: max_queue.popleft() nums[i] += max(0, nums[max_queue[0]]) max_sum = max(max_sum, nums[i]) while max_queue and nums[max_queue[-1]] < nums[i]: max_queue.pop() max_queue.append(i) return max_sum
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: stack = deque() for i in range(len(nums)): nums[i] += stack[0] if stack else 0 while stack and stack[-1] < nums[i]: stack.pop() if i >= k and stack and stack[0] == nums[i - k]: stack.popleft() if nums[i] > 0: stack.append(nums[i]) return max(nums) q = deque() for i in range(len(nums)): nums[i] += q[0] if q else 0 while q and nums[i] > q[-1]: q.pop() if nums[i] > 0: q.append(nums[i]) if i >= k and q and q[0] == nums[i - k]: q.popleft() return max(nums)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER WHILE VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: dp = [-sys.maxsize] * len(nums) ans = nums[0] dp[0] = nums[0] heap = [(-nums[0], 0)] heapq.heapify(heap) for i in range(1, len(nums)): bound = i - k while heap[0][1] < bound: heapq.heappop(heap) dp[i] = max(dp[i], nums[i], -heap[0][0] + nums[i]) ans = max(ans, dp[i]) heapq.heappush(heap, (-dp[i], i)) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: maxNum = nums[0] maxSum = nums[0] maxNegSum = 0 curNegWindowSum = 0 curWindowSum = 0 rightIndex = 0 leftIndex = 0 midIndex = 0 negativeStreak = False while rightIndex < len(nums): if maxNum < nums[rightIndex]: maxNum = nums[rightIndex] if nums[rightIndex] >= 0 and not negativeStreak: curWindowSum += nums[rightIndex] maxSum = max(maxSum, curWindowSum) elif nums[rightIndex] < 0 and not negativeStreak: negativeStreak = True midIndex = rightIndex if k > 1: curNegWindowSum = nums[rightIndex] maxNegSum = curNegWindowSum curWindowSum += nums[rightIndex] elif nums[rightIndex] < 0 and negativeStreak: if rightIndex - midIndex < k - 1: curNegWindowSum += nums[rightIndex] maxNegSum = curNegWindowSum else: if k > 1: curNegWindowSum -= nums[midIndex] curNegWindowSum += nums[rightIndex] maxNegSum = min(maxNegSum, curNegWindowSum) midIndex += 1 curWindowSum += nums[rightIndex] elif nums[rightIndex] >= 0 and negativeStreak: curWindowSum -= maxNegSum if curWindowSum <= 0: midIndex = rightIndex leftIndex = rightIndex curWindowSum = nums[rightIndex] else: curWindowSum += nums[rightIndex] maxSum = max(maxSum, curWindowSum) maxNegSum = 0 curNegWindowSum = 0 negativeStreak = False rightIndex += 1 return max(maxSum, maxNum)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: rec = -nums[0] heap = [(-nums[0], 0)] for j, n in enumerate(nums[1:]): while j + 1 - heap[0][1] > k: heapq.heappop(heap) cand = -n + heap[0][0] if heap[0][0] <= 0 else -n rec = min(rec, cand) heapq.heappush(heap, (cand, j + 1)) return -rec
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER WHILE BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: dp = [(0 - sys.maxsize) for i in range(len(nums))] heap = [] dp[0] = nums[0] heapq.heappush(heap, (0 - nums[0], 0)) for i in range(1, len(nums)): while len(heap) and heap[0][1] < i - k: heapq.heappop(heap) dp[i] = max(dp[i], nums[i] + max(0, 0 - heap[0][0] if len(heap) else 0)) heapq.heappush(heap, (0 - dp[i], i)) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: dp, res = collections.deque(), -float("inf") for i, n in enumerate(nums): if dp and dp[0][0] < i - k: dp.popleft() cur = n + (0 if not dp else dp[0][1]) res = max(res, cur) if cur > 0: while dp and dp[-1][1] <= cur: dp.pop() dp.append((i, cur)) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) _max = collections.deque() res = max(nums) for i in range(n): if len(_max) and _max[0][0] > 0: val = nums[i] + _max[0][0] else: val = nums[i] while len(_max) and _max[-1][0] < val: _max.pop() _max.append((val, i)) res = max(val, res) if _max[0][1] <= i - k: _max.popleft() return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class MonoQ: def __init__(self, k): self.k = k self.m = [] def add(self, a, solution): while len(self.m) > 0: if solution[a] > solution[self.m[-1]]: self.m.pop() else: break self.m.append(a) def grab(self, a, solution, nums): if len(self.m) > 0: if self.m[0] > a + self.k: self.m = self.m[1:] return max(nums[a], nums[a] + solution[self.m[0]]) else: return nums[a] class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) solution = [0] * n m = MonoQ(k) for i in range(n - 1, -1, -1): solution[i] = m.grab(i, solution, nums) m.add(i, solution) return max(solution)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF WHILE FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN 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 EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: withAdding = [(0) for _ in range(len(nums))] notAdding = [(-1) for _ in range(len(nums))] validMax = [nums[0]] maxValue = nums[0] withAdding[0] = nums[0] for i in range(1, len(nums)): if maxValue < 0 and nums[i] > maxValue: withAdding[i] = nums[i] else: withAdding[i] = maxValue + nums[i] validMax.append(withAdding[i]) maxValue = max(withAdding[i], maxValue) if len(validMax) > k and validMax.pop(0) == maxValue: maxValue = max(validMax) notAdding[i] = max(notAdding[i - 1], withAdding[i - 1]) return max(max(notAdding), max(withAdding))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) dp = [0] * n dp[0] = nums[0] heap = [] heappush(heap, (-nums[0], 0)) for i in range(1, n): while heap[0][1] < i - k: heappop(heap) cur = heap[0][0] dp[i] = nums[i] + max(-cur, 0) heappush(heap, (-dp[i], i)) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) dp = [0] * n remove = collections.defaultdict(int) h = [] def get_b(): if not h: return 0 b = heapq.heappop(h) while remove[b] > 0: remove[b] -= 1 b = heapq.heappop(h) heapq.heappush(h, b) return -b for i, d in enumerate(nums): if i > k: remove[-dp[i - k - 1]] += 1 dp[i] = max(get_b(), 0) + d heapq.heappush(h, -dp[i]) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) heap = [(-nums[0], 0)] result = nums[0] for i in range(1, n): while heap and heap[0][1] < i - k: heapq.heappop(heap) a = -heap[0][0] m = nums[i] + (a if a > 0 else 0) heapq.heappush(heap, (-m, i)) result = max(result, m) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) dp = [(-math.inf) for i in range(n)] dp[0] = nums[0] ans = [nums[0]] queue = [0] for i in range(1, n): dp[i] = max(dp[i], nums[i], nums[i] + ans[i - 1]) if len(queue) == 0: queue.append(i) else: while len(queue) > 0 and (dp[i] > dp[queue[-1]] or i - queue[0] >= k): if dp[i] > dp[queue[-1]]: queue.pop(-1) else: queue.pop(0) queue.append(i) ans.append(dp[queue[0]]) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: heap = [(-nums[0], 0)] ret = nums[0] for i in range(1, len(nums)): remove = i - k - 1 while remove >= heap[0][1]: heapq.heappop(heap) cur = max(0, -heap[0][0]) + nums[i] ret = max(ret, cur) heapq.heappush(heap, (-cur, i)) return ret
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: res = -sys.maxsize n = len(nums) dp = [0] * (n + 1) dq = collections.deque() for i in range(n): if not dq: dp[i + 1] = nums[i] else: dp[i + 1] = max(nums[i], dq[0] + nums[i]) dq.append(dp[i + 1]) while len(dq) > k: dq.popleft() while len(dq) > 1 and dq[0] <= dq[-1]: dq.popleft() res = max(res, dp[i + 1]) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) dp = [-sys.maxsize] * n dq = [] dp[n - 1] = nums[n - 1] res = dp[n - 1] for i in range(n - 2, -1, -1): while len(dq) != 0 and dp[i + 1] > dp[dq[-1]]: dq.pop() while len(dq) != 0 and dq[0] > i + k: dq.pop(0) dq.append(i + 1) dp[i] = max(dp[i], nums[i], nums[i] + dp[dq[0]]) res = max(res, dp[i]) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) dp = [-sys.maxsize] * n dp[0] = nums[0] maxDeque = deque() ans = -sys.maxsize for j in range(n): while len(maxDeque) > 0 and j - maxDeque[0] > k: maxDeque.popleft() preMax = dp[maxDeque[0]] if len(maxDeque) > 0 else 0 dp[j] = max(preMax + nums[j], nums[j]) ans = max(dp[j], ans) while len(maxDeque) > 0 and dp[maxDeque[-1]] < dp[j]: maxDeque.pop() maxDeque.append(j) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied. A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.   Example 1: Input: nums = [10,2,-10,5,20], k = 2 Output: 37 Explanation: The subsequence is [10, 2, 5, 20]. Example 2: Input: nums = [-1,-2,-3], k = 1 Output: -1 Explanation: The subsequence must be non-empty, so we choose the largest number. Example 3: Input: nums = [10,-2,-10,-5,20], k = 2 Output: 23 Explanation: The subsequence is [10, -2, -5, 20].   Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4
class Solution: def constrainedSubsetSum(self, nums: List[int], k: int) -> int: n = len(nums) dp = [0] * n q = deque() ans = float("-inf") for i in range(n): if i > k and q[0] == i - k - 1: q.popleft() dp[i] = (0 if len(q) == 0 else max(dp[q[0]], 0)) + nums[i] while len(q) > 0 and dp[i] >= dp[q[-1]]: q.pop() q.append(i) ans = max(ans, dp[i]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin, stdout input = stdin.readline d1 = ["R", "G", "B"] d2 = ["G", "B", "R"] d3 = ["B", "R", "G"] t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input().strip() ans = float("inf") temp = s[0:k] a = b = c = 0 p = 0 q = 1 r = 2 for j in temp: if j != d1[p]: a += 1 if j != d1[q]: b += 1 if j != d1[r]: c += 1 p += 1 p %= 3 q += 1 q %= 3 r += 1 r %= 3 ans = min(a, b, c) for i in range(1, n - k + 1): first = i last = i + k - 1 if s[first - 1] != d1[0]: a -= 1 if s[first - 1] != d2[0]: b -= 1 if s[first - 1] != d3[0]: c -= 1 a, b, c = c, a, b if s[last] != d1[(k - 1) % 3]: a += 1 if s[last] != d2[(k - 1) % 3]: b += 1 if s[last] != d3[(k - 1) % 3]: c += 1 ans = min(ans, a, b, c) print(ans)
ASSIGN VAR VAR ASSIGN VAR LIST STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin input = stdin.readline R = lambda: map(int, input().split()) (q,) = R() a = "RGB" for q1 in range(q): an = [300000] * 3 n, k = R() s = input() for i2 in range(3): ans = [0] * n m = 0 for i in range(n): ans[i] = s[i] == a[(i + i2) % 3] m += ans[i] == 0 if i >= k: m -= ans[i - k] == 0 if i >= k - 1: an[i2] = min(an[i2], m) print(min(an))
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys input = sys.stdin.readline q = int(input()) for i in range(q): n, k = map(int, input().split()) s = input() R, G, B = 0, 0, 0 ans = float("inf") for j in range(n): if j % 3 == 0: if s[j] == "R": G += 1 B += 1 elif s[j] == "G": R += 1 B += 1 else: R += 1 G += 1 elif j % 3 == 1: if s[j] == "R": G += 1 R += 1 elif s[j] == "G": G += 1 B += 1 else: R += 1 B += 1 elif s[j] == "R": R += 1 B += 1 elif s[j] == "G": R += 1 G += 1 else: G += 1 B += 1 if j >= k - 1: ans = min(ans, R, G, B) if (j - k + 1) % 3 == 0: if s[j - k + 1] == "R": G -= 1 B -= 1 elif s[j - k + 1] == "G": R -= 1 B -= 1 else: R -= 1 G -= 1 elif (j - k + 1) % 3 == 1: if s[j - k + 1] == "R": G -= 1 R -= 1 elif s[j - k + 1] == "G": G -= 1 B -= 1 else: R -= 1 B -= 1 elif s[j - k + 1] == "R": R -= 1 B -= 1 elif s[j - k + 1] == "G": R -= 1 G -= 1 else: G -= 1 B -= 1 print(ans)
IMPORT ASSIGN VAR VAR 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 FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys input = sys.stdin.readline q = int(input()) aux = "RGB" def fun(k, s, ini): tam = 0 ans = int(1e20) change = 0 i = 0 j = 0 while len(s) > i: while len(s) > i and k > tam: change += 1 if aux[(ini + i) % 3] != s[i] else 0 tam += 1 i += 1 if tam == k: ans = min(ans, change) change -= 1 if s[j] != aux[(ini + j) % 3] else 0 j += 1 tam -= 1 return ans for _ in range(q): n, k = map(int, input().split(" ")) s = input() print(min(fun(k, s, 0), min(fun(k, s, 1), fun(k, s, 2))))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys input = lambda: sys.stdin.readline().strip("\r\n") for _ in range(int(input())): n, k = map(int, input().split()) s = input() ans = 0 for color in ["RGB", "GBR", "BRG"]: cnt = 0 for i in range(k): if s[i] == color[i % 3]: cnt += 1 maxi = cnt for i in range(n - k): if s[i + k] == color[(i + k) % 3]: cnt += 1 if s[i] == color[i % 3]: cnt -= 1 if cnt > maxi: maxi = cnt ans = max(ans, maxi) print(k - ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING 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 FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR LIST STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) arr = list(str(input())[:-1]) s = "RGB" * (n // 3 + 2) res = float("inf") for i in range(3): st = s[i : n + i] s0 = [(0 if arr[j] == st[j] else 1) for j in range(n)] temp = sum(s0[:k]) res = min(res, temp) for j in range(k, n): temp += s0[j] - s0[j - k] res = min(res, temp) print(res)
IMPORT ASSIGN VAR VAR 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 FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys input = sys.stdin.readline q = int(input()) for i in range(q): n, k = map(int, input().split()) s = input()[:n] if k == 1: print(0) continue rgb = [0, 0, 0] a = 1 for j, c in enumerate(s): if c == "R": rgb[j % 3] += 1 elif c == "G": rgb[(j - 1) % 3] += 1 else: rgb[(j - 2) % 3] += 1 if j + 1 >= k: a = max(a, max(rgb)) t = s[j - k + 1] if t == "R": rgb[(j - k + 1) % 3] -= 1 elif t == "G": rgb[(j - k) % 3] -= 1 else: rgb[(j - k - 1) % 3] -= 1 if a == k: print(0) break else: print(k - a)
IMPORT ASSIGN VAR VAR 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 FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR BIN_OP VAR NUMBER NUMBER IF VAR STRING VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR STRING VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys t = int(input()) for _ in range(t): n, k = list(map(int, sys.stdin.readline().split())) s = sys.stdin.readline() d = {} d["R"] = [0, 0, 0] d["G"] = [0, 0, 0] d["B"] = [0, 0, 0] res = 0 for i in range(k): d[s[i]][i % 3] += 1 res = max( d["R"][0] + d["G"][1] + d["B"][2], d["R"][1] + d["G"][2] + d["B"][0], d["R"][2] + d["G"][0] + d["B"][1], ) for i in range(k, n): d[s[i]][i % 3] += 1 d[s[i - k]][(i - k) % 3] -= 1 res = max( res, d["R"][0] + d["G"][1] + d["B"][2], d["R"][1] + d["G"][2] + d["B"][0], d["R"][2] + d["G"][0] + d["B"][1], ) if res >= k: break print(k - res)
IMPORT 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 FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR STRING LIST NUMBER NUMBER NUMBER ASSIGN VAR STRING LIST NUMBER NUMBER NUMBER ASSIGN VAR STRING LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER BIN_OP BIN_OP VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER BIN_OP BIN_OP VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER BIN_OP BIN_OP VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER BIN_OP BIN_OP VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
def main(): q = int(input()) ans = [] for i in range(q): n, k = map(int, input().split()) s = input() min_ans = 10**9 pr1 = [0] pr2 = [0] pr3 = [0] for i in range(n): count1 = 0 count2 = 0 count3 = 0 if i % 3 == 0: if s[i] != "R": count1 += 1 if s[i] != "G": count2 += 1 if s[i] != "B": count3 += 1 if i % 3 == 1: if s[i] != "G": count1 += 1 if s[i] != "B": count2 += 1 if s[i] != "R": count3 += 1 if i % 3 == 2: if s[i] != "B": count1 += 1 if s[i] != "R": count2 += 1 if s[i] != "G": count3 += 1 pr1.append(pr1[-1] + count1) pr2.append(pr2[-1] + count2) pr3.append(pr3[-1] + count3) j = i + 1 if j >= k: count1 = pr1[j] - pr1[j - k] count2 = pr2[j] - pr2[j - k] count3 = pr3[j] - pr3[j - k] min_ans = min(min_ans, count1, count2, count3) ans.append(min_ans) print(*ans, sep="\n") main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin input = stdin.readline q = int(input()) for ppppp in range(q): [n, k] = [int(item) for item in input().split(" ")] x = input() inf_seq = "RGB" ptr = 0 weights = [] cost = [0, 0, 0] for i in range(k): dd = [0, 0, 0] for d in range(3): if x[i] != inf_seq[(i + d) % 3]: dd[d] = 1 cost[d] += 1 weights.append(dd) min_changes = min(cost) for i in range(k, n): dd = [0, 0, 0] for d in range(3): cost[d] -= weights[i - k][d] if x[i] != inf_seq[(i + d) % 3]: dd[d] = 1 cost[d] += 1 weights.append(dd) min_changes = min(min_changes, min(cost)) print(min_changes)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
q = int(input()) out = "" for _ in range(q): n, k = list(map(int, input().split())) s = input() rgb = 0 gbr = 0 brg = 0 RGB = "RGB" for i in range(k): if i % 3 == 0: rgb += int(s[i] != "R") gbr += int(s[i] != "G") brg += int(s[i] != "B") elif i % 3 == 1: rgb += int(s[i] != "G") gbr += int(s[i] != "B") brg += int(s[i] != "R") else: rgb += int(s[i] != "B") gbr += int(s[i] != "R") brg += int(s[i] != "G") minimum = min(rgb, brg, gbr) for i in range(k, n): if (i - k) % 3 == 0: rgb -= int(s[i - k] != "R") gbr -= int(s[i - k] != "G") brg -= int(s[i - k] != "B") elif (i - k) % 3 == 1: rgb -= int(s[i - k] != "G") gbr -= int(s[i - k] != "B") brg -= int(s[i - k] != "R") else: rgb -= int(s[i - k] != "B") gbr -= int(s[i - k] != "R") brg -= int(s[i - k] != "G") if i % 3 == 0: rgb += int(s[i] != "R") gbr += int(s[i] != "G") brg += int(s[i] != "B") elif i % 3 == 1: rgb += int(s[i] != "G") gbr += int(s[i] != "B") brg += int(s[i] != "R") else: rgb += int(s[i] != "B") gbr += int(s[i] != "R") brg += int(s[i] != "G") minimum = min(minimum, rgb, brg, gbr) if minimum == 0: break out += str(minimum) + "\n" print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
def change(d): for i in range(0, len(d)): if d[i] == "R": d[i] = 1 elif d[i] == "G": d[i] = 2 else: d[i] = 3 return d a = int(input()) if a < 100000: for i in range(0, a): n, k = map(int, input().split()) c = list(input()) c = change(c) e = 0 f = 0 g = 0 h = [0] * (3 * (n - k + 1)) mod3 = [0] * n for i in range(0, n): mod3[i] = (c[i] - i) % 3 for i in range(0, k): if mod3[i] == 0: h[0] = h[0] + 1 elif mod3[i] == 1: h[1] = h[1] + 1 elif mod3[i] == 2: h[2] = h[2] + 1 for i in range(0, n - k): if mod3[i] == 1: if mod3[i + k] == 1: h[3 * (i + 1)] = h[3 * i] h[3 * (i + 1) + 1] = h[3 * i + 1] h[3 * (i + 1) + 2] = h[3 * i + 2] elif mod3[i + k] == 2: h[3 * (i + 1)] = h[3 * i] h[3 * (i + 1) + 1] = h[3 * i + 1] - 1 h[3 * (i + 1) + 2] = h[3 * i + 2] + 1 elif mod3[i + k] == 0: h[3 * (i + 1)] = h[3 * i] + 1 h[3 * (i + 1) + 1] = h[3 * i + 1] - 1 h[3 * (i + 1) + 2] = h[3 * i + 2] elif mod3[i] == 2: if mod3[i + k] == 1: h[3 * (i + 1)] = h[3 * i] h[3 * (i + 1) + 1] = h[3 * i + 1] + 1 h[3 * (i + 1) + 2] = h[3 * i + 2] - 1 elif mod3[i + k] == 2: h[3 * (i + 1)] = h[3 * i] h[3 * (i + 1) + 1] = h[3 * i + 1] h[3 * (i + 1) + 2] = h[3 * i + 2] elif mod3[i + k] == 0: h[3 * (i + 1)] = h[3 * i] + 1 h[3 * (i + 1) + 1] = h[3 * i + 1] h[3 * (i + 1) + 2] = h[3 * i + 2] - 1 elif mod3[i] == 0: if mod3[i + k] == 1: h[3 * (i + 1)] = h[3 * i] - 1 h[3 * (i + 1) + 1] = h[3 * i + 1] + 1 h[3 * (i + 1) + 2] = h[3 * i + 2] elif mod3[i + k] == 2: h[3 * (i + 1)] = h[3 * i] - 1 h[3 * (i + 1) + 1] = h[3 * i + 1] h[3 * (i + 1) + 2] = h[3 * i + 2] + 1 elif mod3[i + k] == 0: h[3 * (i + 1)] = h[3 * i] h[3 * (i + 1) + 1] = h[3 * i + 1] h[3 * (i + 1) + 2] = h[3 * i + 2] print(k - max(h)) elif a == 100000: for i in range(0, a): n, k = map(int, input().split()) c = list(input()) e = 0 if k == 1: print(0) elif n > 2: e = [0] * (3 * (n - k + 1)) c = change(c) for i in range(0, n - k + 1): for j in range(i, i + k): if (c[j] - c[i]) % 3 != (j - i) % 3: e[3 * i] = e[3 * i] + 1 if (c[j] - c[i]) % 3 != (j - i - 1) % 3: e[3 * i + 1] = e[3 * i + 1] + 1 if (c[j] - c[i]) % 3 != (j - i - 2) % 3: e[3 * i + 2] = e[3 * i + 2] + 1 e.sort() print(e[0]) else: c = change(c) if c[0] == 1: if c[1] == 2: print(0) else: print(1) elif c[0] == 2: if c[1] == 3: print(0) else: print(1) elif c[0] == 3: if c[1] == 1: print(0) else: print(1) else: for i in range(0, a): print(0)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys t = int(sys.stdin.readline()) x = "RGB" y = "GBR" z = "BRG" for i in range(t): n, k = list(map(int, sys.stdin.readline().strip().split())) a = sys.stdin.readline().strip() xk = x yk = y zk = z op = 2001 xd = [] yd = [] zd = [] xdc = 0 ydc = 0 zdc = 0 b = a for j in range(k): if b[j] != xk[j % 3]: xd.append(1) xdc += 1 else: xd.append(0) if b[j] != yk[j % 3]: yd.append(1) ydc += 1 else: yd.append(0) if b[j] != zk[j % 3]: zdc += 1 zd.append(1) else: zd.append(0) op = min(xdc, ydc, zdc) for j in range(k, n): if b[j] != xk[j % 3]: xd.append(1) xdc += 1 else: xd.append(0) if b[j] != yk[j % 3]: yd.append(1) ydc += 1 else: yd.append(0) if b[j] != zk[j % 3]: zdc += 1 zd.append(1) else: zd.append(0) xdc -= xd[j - k] ydc -= yd[j - k] zdc -= zd[j - k] op = min(op, xdc, ydc, zdc) print(op)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin input = stdin.readline t = int(input()) def foo(seq, index): if index == None: return seq[0] index = index % 3 return seq[index] for j in range(t): n, k = list(map(int, input().split(" "))) chars = list(input()) chars.insert(0, None) diff = [[] for i in range(n + 1)] diff[0] = [0, 0, 0] for i in range(1, n + 1): diff[i].append(foo("RGB", i - 1) != chars[i]) diff[i].append(foo("GBR", i - 1) != chars[i]) diff[i].append(foo("BRG", i - 1) != chars[i]) for i in range(2, n + 1): diff[i][0] += diff[i - 1][0] diff[i][1] += diff[i - 1][1] diff[i][2] += diff[i - 1][2] res = float("inf") for i in range(k, n + 1): res = min(res, diff[i][0] - diff[i - k][0]) res = min(res, diff[i][1] - diff[i - k][1]) res = min(res, diff[i][2] - diff[i - k][2]) print(res)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NONE RETURN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NONE ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin t = int(stdin.readline()) for _ in range(t): n, k = map(int, stdin.readline().split()) l = stdin.readline() m = float("inf") for q in range(3): if q == 0: s = "RGB" elif q == 1: s = "GBR" else: s = "BRG" i, count, c = 0, 0, 0 while i < k: if l[i] != s[c]: count += 1 i += 1 c += 1 if c == 3: c = 0 m = min(m, count) last = s[0] while i < n: if l[i - k] != last: count -= 1 if l[i] != s[c]: count += 1 i += 1 c += 1 if c == 3: c = 0 m = min(m, count) if last == s[0]: last = s[1] elif last == s[1]: last = s[2] else: last = s[0] print(m)
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys A = "RGB" B = "GBR" C = "BRG" for nt in range(int(sys.stdin.readline())): n, k = map(int, sys.stdin.readline().split()) s = sys.stdin.readline() a = b = c = 0 for i in range(k): if s[i] != A[i % 3]: a += 1 if s[i] != B[i % 3]: b += 1 if s[i] != C[i % 3]: c += 1 ans = min(a, b, c) for i in range(k, n): if s[i - k] != A[(i - k) % 3]: a -= 1 if s[i - k] != B[(i - k) % 3]: b -= 1 if s[i - k] != C[(i - k) % 3]: c -= 1 if s[i] != A[i % 3]: a += 1 if s[i] != B[i % 3]: b += 1 if s[i] != C[i % 3]: c += 1 ans = min(ans, a, b, c) print(ans)
IMPORT ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING 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 FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
q = int(input()) f = [] for i in range(q): n, k = map(int, input().split()) s = input() mi = 0 num = [0, 0, 0] for i in range(n): if i % 3 == 0: if s[i] == "R": num[0] += 1 elif s[i] == "G": num[1] += 1 else: num[2] += 1 elif i % 3 == 1: if s[i] == "G": num[0] += 1 elif s[i] == "B": num[1] += 1 else: num[2] += 1 elif s[i] == "B": num[0] += 1 elif s[i] == "R": num[1] += 1 else: num[2] += 1 if i >= k: if (i - k) % 3 == 0: if s[i - k] == "R": num[0] -= 1 elif s[i - k] == "G": num[1] -= 1 else: num[2] -= 1 elif (i - k) % 3 == 1: if s[i - k] == "G": num[0] -= 1 elif s[i - k] == "B": num[1] -= 1 else: num[2] -= 1 elif s[i - k] == "B": num[0] -= 1 elif s[i - k] == "R": num[1] -= 1 else: num[2] -= 1 e = max(num) if mi < e: mi = e f += [k - mi] for i in f: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR STRING VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR STRING VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR STRING VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR LIST BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin input = stdin.readline q = int(input()) for _ in range(q): n, k = [int(i) for i in input().split()] s = input() start = [0] * n for i in range(n): if s[i] == "R": start[i] = i % 3 elif s[i] == "G": start[i] = (i - 1) % 3 else: start[i] = (i - 2) % 3 act = [0] * 3 for i in range(k): act[start[i]] += 1 res = max(act) for i in range(k, n): act[start[i]] += 1 act[start[i - k]] -= 1 res = max([act[start[i]], res]) print(k - res)
ASSIGN VAR VAR 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 ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys quries = int(sys.stdin.readline()) result = [] mapping = {"R": "G", "G": "B", "B": "R"} rev_mapping = {"R": "B", "B": "G", "G": "R"} count = {"R": 0, "B": 0, "G": 0} memory = {} for i in range(quries): n, k = [int(x) for x in sys.stdin.readline().split()] best = 10000000 lis = sys.stdin.readline() count = {"R": 0, "B": 0, "G": 0} for cur in ["R", "G", "B"]: current = cur for s in range(0, k): if lis[s] != current: count[cur] += 1 current = mapping[current] for s, value in count.items(): best = min(best, value) for s in range(1, n - k + 1): count_temp = {"R": 0, "G": 0, "B": 0} for cur in ["R", "G", "B"]: count_temp[cur] = count[rev_mapping[cur]] if lis[s - 1] != rev_mapping[cur]: count_temp[cur] -= 1 needed = "c" if k % 3 == 0: needed = mapping[mapping[cur]] elif k % 3 == 1: needed = cur else: needed = mapping[cur] if lis[s + k - 1] != needed: count_temp[cur] += 1 count = count_temp for s, value in count.items(): best = min(best, value) print(best, flush=False)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR LIST STRING STRING STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR LIST STRING STRING STRING ASSIGN VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys input = lambda: sys.stdin.readline().strip() nxt = {"R": "G", "G": "B", "B": "R"} T = int(input()) for _ in range(T): n, k = list(map(int, input().split())) s = input() res = [] for start in ["R", "G", "B"]: mis = [] cur = start for j in range(k): if s[j] != cur: mis.append(1) else: mis.append(0) cur = nxt[cur] res.append(sum(mis)) for j in range(k, n): res.append(res[-1] + int(s[j] != cur) - mis[j - k]) if s[j] != cur: mis.append(1) else: mis.append(0) cur = nxt[cur] print(min(res))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING 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 FUNC_CALL VAR ASSIGN VAR LIST FOR VAR LIST STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin, stdout def check(s, k, n): t = "RGB" ans = int(1000000000.0) for off in range(0, 3): res = [None] * n curr = 0 for j in range(n): res[j] = s[j] != t[(j + off) % 3] curr += res[j] if j >= k: curr -= res[j - k] if j >= k - 1: ans = min(ans, curr) return ans t = int(stdin.readline()) while t: nk = list(map(int, stdin.readline().rstrip().split())) n = nk[0] k = nk[1] s = stdin.readline().rstrip().lstrip() stdout.write(str(check(s, k, n)) + "\n") t = t - 1
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin input = stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) s = list(input()) ans = 10**9 a = ["R", "G", "B"] for l in range(3): x = l dp = [0] * (n + 1) cnt = 10**9 for i in range(n): if s[i] != a[x]: dp[i + 1] = dp[i] + 1 else: dp[i + 1] = dp[i] x = (x + 1) % 3 for i in range(1, n - k + 2): cnt = min(cnt, dp[i + k - 1] - dp[i - 1]) ans = min(cnt, ans) print(ans)
ASSIGN VAR VAR 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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys input = sys.stdin.readline for nt in range(int(input())): n, k = map(int, input().split()) s = input() s1 = "RGB" dp = [[0, 0, 0] for i in range(n)] ans = 10**18 for i in range(3): for j in range(k): if s[j] != s1[(i + j) % 3]: dp[0][i] += 1 ans = min(ans, dp[0][i]) for j in range(1, n - k + 1): if s[j - 1] != s1[(i + j - 1) % 3]: dp[j][i] += dp[j - 1][i] - 1 else: dp[j][i] += dp[j - 1][i] if s[j + k - 1] != s1[(i + j + k - 1) % 3]: dp[j][i] += 1 ans = min(ans, dp[j][i]) print(ans)
IMPORT ASSIGN VAR VAR 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 FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin, stdout input = stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) s = str(input()) s1 = "RGB" s2 = "GBR" s3 = "BRG" ans = 200000 cnt1, cnt2, cnt3 = 0, 0, 0 dp = [[(0) for i in range(3)] for j in range(n + 1)] dp[0][0] = 0 dp[0][1] = 0 dp[0][2] = 0 for i in range(n): if s[i] != s1[i % 3]: cnt1 += 1 if s[i] != s2[i % 3]: cnt2 += 1 if s[i] != s3[i % 3]: cnt3 += 1 dp[i + 1][0] = cnt1 dp[i + 1][1] = cnt2 dp[i + 1][2] = cnt3 for i in range(k, n + 1): ans = min( ans, dp[i][0] - dp[i - k][0], dp[i][1] - dp[i - k][1], dp[i][2] - dp[i - k][2], ) print(ans)
ASSIGN VAR VAR 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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin t = int(input()) for _ in range(t): n, k = map(int, stdin.readline().split()) s = stdin.readline() one = "RGB" * (n // 3) s1 = "RGB" one = one + s1[: n % 3] two = "GBR" * (n // 3) s2 = "GBR" two = two + s2[: n % 3] s3 = "BRG" three = s3 * (n // 3) three = three + s3[: n % 3] i, j = 0, 0 count1, count2, count3, m = 0, 0, 0, n while j < n: while j - i < k: if s[j] != one[j]: count1 += 1 if s[j] != two[j]: count2 += 1 if s[j] != three[j]: count3 += 1 j += 1 m = min(count1, count2, count3, m) if s[i] != one[i]: count1 -= 1 if s[i] != two[i]: count2 -= 1 if s[i] != three[i]: count3 -= 1 i += 1 print(m)
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 FUNC_CALL VAR ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR WHILE VAR VAR WHILE BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin q = int(input()) INF = 10**18 for _ in range(q): n, k = map(int, stdin.readline().split()) s = stdin.readline()[:-1] r = ("RGB" * k)[:k] g = ("GBR" * k)[:k] b = ("BRG" * k)[:k] dp = [([0] * 3) for i in range(n)] for i in range(k): if s[i] == r[i]: dp[k - 1][0] += 1 elif s[i] == g[i]: dp[k - 1][1] += 1 elif s[i] == b[i]: dp[k - 1][2] += 1 ans = max(dp[k - 1]) for i in range(k, n): dp[i][0] = dp[i - 1][2] - (s[i - k] == b[0]) + (s[i] == r[k - 1]) dp[i][1] = dp[i - 1][0] - (s[i - k] == r[0]) + (s[i] == g[k - 1]) dp[i][2] = dp[i - 1][1] - (s[i - k] == g[0]) + (s[i] == b[k - 1]) ans = max(ans, max(dp[i])) print(k - ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys input = sys.stdin.readline Q = int(input()) D = {"R": 0, "G": 1, "B": 2} for _ in range(Q): N, K = list(map(int, input().split())) S = input() mi = K for i in range(3): d = 0 for j in range(N): if D[S[j]] != (i + j) % 3: d += 1 if j >= K and D[S[j - K]] != (i + j - K) % 3: d -= 1 if j >= K - 1: mi = min(mi, d) print(mi)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys input = sys.stdin.readline rgb = ["R", "G", "B"] def func(s): r = [0, 0, 0] i = 0 for c in s: if c == rgb[i]: r[1] += 1 r[2] += 1 elif c == rgb[(i + 1) % 3]: r[2] += 1 r[0] += 1 else: r[0] += 1 r[1] += 1 i = (i + 1) % 3 return r for _ in range(int(input())): n, k = map(int, input().split()) s = input() dp = func(s[:k]) ans = min(dp) x, y, z = (k - 1 + 3) % 3, 0, 1 for i in range(k, n): for j in range(3): if s[i - k] != rgb[y]: dp[j] -= 1 y = (y + 1) % 3 dp[1], dp[2], dp[0] = dp[0], dp[1], dp[2] for j in range(3): if s[i] != rgb[x]: dp[j] += 1 x = (x + 1) % 3 ans = min(ans, min(dp)) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR LIST STRING STRING STRING FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys input = sys.stdin.readline def main(): Q = int(input()) data = [] for _ in range(Q): N, K = map(int, input().split()) S = input().rstrip() data.append([N, K, S]) RGB = [] for i in range(300000): if i % 3 == 0: RGB.append("R") elif i % 3 == 1: RGB.append("G") else: RGB.append("B") for N, K, S in data: ans = N dp = [[(0) for _ in range(N)] for _ in range(3)] for j in range(3): for i in range(N): if S[i] != RGB[i + j]: dp[j][i] = 1 for j in range(3): p = 0 for k in range(K): p += dp[j][k] ans = min(ans, p) for i in range(1, N - K + 1): p += dp[j][K - 1 + i] - dp[j][i - 1] ans = min(ans, p) print(ans) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
q = int(input()) rgb = "RGB" answer = [] for req in range(q): n, k = map(int, input().split()) s = input() last1 = rgb[(k - 1) % 3] last2 = rgb[k % 3] last3 = rgb[(k + 1) % 3] dp = [0, 0, 0] for i in range(k): if s[i] != rgb[i % 3]: dp[0] += 1 if s[i] != rgb[(i + 1) % 3]: dp[1] += 1 if s[i] != rgb[(i + 2) % 3]: dp[2] += 1 count = min(dp) for i in range(k, n): first = s[i - k] if first != "R": dp[0] -= 1 if first != "G": dp[1] -= 1 if first != "B": dp[2] -= 1 dp.insert(0, dp.pop()) last = s[i] if last != last1: dp[0] += 1 if last != last2: dp[1] += 1 if last != last3: dp[2] += 1 count = min(min(dp), count) answer.append(str(count)) print("\n".join(answer))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR STRING VAR NUMBER NUMBER IF VAR STRING VAR NUMBER NUMBER IF VAR STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys def process(S, k, n): if k == 1: return 0 answer = float("inf") current = [0, 0, 0] indexes = {"R": 0, "G": 1, "B": 2} for i in range(k): c = chr(S[i]) index = (indexes[c] - i) % 3 for j in range(3): current[j] += 1 current[index] -= 1 answer = min(answer, min(current)) for i in range(k, n): c1 = chr(S[i - k]) index1 = (indexes[c1] - i + k) % 3 for j in range(3): current[j] -= 1 current[index1] += 1 c = chr(S[i]) index = (indexes[c] - i) % 3 for j in range(3): current[j] += 1 current[index] -= 1 answer = min(answer, min(current)) return answer input = sys.stdin.buffer.readline t = int(input()) for i in range(t): n, k = [int(x) for x in input().split()] S = input() if k == 1: print(0) else: print(process(S, k, n))
IMPORT FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR 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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys input = sys.stdin.readline pure = "RGB" * 100006 for _ in range(int(input())): n, k = map(int, input().split()) s = input() ans = 0 Rc = 0 Gc = 0 Bc = 0 count = 0 for i in range(k): if pure[i] == s[i]: Rc += 1 elif pure[i - 1] == s[i]: Bc += 1 elif pure[i - 2] == s[i]: Gc += 1 ans = max(ans, Rc, Gc, Bc) for i in range(k, n): if pure[i] == s[i]: Rc += 1 if pure[i - k] == s[i - k]: Rc -= 1 if pure[i - 1] == s[i]: Bc += 1 if pure[i - k - 1] == s[i - k]: Bc -= 1 if pure[i - 2] == s[i]: Gc += 1 if pure[i - k - 2] == s[i - k]: Gc -= 1 ans = max(ans, Rc, Gc, Bc) print(k - ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP STRING NUMBER 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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys input = sys.stdin.readline RI = lambda: [int(x) for x in sys.stdin.readline().strip().split()] rw = lambda: input().strip().split() infinite = float("inf") t = int(input()) for _ in range(t): n, k = RI() s = input() mini = n count = 0 test = "RGB" * (k // 3 + 5) for j in range(k): if s[j] != test[j]: count += 1 mini = min(count, mini) count1 = count count = 0 test = "GBR" * (k // 3 + 5) for j in range(k): if s[j] != test[j]: count += 1 mini = min(count, mini) count2 = count count = 0 test = "BRG" * (k // 3 + 5) for j in range(k): if s[j] != test[j]: count += 1 mini = min(count, mini) count3 = count count = 0 for i in range(1, n - k + 1): if s[i - 1] == "B": count = count3 temp = "RGB" * (k // 3 + 5) if s[i + k - 1] != temp[k - 1]: count += 1 mini = min(count, mini) count1_new = count count = count1 - 1 temp = "GBR" * (k // 3 + 5) if s[i + k - 1] != temp[k - 1]: count += 1 mini = min(count, mini) count2_new = count temp = "BRG" * (k // 3 + 5) count = count2 - 1 if s[i + k - 1] != temp[k - 1]: count += 1 mini = min(count, mini) count3_new = count count1 = count1_new count2 = count2_new count3 = count3_new elif s[i - 1] == "R": temp = "GBR" * (k // 3 + 5) count = count1 if s[i + k - 1] != temp[k - 1]: count += 1 mini = min(count, mini) count2_new = count temp = "BRG" * (k // 3 + 5) count = count2 - 1 if s[i + k - 1] != temp[k - 1]: count += 1 mini = min(count, mini) count3_new = count temp = "RGB" * (k // 3 + 5) count = count3 - 1 if s[i + k - 1] != temp[k - 1]: count += 1 mini = min(count, mini) count1_new = count count1 = count1_new count2 = count2_new count3 = count3_new else: count = count2 temp = "BRG" * (k // 3 + 5) if s[i + k - 1] != temp[k - 1]: count += 1 mini = min(count, mini) count3_new = count temp = "RGB" * (k // 3 + 5) count = count3 - 1 if s[i + k - 1] != temp[k - 1]: count += 1 mini = min(count, mini) count1_new = count temp = "GBR" * (k // 3 + 5) count = count1 - 1 if s[i + k - 1] != temp[k - 1]: count += 1 mini = min(count, mini) count2_new = count count1 = count1_new count2 = count2_new count3 = count3_new print(mini)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin input = stdin.readline input = stdin.readline t = int(input()) for i in range(t): n, k = map(int, input().split(" ")) str_1 = input() str_2 = ["GBR", "BRG", "RGB"] do = [] for h in str_2: count = 0 for j in range(k): if str_1[j] != h[j % 3]: count += 1 do.append(count) for j in range(k, n): if str_1[j - k] != h[(j - k) % 3]: count -= 1 if str_1[j] != h[j % 3]: count += 1 do.append(count) print(min(do))
ASSIGN VAR VAR ASSIGN VAR VAR 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 STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
import sys for _ in range(int(input())): n, k = map(int, sys.stdin.readline().split()) a = sys.stdin.readline() if n == 1: print(0) else: next = a[:2] parsum1 = [(0) for _ in range(n + 1)] parsum2 = [(0) for _ in range(n + 1)] parsum3 = [(0) for _ in range(n + 1)] check = "RGB" i1 = 0 s1 = 0 i2 = 1 s2 = 0 i3 = 2 s3 = 0 for i in range(n): if a[i] != check[i1]: s1 += 1 if a[i] != check[i2]: s2 += 1 if a[i] != check[i3]: s3 += 1 i1 = i1 + 1 if i1 + 1 < 3 else 0 i2 = i2 + 1 if i2 + 1 < 3 else 0 i3 = i3 + 1 if i3 + 1 < 3 else 0 parsum1[i + 1] = s1 parsum2[i + 1] = s2 parsum3[i + 1] = s3 mn1 = n mn2 = n mn3 = n i = 1 while 1: mxRange = i + k - 1 if mxRange <= n: mn1 = min(parsum1[mxRange] - parsum1[i - 1], mn1) mn2 = min(parsum2[mxRange] - parsum2[i - 1], mn2) mn3 = min(parsum3[mxRange] - parsum3[i - 1], mn3) else: break i += 1 sys.stdout.write("{}\n".format(min(mn1, mn2, mn3)))
IMPORT 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 FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN 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 IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from itertools import chain from sys import stdin, stdout def main(): input = stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) ans = [k, k, k, 1, 1] fin_ans = k inp = tuple( (ord("r") + i - ord(c)) % 3 for i, c in enumerate(input().rstrip().lower()) ) for inp1, inp2 in zip(inp, chain((4,) * k, inp)): ans[inp2] += 1 ans[inp1] -= 1 fin_ans = min(fin_ans, ans[inp1]) stdout.write(f"{fin_ans}\n") main()
FUNC_DEF ASSIGN VAR VAR 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 VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin, stdout input = stdin.readline for _ in range(int(input())): x = 10**5 n, k = list(map(int, input().split())) s = input() a = 10**9 ans = [([0] * n) for i in range(3)] curr = ["R", "G", "B"] for l in range(3): z = l for j in range(n): if s[j] != curr[z]: ans[l][j] = 1 z += 1 z %= 3 for i in range(3): ans[i] = [0] + ans[i] for l in range(3): z = l for j in range(1, n + 1): ans[l][j] += ans[l][j - 1] for l in range(3): for j in range(1, n - k + 2): a = min(a, ans[l][j + k - 1] - ans[l][j - 1]) print(a)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
from sys import stdin, stdout inp = stdin.readline for _ in range(int(inp())): n, k = map(int, inp().split()) a, b, c, ans = [0], [0], [0], 200000.0 + 5 s = inp() for i in range(n): a.append(a[-1] + (s[i] != "RGB"[i % 3])) b.append(b[-1] + (s[i] != "GBR"[i % 3])) c.append(c[-1] + (s[i] != "BRG"[i % 3])) for i in range(n - k + 1): ans = min(ans, a[i + k] - a[i], b[i + k] - b[i], c[i + k] - c[i]) stdout.write(str(ans) + "\n")
ASSIGN VAR VAR 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 VAR VAR VAR LIST NUMBER LIST NUMBER LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is the size of the input. You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Then $q$ queries follow. The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the string $s$ and the length of the substring. The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...". -----Example----- Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Output 1 0 3 -----Note----- In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
def two(x): for i in range(len(x) - 1): if x[i] == "R" and x[i + 1] == "G": return 0 elif x[i] == "G" and x[i + 1] == "B": return 0 elif x[i] == "B" and x[i + 1] == "R": return 0 return 1 t = int(input()) for i in range(t): n, k = map(int, input().split()) x = input() if k == 1: print(0) continue elif k == 2: print(two(x)) continue d = [["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"]] ans = 10**10 for j in range(3): count = 0 for i in range(k): if x[i] != d[j][i % 3]: count += 1 ans = min(ans, count) for i in range(k, n): if x[i] != d[j][i % 3]: count += 1 if x[i - k] != d[j][(i - k) % 3]: count -= 1 ans = min(ans, count) print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING RETURN NUMBER RETURN NUMBER 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 FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR