description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You're creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from $1$, and in each cell you can either put a platform or leave it empty. In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell $p$, then bounces off it, then bounces off a platform in the cell $(p + k)$, then a platform in the cell $(p + 2k)$, and so on every $k$-th platform until it goes farther than the last cell. If any of these cells has no platform, you can't pass the level with these $p$ and $k$. You already have some level pattern $a_1$, $a_2$, $a_3$, ..., $a_n$, where $a_i = 0$ means there is no platform in the cell $i$, and $a_i = 1$ means there is one. You want to modify it so that the level can be passed with given $p$ and $k$. In $x$ seconds you can add a platform in some empty cell. In $y$ seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can't do any other operation. You can not reduce the number of cells to less than $p$. Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added. What is the minimum number of seconds you need to make this level passable with given $p$ and $k$? -----Input----- The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of test cases follows. The first line of each test case contains three integers $n$, $p$, and $k$ ($1 \le p \le n \le 10^5$, $1 \le k \le n$) β€” the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required. The second line of each test case contains a string $a_1 a_2 a_3 \ldots a_n$ ($a_i = 0$ or $a_i = 1$) β€” the initial pattern written without spaces. The last line of each test case contains two integers $x$ and $y$ ($1 \le x, y \le 10^4$) β€” the time required to add a platform and to remove the first cell correspondingly. The sum of $n$ over test cases does not exceed $10^5$. -----Output----- For each test case output a single integer β€” the minimum number of seconds you need to modify the level accordingly. It can be shown that it is always possible to make the level passable. -----Examples----- Input 3 10 3 2 0101010101 2 2 5 4 1 00000 2 10 11 2 3 10110011000 4 3 Output 2 4 10 -----Note----- In the first test case it's best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is $y = 2$. In the second test case it's best to add a platform to both cells $4$ and $5$: 00000 $\to$ 00011. The time required is $x \cdot 2 = 4$. In the third test case it's best to to remove the first cell twice and then add a platform to the cell which was initially $10$-th: 10110011000 $\to$ 10110011010. The time required is $y \cdot 2 + x = 10$.
t = int(input()) for l in range(t): n, p, k = map(int, input().split()) a = list(map(int, list(input()))) a = [(1 - a[i]) for i in range(n)] x, y = map(int, input().split()) p -= 1 mn = n * x sumadds = [0] * k start = [i for i in range(k)] for i in range(n): sumadds[i % k] += a[i] for i in range(p, n): while start[i % k] != i: sumadds[i % k] -= a[start[i % k]] start[i % k] += k mn = min(mn, sumadds[i % k] * x + (i - p) * y) print(mn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You're creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from $1$, and in each cell you can either put a platform or leave it empty. In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell $p$, then bounces off it, then bounces off a platform in the cell $(p + k)$, then a platform in the cell $(p + 2k)$, and so on every $k$-th platform until it goes farther than the last cell. If any of these cells has no platform, you can't pass the level with these $p$ and $k$. You already have some level pattern $a_1$, $a_2$, $a_3$, ..., $a_n$, where $a_i = 0$ means there is no platform in the cell $i$, and $a_i = 1$ means there is one. You want to modify it so that the level can be passed with given $p$ and $k$. In $x$ seconds you can add a platform in some empty cell. In $y$ seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can't do any other operation. You can not reduce the number of cells to less than $p$. Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added. What is the minimum number of seconds you need to make this level passable with given $p$ and $k$? -----Input----- The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of test cases follows. The first line of each test case contains three integers $n$, $p$, and $k$ ($1 \le p \le n \le 10^5$, $1 \le k \le n$) β€” the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required. The second line of each test case contains a string $a_1 a_2 a_3 \ldots a_n$ ($a_i = 0$ or $a_i = 1$) β€” the initial pattern written without spaces. The last line of each test case contains two integers $x$ and $y$ ($1 \le x, y \le 10^4$) β€” the time required to add a platform and to remove the first cell correspondingly. The sum of $n$ over test cases does not exceed $10^5$. -----Output----- For each test case output a single integer β€” the minimum number of seconds you need to modify the level accordingly. It can be shown that it is always possible to make the level passable. -----Examples----- Input 3 10 3 2 0101010101 2 2 5 4 1 00000 2 10 11 2 3 10110011000 4 3 Output 2 4 10 -----Note----- In the first test case it's best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is $y = 2$. In the second test case it's best to add a platform to both cells $4$ and $5$: 00000 $\to$ 00011. The time required is $x \cdot 2 = 4$. In the third test case it's best to to remove the first cell twice and then add a platform to the cell which was initially $10$-th: 10110011000 $\to$ 10110011010. The time required is $y \cdot 2 + x = 10$.
t = int(input()) for i in range(0, t): l1 = [int(x) for x in input().split()] n = l1[0] p = l1[1] k = l1[2] inp_str = input() l2 = [int(x) for x in input().split()] add = l2[0] rem = l2[1] c = [(0) for x in range(0, n)] for j in range(n - 1, -1, -1): if j + k < n: if inp_str[j] == "0": c[j] = c[j + k] + 1 else: c[j] = c[j + k] elif inp_str[j] == "0": c[j] = 1 else: c[j] = 0 p -= 1 mini = 10**9 for j in range(p, n): val = (j - p) * rem + add * c[j] if mini > val: mini = val print(mini)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You're creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from $1$, and in each cell you can either put a platform or leave it empty. In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell $p$, then bounces off it, then bounces off a platform in the cell $(p + k)$, then a platform in the cell $(p + 2k)$, and so on every $k$-th platform until it goes farther than the last cell. If any of these cells has no platform, you can't pass the level with these $p$ and $k$. You already have some level pattern $a_1$, $a_2$, $a_3$, ..., $a_n$, where $a_i = 0$ means there is no platform in the cell $i$, and $a_i = 1$ means there is one. You want to modify it so that the level can be passed with given $p$ and $k$. In $x$ seconds you can add a platform in some empty cell. In $y$ seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can't do any other operation. You can not reduce the number of cells to less than $p$. Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added. What is the minimum number of seconds you need to make this level passable with given $p$ and $k$? -----Input----- The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of test cases follows. The first line of each test case contains three integers $n$, $p$, and $k$ ($1 \le p \le n \le 10^5$, $1 \le k \le n$) β€” the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required. The second line of each test case contains a string $a_1 a_2 a_3 \ldots a_n$ ($a_i = 0$ or $a_i = 1$) β€” the initial pattern written without spaces. The last line of each test case contains two integers $x$ and $y$ ($1 \le x, y \le 10^4$) β€” the time required to add a platform and to remove the first cell correspondingly. The sum of $n$ over test cases does not exceed $10^5$. -----Output----- For each test case output a single integer β€” the minimum number of seconds you need to modify the level accordingly. It can be shown that it is always possible to make the level passable. -----Examples----- Input 3 10 3 2 0101010101 2 2 5 4 1 00000 2 10 11 2 3 10110011000 4 3 Output 2 4 10 -----Note----- In the first test case it's best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is $y = 2$. In the second test case it's best to add a platform to both cells $4$ and $5$: 00000 $\to$ 00011. The time required is $x \cdot 2 = 4$. In the third test case it's best to to remove the first cell twice and then add a platform to the cell which was initially $10$-th: 10110011000 $\to$ 10110011010. The time required is $y \cdot 2 + x = 10$.
t = int(input()) for _ in range(t): n, p, k = map(int, input().split()) s = input() x, y = map(int, input().split()) add_cost = [(0) for i in range(n - p + 1)] for i in range(n - p, -1, -1): add_cost[i] = 1 - int(s[i + p - 1]) if i + k <= n - p: add_cost[i] += add_cost[i + k] ans = add_cost[0] * x for i in range(1, n - p + 1): curr_cost = add_cost[i] * x + i * y ans = min(ans, curr_cost) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You're creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from $1$, and in each cell you can either put a platform or leave it empty. In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell $p$, then bounces off it, then bounces off a platform in the cell $(p + k)$, then a platform in the cell $(p + 2k)$, and so on every $k$-th platform until it goes farther than the last cell. If any of these cells has no platform, you can't pass the level with these $p$ and $k$. You already have some level pattern $a_1$, $a_2$, $a_3$, ..., $a_n$, where $a_i = 0$ means there is no platform in the cell $i$, and $a_i = 1$ means there is one. You want to modify it so that the level can be passed with given $p$ and $k$. In $x$ seconds you can add a platform in some empty cell. In $y$ seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can't do any other operation. You can not reduce the number of cells to less than $p$. Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added. What is the minimum number of seconds you need to make this level passable with given $p$ and $k$? -----Input----- The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of test cases follows. The first line of each test case contains three integers $n$, $p$, and $k$ ($1 \le p \le n \le 10^5$, $1 \le k \le n$) β€” the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required. The second line of each test case contains a string $a_1 a_2 a_3 \ldots a_n$ ($a_i = 0$ or $a_i = 1$) β€” the initial pattern written without spaces. The last line of each test case contains two integers $x$ and $y$ ($1 \le x, y \le 10^4$) β€” the time required to add a platform and to remove the first cell correspondingly. The sum of $n$ over test cases does not exceed $10^5$. -----Output----- For each test case output a single integer β€” the minimum number of seconds you need to modify the level accordingly. It can be shown that it is always possible to make the level passable. -----Examples----- Input 3 10 3 2 0101010101 2 2 5 4 1 00000 2 10 11 2 3 10110011000 4 3 Output 2 4 10 -----Note----- In the first test case it's best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is $y = 2$. In the second test case it's best to add a platform to both cells $4$ and $5$: 00000 $\to$ 00011. The time required is $x \cdot 2 = 4$. In the third test case it's best to to remove the first cell twice and then add a platform to the cell which was initially $10$-th: 10110011000 $\to$ 10110011010. The time required is $y \cdot 2 + x = 10$.
for _ in range(int(input())): n, p, k = map(int, input().split()) s = input() l = list(s) x, y = map(int, input().split()) dpk = [0] * n for i in range(n - k, n): if l[i] == "0": dpk[i] = x for i in range(n - k - 1, -1, -1): if l[i] == "1": dpk[i] += dpk[i + k] else: dpk[i] += x + dpk[i + k] ans = float("inf") for i in range(p - 1, n): dpk[i] += y * (i - p + 1) ans = min(ans, dpk[i]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You're creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from $1$, and in each cell you can either put a platform or leave it empty. In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell $p$, then bounces off it, then bounces off a platform in the cell $(p + k)$, then a platform in the cell $(p + 2k)$, and so on every $k$-th platform until it goes farther than the last cell. If any of these cells has no platform, you can't pass the level with these $p$ and $k$. You already have some level pattern $a_1$, $a_2$, $a_3$, ..., $a_n$, where $a_i = 0$ means there is no platform in the cell $i$, and $a_i = 1$ means there is one. You want to modify it so that the level can be passed with given $p$ and $k$. In $x$ seconds you can add a platform in some empty cell. In $y$ seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can't do any other operation. You can not reduce the number of cells to less than $p$. Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added. What is the minimum number of seconds you need to make this level passable with given $p$ and $k$? -----Input----- The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of test cases follows. The first line of each test case contains three integers $n$, $p$, and $k$ ($1 \le p \le n \le 10^5$, $1 \le k \le n$) β€” the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required. The second line of each test case contains a string $a_1 a_2 a_3 \ldots a_n$ ($a_i = 0$ or $a_i = 1$) β€” the initial pattern written without spaces. The last line of each test case contains two integers $x$ and $y$ ($1 \le x, y \le 10^4$) β€” the time required to add a platform and to remove the first cell correspondingly. The sum of $n$ over test cases does not exceed $10^5$. -----Output----- For each test case output a single integer β€” the minimum number of seconds you need to modify the level accordingly. It can be shown that it is always possible to make the level passable. -----Examples----- Input 3 10 3 2 0101010101 2 2 5 4 1 00000 2 10 11 2 3 10110011000 4 3 Output 2 4 10 -----Note----- In the first test case it's best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is $y = 2$. In the second test case it's best to add a platform to both cells $4$ and $5$: 00000 $\to$ 00011. The time required is $x \cdot 2 = 4$. In the third test case it's best to to remove the first cell twice and then add a platform to the cell which was initially $10$-th: 10110011000 $\to$ 10110011010. The time required is $y \cdot 2 + x = 10$.
def solve(): n, p, k = map(int, input().split()) blocks = input() x, y = map(int, input().split()) costs = [0] * n for i in range(n - 1, p - 2, -1): if i + k < n: costs[i] = costs[i + k] if blocks[i] == "0": costs[i] += x for i in range(p - 1, n): costs[i] += y * (i - p + 1) print(min(costs[p - 1 :])) def main(): for _ in range(int(input())): solve() main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR STRING VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You're creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from $1$, and in each cell you can either put a platform or leave it empty. In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell $p$, then bounces off it, then bounces off a platform in the cell $(p + k)$, then a platform in the cell $(p + 2k)$, and so on every $k$-th platform until it goes farther than the last cell. If any of these cells has no platform, you can't pass the level with these $p$ and $k$. You already have some level pattern $a_1$, $a_2$, $a_3$, ..., $a_n$, where $a_i = 0$ means there is no platform in the cell $i$, and $a_i = 1$ means there is one. You want to modify it so that the level can be passed with given $p$ and $k$. In $x$ seconds you can add a platform in some empty cell. In $y$ seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can't do any other operation. You can not reduce the number of cells to less than $p$. Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added. What is the minimum number of seconds you need to make this level passable with given $p$ and $k$? -----Input----- The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of test cases follows. The first line of each test case contains three integers $n$, $p$, and $k$ ($1 \le p \le n \le 10^5$, $1 \le k \le n$) β€” the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required. The second line of each test case contains a string $a_1 a_2 a_3 \ldots a_n$ ($a_i = 0$ or $a_i = 1$) β€” the initial pattern written without spaces. The last line of each test case contains two integers $x$ and $y$ ($1 \le x, y \le 10^4$) β€” the time required to add a platform and to remove the first cell correspondingly. The sum of $n$ over test cases does not exceed $10^5$. -----Output----- For each test case output a single integer β€” the minimum number of seconds you need to modify the level accordingly. It can be shown that it is always possible to make the level passable. -----Examples----- Input 3 10 3 2 0101010101 2 2 5 4 1 00000 2 10 11 2 3 10110011000 4 3 Output 2 4 10 -----Note----- In the first test case it's best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is $y = 2$. In the second test case it's best to add a platform to both cells $4$ and $5$: 00000 $\to$ 00011. The time required is $x \cdot 2 = 4$. In the third test case it's best to to remove the first cell twice and then add a platform to the cell which was initially $10$-th: 10110011000 $\to$ 10110011010. The time required is $y \cdot 2 + x = 10$.
try: inp = open("file.txt").readline except: inp = input t = int(inp().strip()) for _ in range(t): n, p, k = map(int, inp().strip().split(" ")) arr = list(inp().strip()) y, x = map(int, inp().strip().split(" ")) kcou = [(0) for i in range(n)] i = 0 for i in range(n - 1, n - k - 1, -1): j = i while j >= 0: if arr[j] != "1": kcou[j] += 1 if j + k < n: kcou[j] += kcou[j + k] j -= k ans = int(1000000000.0) rem = 0 for i in range(p - 1, n): if arr[i] == "1": cur = rem * x if i + k < n: cur += kcou[i + k] * y ans = min(ans, cur) else: cur = rem * x + y if i + k < n: cur += kcou[i + k] * y ans = min(ans, cur) rem += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You're creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from $1$, and in each cell you can either put a platform or leave it empty. In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell $p$, then bounces off it, then bounces off a platform in the cell $(p + k)$, then a platform in the cell $(p + 2k)$, and so on every $k$-th platform until it goes farther than the last cell. If any of these cells has no platform, you can't pass the level with these $p$ and $k$. You already have some level pattern $a_1$, $a_2$, $a_3$, ..., $a_n$, where $a_i = 0$ means there is no platform in the cell $i$, and $a_i = 1$ means there is one. You want to modify it so that the level can be passed with given $p$ and $k$. In $x$ seconds you can add a platform in some empty cell. In $y$ seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can't do any other operation. You can not reduce the number of cells to less than $p$. Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added. What is the minimum number of seconds you need to make this level passable with given $p$ and $k$? -----Input----- The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of test cases follows. The first line of each test case contains three integers $n$, $p$, and $k$ ($1 \le p \le n \le 10^5$, $1 \le k \le n$) β€” the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required. The second line of each test case contains a string $a_1 a_2 a_3 \ldots a_n$ ($a_i = 0$ or $a_i = 1$) β€” the initial pattern written without spaces. The last line of each test case contains two integers $x$ and $y$ ($1 \le x, y \le 10^4$) β€” the time required to add a platform and to remove the first cell correspondingly. The sum of $n$ over test cases does not exceed $10^5$. -----Output----- For each test case output a single integer β€” the minimum number of seconds you need to modify the level accordingly. It can be shown that it is always possible to make the level passable. -----Examples----- Input 3 10 3 2 0101010101 2 2 5 4 1 00000 2 10 11 2 3 10110011000 4 3 Output 2 4 10 -----Note----- In the first test case it's best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is $y = 2$. In the second test case it's best to add a platform to both cells $4$ and $5$: 00000 $\to$ 00011. The time required is $x \cdot 2 = 4$. In the third test case it's best to to remove the first cell twice and then add a platform to the cell which was initially $10$-th: 10110011000 $\to$ 10110011010. The time required is $y \cdot 2 + x = 10$.
t = int(input()) for j in range(t): n, p, k = map(int, input().split()) s = input() s = "~" + s x, y = map(int, input().split()) a = [0] * n b = [0] * n ans = 9999999999 for i in range(p, n + 1): a[(i - p) % k] += s[i] == "0" for i in range(p, n + 1): ans = min(ans, (i - p) * y + (a[(i - p) % k] - b[(i - p) % k]) * x) b[(i - p) % k] += s[i] == "0" print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by a_{i}. Each extension can't be used more than once, the extensions can be used in any order. Now Arkady's field has size h Γ— w. He wants to enlarge it so that it is possible to place a rectangle of size a Γ— b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal. -----Input----- The first line contains five integers a, b, h, w and n (1 ≀ a, b, h, w, n ≀ 100 000)Β β€” the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions. The second line contains n integers a_1, a_2, ..., a_{n} (2 ≀ a_{i} ≀ 100 000), where a_{i} equals the integer a side multiplies by when the i-th extension is applied. -----Output----- Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0. -----Examples----- Input 3 3 2 4 4 2 5 4 10 Output 1 Input 3 3 3 3 5 2 3 5 4 2 Output 0 Input 5 5 1 2 3 2 2 3 Output -1 Input 3 4 1 1 3 2 3 2 Output 3 -----Note----- In the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field.
f = lambda: list(map(int, input().split())) a, b, h, w, n = f() c = sorted(list(f()), key=lambda x: -x) d = {(h, w), (w, h)} for i, q in enumerate([1] + c): for u, v in d.copy(): h, w = u, v * q if a <= w and b <= h or a <= h and b <= w: print(i) return d.add((h, w)) d.add((w, h)) print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by a_{i}. Each extension can't be used more than once, the extensions can be used in any order. Now Arkady's field has size h Γ— w. He wants to enlarge it so that it is possible to place a rectangle of size a Γ— b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal. -----Input----- The first line contains five integers a, b, h, w and n (1 ≀ a, b, h, w, n ≀ 100 000)Β β€” the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions. The second line contains n integers a_1, a_2, ..., a_{n} (2 ≀ a_{i} ≀ 100 000), where a_{i} equals the integer a side multiplies by when the i-th extension is applied. -----Output----- Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0. -----Examples----- Input 3 3 2 4 4 2 5 4 10 Output 1 Input 3 3 3 3 5 2 3 5 4 2 Output 0 Input 5 5 1 2 3 2 2 3 Output -1 Input 3 4 1 1 3 2 3 2 Output 3 -----Note----- In the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field.
def gen(o, gen1): if o == len(my_arr): fp = 1 sp = 1 for i in range(len(my_arr)): fp *= my_arr[i][0] ** gen1[i] sp *= my_arr[i][0] ** (my_arr[i][1] - gen1[i]) if h * fp >= a and w * sp >= b or h * fp >= b and w * sp >= a: return True return False for i in range(my_arr[o][1] + 1): if gen(o + 1, gen1 + [i]): return True return False a, b, h, w, n = map(int, input().split()) arr = list(map(int, input().split())) arr.sort(reverse=True) arr2 = [] for i in range(n): if not i or arr[i] != arr[i - 1]: arr2.append([arr[i], 1]) else: arr2[-1][1] += 1 if h >= a and w >= b or h >= b and w >= a: print(0) else: my_arr = [] j = -1 ans = -1 for i in range(1, min(34, len(arr)) + 1): if my_arr and my_arr[-1] < arr2[j]: my_arr[-1][1] += 1 else: j += 1 my_arr.append([arr2[j][0], 1]) if gen(0, []): ans = i break print(ans)
FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR 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 NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER IF FUNC_CALL VAR NUMBER LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by a_{i}. Each extension can't be used more than once, the extensions can be used in any order. Now Arkady's field has size h Γ— w. He wants to enlarge it so that it is possible to place a rectangle of size a Γ— b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal. -----Input----- The first line contains five integers a, b, h, w and n (1 ≀ a, b, h, w, n ≀ 100 000)Β β€” the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions. The second line contains n integers a_1, a_2, ..., a_{n} (2 ≀ a_{i} ≀ 100 000), where a_{i} equals the integer a side multiplies by when the i-th extension is applied. -----Output----- Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0. -----Examples----- Input 3 3 2 4 4 2 5 4 10 Output 1 Input 3 3 3 3 5 2 3 5 4 2 Output 0 Input 5 5 1 2 3 2 2 3 Output -1 Input 3 4 1 1 3 2 3 2 Output 3 -----Note----- In the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field.
import sys MAXV = 100010 d = [0] * MAXV a, b, h, w, n = list(map(int, input().split())) arr = input().split() for it in range(n): arr[it] = int(arr[it]) def solve(a, b, h, w, z, product, it): k = 0 if a % h: k = a // h + 1 else: k = a // h if k <= z and product // z * w >= b: print(it) return arr = sorted(arr) arr = arr[::-1] d[1] = 1 solve(a, b, h, w, 1, 1, 0) solve(a, b, w, h, 1, 1, 0) product = 1 xxx = 0 for it in range(1, n + 1): product *= arr[it - 1] for j in reversed(list(range(1, MAXV))): if not d[j]: continue x = j * arr[it - 1] if x < MAXV: d[x] = 1 elif xxx: xxx = min(x, xxx) else: xxx = x if xxx: solve(a, b, h, w, xxx, product, it) solve(a, b, w, h, xxx, product, it) for j in range(MAXV): if d[j]: solve(a, b, h, w, j, product, it) solve(a, b, w, h, j, product, it) print(-1)
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by a_{i}. Each extension can't be used more than once, the extensions can be used in any order. Now Arkady's field has size h Γ— w. He wants to enlarge it so that it is possible to place a rectangle of size a Γ— b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal. -----Input----- The first line contains five integers a, b, h, w and n (1 ≀ a, b, h, w, n ≀ 100 000)Β β€” the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions. The second line contains n integers a_1, a_2, ..., a_{n} (2 ≀ a_{i} ≀ 100 000), where a_{i} equals the integer a side multiplies by when the i-th extension is applied. -----Output----- Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0. -----Examples----- Input 3 3 2 4 4 2 5 4 10 Output 1 Input 3 3 3 3 5 2 3 5 4 2 Output 0 Input 5 5 1 2 3 2 2 3 Output -1 Input 3 4 1 1 3 2 3 2 Output 3 -----Note----- In the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field.
a, b, h, w, n = list(map(int, input().strip().split(" "))) if a > b: a, b = b, a factor = list(map(int, input().strip().split(" "))) factor = sorted(factor)[::-1] def findout(a, b, h, w, factor): possible = set() for i in range(len(factor)): temp = set() if i == 0: temp.add((factor[0], 1)) temp.add((1, factor[0])) possible = temp for X in temp: f1, f2 = X if f1 * h >= a and f2 * w >= b: return i + 1 else: for X in possible: c1, c2 = X if c1 * h <= a: temp.add((c1 * factor[i], c2)) if c1 * factor[i] * h >= a and c2 * w >= b: return i + 1 if c2 * w <= b: temp.add((c1, c2 * factor[i])) if c1 * h >= a and c2 * w * factor[i] >= b: return i + 1 possible = temp return 10**9 + 1 if h >= a and w >= b or h >= b and w >= a: print(0) else: ans = min(findout(a, b, h, w, factor), findout(a, b, w, h, factor)) if ans != 10**9 + 1: print(ans) else: print(-1)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by a_{i}. Each extension can't be used more than once, the extensions can be used in any order. Now Arkady's field has size h Γ— w. He wants to enlarge it so that it is possible to place a rectangle of size a Γ— b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal. -----Input----- The first line contains five integers a, b, h, w and n (1 ≀ a, b, h, w, n ≀ 100 000)Β β€” the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions. The second line contains n integers a_1, a_2, ..., a_{n} (2 ≀ a_{i} ≀ 100 000), where a_{i} equals the integer a side multiplies by when the i-th extension is applied. -----Output----- Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0. -----Examples----- Input 3 3 2 4 4 2 5 4 10 Output 1 Input 3 3 3 3 5 2 3 5 4 2 Output 0 Input 5 5 1 2 3 2 2 3 Output -1 Input 3 4 1 1 3 2 3 2 Output 3 -----Note----- In the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field.
def isin(a, b, h, w): return h >= a and w >= b or h >= b and w >= a a, b, h, w, n = map(int, input().split()) c = sorted(list(map(int, input().split())), key=lambda x: -x) if isin(a, b, h, w): print(0) exit() vis = {h: w} for i in range(len(c)): nc = c[i] pairs = [] for l in vis.keys(): pair = l, vis[l] * nc if isin(a, b, pair[0], pair[1]): print(i + 1) exit() pairs.append(pair) if nc * l not in vis or vis[l] > vis[nc * l]: pair = nc * l, vis[l] if isin(a, b, pair[0], pair[1]): print(i + 1) exit() pairs.append(pair) for p in pairs: vis[p[0]] = p[1] print(-1)
FUNC_DEF RETURN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys from sys import stdin ty = ["abc", "acb", "bac", "bca", "cab", "cba"] n, m = map(int, stdin.readline().split()) s = list(stdin.readline()[:-1]) dp = [([0] * (n + 1)) for i in range(6)] for c in range(6): for i in range(n): if s[i] != ty[c][i % 3]: dp[c][i + 1] = dp[c][i] + 1 else: dp[c][i + 1] = dp[c][i] ANS = [] for loop in range(m): l, r = map(int, stdin.readline().split()) ans = float("inf") for c in range(6): ans = min(ans, dp[c][r] - dp[c][l - 1]) ANS.append(str(ans)) print("\n".join(ANS))
IMPORT ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR 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 STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = map(int, input().split()) s = input() ss = ["abc", "acb", "cba", "cab", "bac", "bca"] dps = [] for i in range(6): l = [0] x = ss[i] y = x * (n // 3) + x[0 : n % 3] for c in range(n): l.append(l[-1] + (s[c] != y[c])) dps.append(l) for _ in range(m): a, b = map(int, input().split()) ans = 1000000000.0 for i in range(6): ans = min(dps[i][b] - dps[i][a - 1], ans) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys MOD = 3 def get_next(start, delta): return (start + delta) % MOD def get_state(arr, start, dir): n = len(arr) state = [] prev = 0 cur = start for a in arr: state.append(prev + int(a != cur)) prev = state[-1] cur = get_next(cur, dir) return state def preprocess(arr): arr = [(ord(a) - ord("a")) for a in arr] states = { (start, dir): ([0] + get_state(arr, start, dir)) for start in range(MOD) for dir in [-1, 1] } return states def solve(states, l, r): results = [] for (start, dir), state in states.items(): results.append(state[r] - state[l - 1]) return min(results) def main(): outputs = [] fin = sys.stdin n, m = map(int, fin.readline().split()) arr = list(fin.readline().strip()) states = preprocess(arr) requests = [] for _ in range(m): l, r = map(int, fin.readline().split()) outputs.append(solve(states, l, r)) print("\n".join(map(str, outputs))) main()
IMPORT ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR LIST NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import itertools from sys import stdin def input(): return next(stdin)[:-1] def readline(): return map(int, input().split()) n, m = readline() s = input() periods = list(itertools.permutations("abc")) c = {p: [0] for p in periods} for i, ch in enumerate(s): for p in periods: c[p].append(c[p][-1] + (ch != p[i % 3])) for __ in range(m): l, r = readline() print(min(c[p][r] - c[p][l - 1] for p in periods))
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR VAR LIST NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
n, m = [int(x) for x in input().split(" ")] s = input() per = ["abc", "cba", "cab", "bca", "acb", "bac"] dif = [] for j in range(len(per)): tmp = [int(per[j][0] != s[0])] for i in range(1, n, 1): tmp.append(tmp[-1] + (per[j][i % 3] != s[i])) dif.append(tmp) res = "" for _ in range(m): l, r = [int(x) for x in input().split(" ")] l -= 1 r -= 1 if l == 0: res += str(min(*list(map(lambda x: x[r], dif)))) + "\n" else: res += str(min(*list(map(lambda x: x[r] - x[l - 1], dif)))) + "\n" print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys from itertools import permutations input = sys.stdin.readline n, m = map(int, input().split()) s = input()[:-1] k = (n + 2) // 3 A = [] for cand in permutations("abc"): x = "".join(cand) t = (x * k)[:n] cur = [0] for a, b in zip(s, t): cur.append(cur[-1] + (a != b)) A.append(cur) for _ in range(m): a, b = map(int, input().split()) ans = float("inf") for acc in A: cur = acc[b] - acc[a - 1] ans = min(ans, cur) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
def read_ints(): return [int(x) for x in input().split()] forms = ["abc", "acb", "bac", "bca", "cab", "cba"] n, m = read_ints() s = input() k = 6 ans = [([0] * n) for _ in range(k)] for j in range(k): for i in range(n): ans[j][i] = 0 if s[i] == forms[j][i % 3] else 1 ans[j][i] += 0 if i == 0 else ans[j][i - 1] jwbs = [] for i in range(m): l, r = read_ints() l -= 1 r -= 1 jwb = n for j in range(k): prev = 0 if l == 0 else ans[j][l - 1] jwb = min(jwb, ans[j][r] - prev) jwbs.append(str(jwb)) print("\n".join(jwbs))
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline def process(S, Q): diffs = {"a": [[0], [0]], "b": [[0], [0]], "c": [[0], [0]]} should_dict = {} for first in "abc": for diff in [1, 2]: for i in range(3): T = first, diff, i should = "abc"[(ord(first) - ord("a") + diff * i) % 3] should_dict[T] = should n = len(S) for i in range(n): c = S[i] for first in "abc": for diff in [1, 2]: should = should_dict[first, diff, i % 3] X = diffs[first][diff - 1][-1] if c != should: diffs[first][diff - 1].append(X + 1) else: diffs[first][diff - 1].append(X) d = {} final = [] for l, r in Q: if (l, r) in d: final.append(d[l, r]) else: answer = float("inf") for first in "abc": for diff in [1, 2]: L = diffs[first][diff - 1] D1 = L[r] D2 = L[l - 1] answer = min(answer, D1 - D2) d[l, r] = answer final.append(answer) return final n, m = [int(x) for x in input().split()] S = input() Q = [] for i in range(m): l, r = [int(x) for x in input().split()] Q.append([l, r]) answer = process(S, Q) for x in answer: sys.stdout.write(str(x) + "\n")
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR DICT STRING STRING STRING LIST LIST NUMBER LIST NUMBER LIST LIST NUMBER LIST NUMBER LIST LIST NUMBER LIST NUMBER ASSIGN VAR DICT FOR VAR STRING FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR STRING BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR STRING FOR VAR LIST NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR STRING FOR VAR LIST NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys from itertools import permutations LI = lambda: list(map(int, sys.stdin.readline().split())) MI = lambda: map(int, sys.stdin.readline().split()) SI = lambda: sys.stdin.readline().strip("\n") II = lambda: int(sys.stdin.readline()) n, m = MI() s = SI() a = "abc" dp = {} p = [] for v in permutations(a): v = "".join(v) p.append(v) dp[v] = [0] * n x = v * (n // 3 + (n % 3 == 0) + 1) for i in range(n): dp[v][i] += s[i] != x[i] for i in range(1, n): dp[v][i] += dp[v][i - 1] for _ in range(m): a, b = MI() print(min(dp[v][b - 1] - (dp[v][a - 2] if a > 1 else 0) for v in p))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
n, m = map(int, input().split()) s = list(input()) a = [0] * (n + 1) b = [0] * (n + 1) c = [0] * (n + 1) d = [0] * (n + 1) e = [0] * (n + 1) f = [0] * (n + 1) pa, pb, pc, pd, pe, pf = "abc", "bca", "cab", "acb", "cba", "bac" for i in range(n): if s[i] != pa[i % 3]: a[i] += 1 if s[i] != pb[i % 3]: b[i] += 1 if s[i] != pc[i % 3]: c[i] += 1 if s[i] != pd[i % 3]: d[i] += 1 if s[i] != pe[i % 3]: e[i] += 1 if s[i] != pf[i % 3]: f[i] += 1 for i in range(1, n): a[i] += a[i - 1] b[i] += b[i - 1] c[i] += c[i - 1] d[i] += d[i - 1] e[i] += e[i - 1] f[i] += f[i - 1] ans = [] for _ in range(m): L, R = map(lambda z: int(z) - 1, input().split()) ans.append( min( a[R] - a[L - 1], b[R] - b[L - 1], c[R] - c[L - 1], d[R] - d[L - 1], e[R] - e[L - 1], f[R] - f[L - 1], ) ) print("\n".join(map(str, ans)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = map(int, input().split()) s = list(input())[:-1] for i in range(n): s[i] = ord(s[i]) - 97 x = [(i % 3) for i in range(n)] y = [((i + 1) % 3) for i in range(n)] z = [((i + 2) % 3) for i in range(n)] a = [(i % 3) for i in range(n, 0, -1)] b = [((i + 1) % 3) for i in range(n, 0, -1)] c = [((i + 2) % 3) for i in range(n, 0, -1)] sx = [0] * (n + 1) sy = [0] * (n + 1) sz = [0] * (n + 1) sa = [0] * (n + 1) sb = [0] * (n + 1) sc = [0] * (n + 1) for i in range(n): sx[i] = sx[i - 1] if x[i] != s[i]: sx[i] += 1 sy[i] = sy[i - 1] if y[i] != s[i]: sy[i] += 1 sz[i] = sz[i - 1] if z[i] != s[i]: sz[i] += 1 sa[i] = sa[i - 1] if a[i] != s[i]: sa[i] += 1 sb[i] = sb[i - 1] if b[i] != s[i]: sb[i] += 1 sc[i] = sc[i - 1] if c[i] != s[i]: sc[i] += 1 def get(l, r): res = 10**18 res = min(res, sx[r] - sx[l - 1]) res = min(res, sy[r] - sy[l - 1]) res = min(res, sz[r] - sz[l - 1]) res = min(res, sa[r] - sa[l - 1]) res = min(res, sb[r] - sb[l - 1]) res = min(res, sc[r] - sc[l - 1]) return res for _ in range(m): l, r = map(int, input().split()) l -= 1 r -= 1 print(get(l, r))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
from sys import * n, m = map(int, stdin.readline().split()) s = stdin.readline() perm = ["abc", "acb", "bac", "bca", "cab", "cba"] a = [[], [], [], [], [], []] for i in range(6): for j in range(n): if s[j] != perm[i][j % 3]: if j == 0: a[i].append(1) else: a[i].append(a[i][-1] + 1) elif j == 0: a[i].append(0) else: a[i].append(a[i][-1]) for i in range(m): l, r = map(int, stdin.readline().split()) l -= 1 r -= 1 if l == 0: ans = r - l + 1 for j in range(6): ans = min(ans, a[j][r]) else: ans = r - l + 1 for j in range(6): ans = min(ans, a[j][r] - a[j][l - 1]) stdout.write(str(ans) + "\n")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST LIST LIST LIST LIST LIST LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
from sys import stdin input = stdin.readline cycles = ["abc", "acb", "bac", "bca", "cab", "cba"] n, m = [int(x) for x in input().split()] s = input().rstrip() prefix_changes = [[0] for _ in range(6)] for i in range(n): k = i % 3 for j in range(6): if s[i] != cycles[j][k]: prefix_changes[j].append(prefix_changes[j][-1] + 1) else: prefix_changes[j].append(prefix_changes[j][-1]) for _ in range(m): l, r = [int(x) for x in input().split()] ans = float("inf") for i in range(6): x = prefix_changes[i][r] - prefix_changes[i][l - 1] ans = min(ans, x) print(ans)
ASSIGN VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = map(int, input().split()) S = input().strip() DP = [([0] * (n + 5)) for i in range(6)] SX = ["abc", "acb", "bac", "bca", "cab", "cba"] for i in range(n): for j in range(6): if SX[j][i % 3] == S[i]: DP[j][i] = DP[j][i - 1] else: DP[j][i] = DP[j][i - 1] + 1 for i in range(m): l, r = map(int, input().split()) l -= 1 r -= 1 ANS = 1 << 30 for j in range(6): ANS = min(ANS, DP[j][r] - DP[j][l - 1]) print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
length, count = map(int, input().split()) string = input() pref = [] for s in ["abc", "acb", "bca", "cab", "cba", "bac"]: pref_sum = [0] * (length + 1) for i in range(length): pref_sum[i + 1] = pref_sum[i] + int(string[i] != s[i % 3]) pref.append(tuple(pref_sum)) ans = [0] * count for i in range(count): a, b = map(int, input().split()) ans[i] = str(min([(el[b] - el[a - 1]) for el in pref])) print("\n".join(ans))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = lambda: sys.stdin.readline().rstrip() sys.setrecursionlimit(2 * 10**5 + 10) write = lambda x: sys.stdout.write(x + "\n") debug = lambda x: sys.stderr.write(x + "\n") writef = lambda x: print("{:.12f}".format(x)) n, m = list(map(int, input().split())) vs = [([0] * n) for _ in range(3)] i = 0 for c in input(): v = ord(c) - ord("a") for vv in range(3): if vv == v: continue vs[vv][i] += 1 i += 1 for j in range(3): for i in range(3, n): vs[j][i] = vs[j][i - 3] + vs[j][i] ans = [] inf = 10**12 def _sub(l, r, v): if l > r: return 0 li = vs[v] rr = l + (r - l) // 3 * 3 return li[rr] - (li[l - 3] if l >= 3 else 0) def sub(l, r): res = inf for v0 in range(3): for v1 in range(3): if v0 == v1: continue v2 = 3 ^ v0 ^ v1 res = min(res, _sub(l, r, v0) + _sub(l + 1, r, v1) + _sub(l + 2, r, v2)) return res for i in range(m): l, r = map(int, input().split()) l -= 1 r -= 1 val = sub(l, r) ans.append(val) write("\n".join(map(str, ans)))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys from itertools import * input = sys.stdin.readline n, m = map(int, input().split()) s = input() ps = [ list(accumulate((c1 != c2 for c1, c2 in zip(cycle(pm), s)), initial=0)) for pm in permutations("abc") ] for _ in range(m): l, r = map(int, input().split()) print(min(p[r] - p[l - 1] for p in ps))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
n, m = map(int, input().split()) s = list(input()) lr = [[int(i) for i in input().split()] for _ in range(m)] li = [([0] * n) for _ in range(6)] ss = [["a", "b", "c"].index(c) for c in s] c0 = 0 c1 = 1 c2 = 2 for i in range(n): d0 = (c0 + i) % 3 d1 = (c1 + i) % 3 d2 = (c2 + i) % 3 d3 = (c0 - i) % 3 d4 = (c1 - i) % 3 d5 = (c2 - i) % 3 if ss[i] != d0: li[0][i] = 1 if ss[i] != d1: li[1][i] = 1 if ss[i] != d2: li[2][i] = 1 if ss[i] != d3: li[3][i] = 1 if ss[i] != d4: li[4][i] = 1 if ss[i] != d5: li[5][i] = 1 for ind in range(6): for i in range(1, n): li[ind][i] += li[ind][i - 1] for l, r in lr: l -= 1 r -= 1 print(min([(li[ind][r] - (li[ind][l - 1] if l > 0 else 0)) for ind in range(6)]))
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 VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL LIST STRING STRING STRING VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = map(int, input().split()) s = input().rstrip() data = ["abc", "acb", "bac", "bca", "cab", "cba"] cache = [[0] for _ in range(6)] k = n // 3 + 1 for i in range(6): comp = data[i] * k summ = 0 for j in range(n): if s[j] != comp[j]: summ += 1 cache[i].append(summ) for _ in range(m): a, b = map(int, input().split()) ret = int(1000000000.0) for i in range(6): ret = min(ret, cache[i][b] - cache[i][a - 1]) print(ret)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import itertools import sys input = sys.stdin.readline def main(): n, m = map(int, input().split()) s = str(input()) alphabet = ["a", "b", "c"] kouho = [] for L in itertools.permutations(alphabet, 3): temp = [0] for i in range(n): plus = 0 if s[i] != L[i % 3]: plus += 1 temp.append(temp[-1] + plus) kouho.append(temp) for i in range(m): l, r = map(int, input().split()) l -= 1 ans = pow(10, 6) for j in range(6): temp = kouho[j][r] - kouho[j][l] ans = min(ans, temp) print(ans) main()
IMPORT IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys from itertools import permutations inp = sys.stdin.readline n, m = map(int, inp().split()) s = inp() li = [] for p in permutations("abc"): li.append(0) count = 0 for i in range(n): if s[i] != p[i % 3]: count += 1 li.append(count) for _ in range(m): l, r = map(int, inp().split()) ans = 1000000000.0 for i in range(6): ans = min(li[(n + 1) * i + r] - li[(n + 1) * i + l - 1], ans) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER 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 ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import itertools DEBUG = 0 if DEBUG == 1: data = open("sample1.in", "r") def readline(): if DEBUG == 1: return data.readline().strip("\r\n") else: try: return input().strip("\r\n") except EOFError: return "" def readi(): return [int(t) for t in readline().split()] n, m = readi() s = readline() summ = [([0] * n) for i in range(6)] k = 0 for i in itertools.permutations("abc", 3): t = "".join(i) for i in range(n): if s[i] != t[i % 3]: summ[k][i] = 1 if i > 0: summ[k][i] += summ[k][i - 1] k += 1 ans = [200000] * m for i in range(m): l, r = readi() l -= 1 r -= 1 for j in range(6): if l - 1 >= 0: ans[i] = min(ans[i], summ[j][r] - summ[j][l - 1]) else: ans[i] = min(ans[i], summ[j][r]) print(*ans, sep="\n") if DEBUG == 1: data.close()
IMPORT ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL FUNC_CALL VAR STRING RETURN FUNC_CALL FUNC_CALL VAR STRING VAR RETURN STRING FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline t = 1 for j in range(t): n, m = map(int, input().split()) s = list(input().strip()) temp = ["abc", "acb", "bac", "bca", "cab", "cba"] dp = [[(0) for x in range(n + 1)] for y in range(6)] for x in range(6): for y in range(n): dp[x][y + 1] = dp[x][y] if s[y] != temp[x][y % 3]: dp[x][y + 1] += 1 for i in range(m): l, r = map(int, input().split()) ans = dp[0][r] - dp[0][l - 1] for x in range(5): ans = min(ans, dp[x + 1][r] - dp[x + 1][l - 1]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline def getP(j): if j >= 3: l = j - 3 return 3 + (l + 1) % 3 else: return (j + 1) % 3 def solve(st, n, m): pref = [([0] * (n + 1)) for i in range(6)] match = "abcacb" for j in range(n - 1, -1, -1): for i in range(6): pref[i][j] = 1 + pref[getP(i)][j + 1] if match[i] == st[j]: pref[i][j] -= 1 def get(ind, l, r): ln = r - l + 1 p = None if ind >= 3: p = 3 + (ind + ln) % 3 else: p = (ind + ln) % 3 change = pref[ind][l - 1] - pref[p][r] return change for q in range(m): l, r = map(int, input().split()) ans = float("inf") for i in range(6): ans = min(ans, get(i, l, r)) print(ans) n, m = map(int, input().split()) st = input().rstrip() solve(st, n, m)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NONE IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR 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 VAR VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin output = sys.stdout inputs = input.readline().strip().split() N = int(inputs[0]) M = int(inputs[1]) S = input.readline().strip() SL = ["abc", "acb", "bac", "bca", "cab", "cba"] CL = [([0] * (N + 1)) for x in range(6)] for i in range(6): subtotal = 0 substring = SL[i] subCL = CL[i] for j in range(N): if S[j] != substring[j % 3]: subtotal += 1 subCL[j + 1] = subtotal for i in range(M): y = input.readline().strip().split() start = int(y[0]) end = int(y[1]) total = N for j in range(6): a = CL[j][end] - CL[j][start - 1] if a < total: total = a print(total)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = map(int, input().split()) s = input()[:-1] L = [] for si in ["a", "b", "c"]: for sj in ["a", "b", "c"]: for sk in ["a", "b", "c"]: if si != sj and sj != sk and sk != si: l = [] for i in range(n): if i % 3 == 0: l.append(si) elif i % 3 == 1: l.append(sj) else: l.append(sk) L.append(l) acc = [[0] for _ in range(6)] for i in range(n): for j in range(6): acc[j].append(acc[j][-1] + (1 if s[i] != L[j][i] else 0)) for _ in range(m): l, r = map(int, input().split()) ans = n for i in range(6): ans = min(ans, acc[i][r] - acc[i][l - 1]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR LIST STRING STRING STRING FOR VAR LIST STRING STRING STRING FOR VAR LIST STRING STRING STRING IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
n, m = map(int, input().split()) s = input() s0 = "abc" s1 = "bca" s2 = "cab" s3 = "cba" s4 = "acb" s5 = "bac" S = [s0, s1, s2, s3, s4, s5] pre = [[0], [0], [0], [0], [0], [0]] for i in range(n): for j in range(6): pre[j].append(pre[j][-1] + int(S[j][i % 3] != s[i])) qry = [list(map(int, input().split())) for i in range(m)] for i in range(m): l, r = qry[i] ans = 100000000 for i in range(6): ans = min(pre[i][r] - pre[i][l - 1], ans) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST LIST NUMBER LIST NUMBER LIST NUMBER LIST NUMBER LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = map(int, input().split()) s = input().strip("\n") k = n // 3 + n % 3 ligma = ["abc" * k, "acb" * k, "bac" * k, "bca" * k, "cba" * k, "cab" * k] psa = [[(0) for i in range(n + 1)] for j in range(6)] for i in range(6): for j in range(1, n + 1): psa[i][j] = psa[i][j - 1] + (ligma[i][j - 1] != s[j - 1]) for i in range(m): l, r = map(int, input().split()) ans = float("inf") for x in range(6): ans = min(ans, psa[x][r] - psa[x][l - 1]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline def give(l, n, s): pr = [0] k = 0 for i in range(n): if l[i] != s[k]: pr.append(pr[-1] + 1) else: pr.append(pr[-1]) k = (k + 1) % 3 return pr n, m = map(int, input().split()) l = input() s1 = "bac" pr1 = give(l, n, s1) s2 = "bca" pr2 = give(l, n, s2) s3 = "acb" pr3 = give(l, n, s3) s4 = "abc" pr4 = give(l, n, s4) s5 = "cab" pr5 = give(l, n, s5) s6 = "cba" pr6 = give(l, n, s6) res = [] for i in range(m): l, r = map(int, input().split()) res.append( min( pr1[r] - pr1[l - 1], pr2[r] - pr2[l - 1], pr3[r] - pr3[l - 1], pr4[r] - pr4[l - 1], pr5[r] - pr5[l - 1], pr6[r] - pr6[l - 1], ) ) print(*res)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
from sys import stdin input = stdin.readline def f(s, q): perm = ["abc", "bca", "acb", "bac", "cab", "cba"] res = [(i[1] - i[0] + 1) for i in q] for word in perm: pref = [0] * (len(s) + 1) for i in range(len(s)): pref[i + 1] = s[i] != word[i % 3] for i in range(1, len(pref)): pref[i] += pref[i - 1] for id, (l, r) in enumerate(q): res[id] = min(res[id], pref[r] - pref[l - 1]) return res n, m = map(int, input().strip().split()) s = input().strip() q = [] for i in range(m): l, r = map(int, input().strip().split()) q.append((l, r)) print(*f(s, q), sep="\n")
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip("\n") def lis(): return list(map(int, input().split())) def ma(): return map(int, input().split()) t = 1 gl = {"a": 0, "b": 1, "c": 2} while t: t -= 1 n, q = ma() s = st() first = [[0, 0, 0]] second = [[0, 0, 0]] third = [[0, 0, 0]] for i in range(len(s)): if i % 3 == 0: x = first[-1][:] if s[i] == "a": first.append([x[0] + 1, x[1], x[2]]) elif s[i] == "b": first.append([x[0], x[1] + 1, x[2]]) else: first.append([x[0], x[1], x[2] + 1]) second.append(second[-1]) third.append(third[-1]) elif i % 3 == 1: x = second[-1][:] if s[i] == "a": second.append([x[0] + 1, x[1], x[2]]) elif s[i] == "b": second.append([x[0], x[1] + 1, x[2]]) else: second.append([x[0], x[1], x[2] + 1]) first.append(first[-1]) third.append(third[-1]) else: x = third[-1][:] if s[i] == "a": third.append([x[0] + 1, x[1], x[2]]) elif s[i] == "b": third.append([x[0], x[1] + 1, x[2]]) else: third.append([x[0], x[1], x[2] + 1]) first.append(first[-1]) second.append(second[-1]) for i in range(q): l, r = ma() res = float("inf") a = first[r][:] b = first[l - 1][:] f1 = [a[0] - b[0], a[1] - b[1], a[2] - b[2]] a = second[r][:] b = second[l - 1][:] f2 = [a[0] - b[0], a[1] - b[1], a[2] - b[2]] a = third[r][:] b = third[l - 1][:] f3 = [a[0] - b[0], a[1] - b[1], a[2] - b[2]] res = min( res, f1[1] + f1[2] + f2[0] + f2[2] + f3[0] + f3[1], f1[1] + f1[2] + f2[0] + f2[1] + f3[0] + f3[2], f1[0] + f1[2] + f2[1] + f2[2] + f3[0] + f3[1], f1[0] + f1[2] + f2[0] + f2[1] + f3[1] + f3[2], f1[0] + f1[1] + f2[1] + f2[2] + f3[0] + f3[2], f1[0] + f1[1] + f2[0] + f2[2] + f3[1] + f3[2], ) print(res)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline def find(n, s, arr): j = 0 ans = [0] * (n + 1) for i in range(n): if s[i] != arr[j]: ans[i + 1] = 1 ans[i + 1] = ans[i] + ans[i + 1] j = (j + 1) % 3 return ans n, m = map(int, input().split()) s = list(input()) A = [ ["a", "b", "c"], ["a", "c", "b"], ["b", "a", "c"], ["b", "c", "a"], ["c", "a", "b"], ["c", "b", "a"], ] val = [] for arr in A: B = find(n, s, arr) val.append(B.copy()) for i in range(m): l, r = map(int, input().split()) ans = r - l + 1 for i in range(6): ans = min(ans, val[i][r] - val[i][l - 1]) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR 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 BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = map(int, input().split()) s = input().strip() A = [([0] * n) for _ in range(6)] B = ["abc", "acb", "bac", "bca", "cba", "cab"] for i in range(6): pat = B[i] for k in range(n): if s[k] != pat[k % 3]: A[i][k] += 1 if k >= 1: A[i][k] += A[i][k - 1] for _ in range(m): l, r = map(int, input().split()) l -= 1 r -= 1 print(min([(A[i][r] - (A[i][l - 1] if l >= 1 else 0)) for i in range(6)]))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = map(int, input().split()) s = input() a = ["abc", "acb", "bac", "bca", "cab", "cba"] for i in range(6): a[i] = a[i] * (n // 3) + a[i] * (n % 3) prefix = [[(0) for i in range(n)] for j in range(6)] for i in range(6): if a[i][0] != s[0]: prefix[i][0] = 1 for i in range(6): for j in range(1, n): if s[j] != a[i][j]: prefix[i][j] = prefix[i][j - 1] + 1 else: prefix[i][j] = prefix[i][j - 1] for i in range(m): l, r = map(int, input().split()) minn = 10**18 for i in range(6): if l != 1: minn = min(minn, prefix[i][r - 1] - prefix[i][l - 2]) else: minn = min(minn, prefix[i][r - 1]) print(minn)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = [int(c) for c in input().split()] s = input() arr = ["abc", "bca", "cab", "bac", "acb", "cba"] a = [[(0) for i in range(n + 1)] for i in range(6)] for i in range(6): ss = arr[i] * (n // 3 + 1) for j in range(n): a[i][j] = 1 if ss[j] != s[j] else 0 a[i][j] += a[i][j - 1] for _ in range(m): l, r = [int(c) for c in input().split()] l -= 1 r -= 1 ans = float("inf") for i in range(6): ans = min(ans, a[i][r] - a[i][l - 1]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
def main(n, m, s, query): permutation = ["abc", "acb", "bac", "bca", "cab", "cba"] dp = [([0] * len(permutation)) for _ in range(n + 1)] for i in range(1, n + 1): for j in range(len(permutation)): req = permutation[j][(i - 1) % 3] dp[i][j] = dp[i - 1][j] + (0 if s[i - 1] == req else 1) ans = [] for l, r in query: ans.append(min(dp[r][i] - dp[l - 1][i] for i in range(len(permutation)))) return ans t = 1 ans = [] for i in range(t): inp = input().split(" ") n, m = int(inp[0]), int(inp[1]) s = input() query = [] for q in range(m): inp = input().split(" ") l, r = int(inp[0]), int(inp[1]) query.append((l, r)) ans = main(n, m, s, query) print("\n".join(str(i) for i in ans))
FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys from itertools import permutations def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int, sys.stdin.readline().rstrip().split()) def LI(): return list(map(int, sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int, sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) n, m = MI() s = S() A = [([0] * 6) for _ in range(n + 1)] X = list(permutations(["a", "b", "c"], 3)) for j in range(6): a, b, c = X[j] for i in range(1, n + 1): A[i][j] = A[i - 1][j] char = s[i - 1] if i % 3 == 0 and char != a: A[i][j] += 1 elif i % 3 == 1 and char != b: A[i][j] += 1 elif i % 3 == 2 and char != c: A[i][j] += 1 for _ in range(m): l, r = MI() ans = 10**18 for j in range(6): ans = min(ans, A[r][j] - A[l - 1][j]) print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST STRING STRING STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys from itertools import accumulate def rall(): return sys.stdin.readlines() def rl(): return sys.stdin.readline().strip() def rl_types(types): str_list = [x for x in sys.stdin.readline().strip().split(" ")] return [types[i](str_list[i]) for i in range(len(str_list))] def pr(something): sys.stdout.write(str(something) + "\n") def pra(array): sys.stdout.write(" ".join([str(x) for x in array]) + "\n") def solve(array): return array n, m = [int(x) for x in rl().split(" ")] s = rl() VALID_SHORTS = set( [ "a", "b", "c", "ab", "ac", "ba", "bc", "ca", "cb", "abc", "bca", "cab", "acb", "bac", "cba", ] ) COSTS_SHORTS = {"aa": 1, "bb": 1, "cc": 1, "aaa": 2, "bbb": 2, "ccc": 2} SEQS = ["abc", "bca", "cab", "acb", "bac", "cba"] CONFLICTS = {} for seq in SEQS: conflict = [(0 if c == seq[i % 3] else 1) for i, c in enumerate(s)] CONFLICTS[seq] = list(accumulate([0, *conflict])) for _ in range(m): l, r = map(int, rl().split(" ")) nchar = r - l + 1 if nchar <= 3: subs = s[l - 1 : r] if subs in VALID_SHORTS: cost = 0 elif subs in COSTS_SHORTS: cost = nchar - 1 else: cost = 1 else: cost = min([(CONFLICTS[seq][r] - CONFLICTS[seq][l - 1]) for seq in SEQS]) pr(cost)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING FUNC_DEF RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
from itertools import accumulate from sys import stdin input = stdin.readline n, m = map(int, input().split()) s = list(input().rstrip()) test1 = "abc" * n test2 = "acb" * n test3 = "bac" * n test4 = "bca" * n test5 = "cab" * n test6 = "cba" * n test = [test1, test2, test3, test4, test5, test6] raw = [[(-1) for x in range(n)] for y in range(6)] pref = [[(-1) for x in range(n)] for y in range(6)] for j in range(6): raw[j] = [(0 if test[j][i] == s[i] else 1) for i in range(n)] for i in range(6): pref[i] = list(accumulate(raw[i])) for _ in range(m): l, r = map(int, input().split()) ans = min( pref[0][r - 1] - pref[0][l - 1] + raw[0][l - 1], pref[1][r - 1] - pref[1][l - 1] + raw[1][l - 1], pref[2][r - 1] - pref[2][l - 1] + raw[2][l - 1], pref[3][r - 1] - pref[3][l - 1] + raw[3][l - 1], pref[4][r - 1] - pref[4][l - 1] + raw[4][l - 1], pref[5][r - 1] - pref[5][l - 1] + raw[5][l - 1], ) print(ans)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import itertools import sys def input(): return sys.stdin.readline().rstrip() DXY = [(0, -1), (1, 0), (0, 1), (-1, 0)] def to_int(char): return ord(char) - ord("a") def main(): n, q = map(int, input().split()) s = input() a = [to_int(char) for char in s] Querys = [tuple(map(int, input().split())) for i in range(q)] perms = list(itertools.permutations((0, 1, 2))) mods = [([0] * (n + 1)) for i in range(6)] for i in range(1, n + 1): for mod in range(6): mods[mod][i] = mods[mod][i - 1] + (perms[mod][(i - 1) % 3] != a[i - 1]) for l, r in Querys: ans = min(mods[mod][r] - mods[mod][l - 1] for mod in range(6)) print(ans) return 0 main()
IMPORT IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.buffer.readline n, m = map(int, input().split()) sst = [0] + list(input().strip()) st = [] for i in sst: st.append(chr(i)) arr = ["abc", "acb", "bac", "bca", "cab", "cba"] dp = [[(0) for i in range(n + 2)] for j in range(6)] for i in range(6): for j in range(1, n + 1): if st[j] != arr[i][(j - 1) % 3]: dp[i][j] = dp[i][j - 1] + 1 continue dp[i][j] = dp[i][j - 1] for i in range(m): l, r = map(int, input().split()) mini = float("inf") for j in range(6): mini = min(mini, dp[j][r] - dp[j][l - 1]) print(mini)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys from itertools import permutations input = sys.stdin.readline n, m = list(map(int, input().split())) s = list(input().strip()) p = list(permutations("abc")) all_s = [] for sx in p: temp = [] all_s.append([sx[x % 3] for x in range(n)]) dp = {i: ([0] * n) for i in range(6)} s_dp = {i: ([0] * n) for i in range(6)} for i in range(0, 6): for j in range(0, n): if all_s[i][j] != s[j]: dp[i][j] = 1 if j == 0: s_dp[i][j] = dp[i][j] else: s_dp[i][j] = dp[i][j] + s_dp[i][j - 1] for _ in range(m): u, v = list(map(int, input().split())) u -= 1 v -= 1 ans = 10000000000.0 for i in range(0, 6): if u == 0: ans = min(ans, s_dp[i][v]) else: ans = min(ans, s_dp[i][v] - s_dp[i][u - 1]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = map(int, input().split()) s = input() abc = [] for i in range(6): abc.append([0] * (n + 1)) words = ["abc", "acb", "bac", "bca", "cab", "cba"] tmps = [0] * 6 for i in range(n): for j in range(6): if words[j][i % 3] != s[i]: tmps[j] += 1 abc[j][i + 1] = tmps[j] for i in range(m): l, r = map(int, input().split()) ans = r - l + 1 for j in range(6): ans = min(ans, abc[j][r] - abc[j][l - 1]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = map(int, input().split()) s = input().rstrip() ss = [ ("abc" * n)[:n], ("acb" * n)[:n], ("bac" * n)[:n], ("bca" * n)[:n], ("cab" * n)[:n], ("cba" * n)[:n], ] c = [([0] * (n + 1)) for i in range(6)] for i in range(6): si = ss[i] for j in range(n): if s[j] != si[j]: c[i][j + 1] += 1 for j in range(n): c[i][j + 1] += c[i][j] for _ in range(m): nl, nr = map(int, input().split()) ans = 10**18 for i in range(6): d = c[i][nr] - c[i][nl - 1] ans = min(ans, d) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP STRING VAR VAR BIN_OP STRING VAR VAR BIN_OP STRING VAR VAR BIN_OP STRING VAR VAR BIN_OP STRING VAR VAR BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline N, Q = map(int, input().split()) S = input() T = ["abc", "bca", "cab", "cba", "bac", "acb"] Cum = [([0] * (N + 1)) for _ in range(6)] for i in range(6): for j in range(N): if T[i][j % 3] != S[j]: Cum[i][j + 1] += 1 for j in range(N): Cum[i][j + 1] += Cum[i][j] for _ in range(Q): L, R = map(int, input().split()) L -= 1 Ans = 1001001001 for i in range(6): Value = Cum[i][R] - Cum[i][L] if Ans > Value: Ans = Value print(Ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = map(int, input().split()) s = input().strip() costs = [] possible = "abc" for i in range(3): for j in range(3): if i != j: k = 0 while k == i or k == j: k += 1 pattern = possible[i] + possible[j] + possible[k] cost = [0] * (n + 1) for i2 in range(n): cost[i2] = cost[i2 - 1] + (s[i2] != pattern[i2 % 3]) costs.append(cost) for i in range(m): l, r = map(int, input().split()) l -= 1 r -= 1 ans = 1000000000.0 for cost in costs: ans = min(ans, cost[r] - cost[l - 1]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call the string beautiful if it does not contain a substring of length at least $2$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $3$ letters of the Latin alphabet (in lowercase). You are given a string $s$ of length $n$, each character of the string is one of the first $3$ letters of the Latin alphabet (in lowercase). You have to answer $m$ queries β€” calculate the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) β€” the length of the string $s$ and the number of queries. The second line contains the string $s$, it consists of $n$ characters, each character one of the first $3$ Latin letters. The following $m$ lines contain two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” parameters of the $i$-th query. -----Output----- For each query, print a single integer β€” the cost of the substring of the string $s$ from $l_i$-th to $r_i$-th position, inclusive. -----Examples----- Input 5 4 baacb 1 3 1 5 4 5 2 3 Output 1 2 0 1 -----Note----- Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
import sys input = sys.stdin.readline n, m = map(int, input().split()) st = list(input()) a0 = [0] a1 = [0] a2 = [0] b0 = [0] b1 = [0] b2 = [0] c0 = [0] c1 = [0] c2 = [0] a = [0, 0, 0] b = [0, 0, 0] c = [0, 0, 0] for i in range(n): if st[i] == "a": a[i % 3] += 1 if st[i] == "b": b[i % 3] += 1 if st[i] == "c": c[i % 3] += 1 a0.append(a[0]) a1.append(a[1]) a2.append(a[2]) b0.append(b[0]) b1.append(b[1]) b2.append(b[2]) c0.append(c[0]) c1.append(c[1]) c2.append(c[2]) for _ in range(m): l, r = map(int, input().split()) l -= 1 ans1 = ( a1[r] + a2[r] + b0[r] + b2[r] + c0[r] + c1[r] - (a1[l] + a2[l] + b0[l] + b2[l] + c0[l] + c1[l]) ) ans2 = ( a1[r] + a2[r] + b0[r] + b1[r] + c0[r] + c2[r] - (a1[l] + a2[l] + b0[l] + b1[l] + c0[l] + c2[l]) ) ans3 = ( a0[r] + a2[r] + b1[r] + b2[r] + c0[r] + c1[r] - (a0[l] + a2[l] + b1[l] + b2[l] + c0[l] + c1[l]) ) ans4 = ( a0[r] + a1[r] + b1[r] + b2[r] + c0[r] + c2[r] - (a0[l] + a1[l] + b1[l] + b2[l] + c0[l] + c2[l]) ) ans5 = ( a0[r] + a2[r] + b0[r] + b1[r] + c1[r] + c2[r] - (a0[l] + a2[l] + b0[l] + b1[l] + c1[l] + c2[l]) ) ans6 = ( a0[r] + a1[r] + b0[r] + b2[r] + c1[r] + c2[r] - (a0[l] + a1[l] + b0[l] + b2[l] + c1[l] + c2[l]) ) print(min(ans1, ans2, ans3, ans4, ans5, ans6))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
import sys input = sys.stdin.readline n = int(input()) ans = 1 arr = [int(x) for x in input().split()] for i in range(n): temp = set() d = {} for j in range(i + 1, n): if arr[j] == arr[i]: temp = set() elif arr[j] not in temp: if arr[j] in d: d[arr[j]] += 1 else: d[arr[j]] = 1 temp.add(arr[j]) number = -1 maxi = -1 for i in d: if maxi < d[i]: number = i maxi = d[i] if number in temp: ans = max(ans, 2 * maxi) else: ans = max(ans, 2 * maxi + 1) d = {} for i in arr: if i in d: d[i] += 1 else: d[i] = 1 for i in d: ans = max(ans, d[i]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
n = int(input()) b = list(map(int, input().split())) dp = [([1] * n) for i in range(n)] d = {} k = 0 for i in range(n): if b[i] not in d: d[b[i]] = k k += 1 b[i] = d[b[i]] for i in range(n): for j in range(i): dp[i][b[j]] = max(dp[j][b[i]] + 1, dp[i][b[j]]) ans = 0 for l in dp: ans = max(ans, max(l)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n = mint() a = [None] * n i = 0 for v in mints(): a[i] = v, i i += 1 a.sort() i = 0 r = 0 while i < n: j = i + 1 x = a[i][0] while j < n and a[j][0] == x: j += 1 r = max(r, j - i) ni = j while j < n: jj = j y = a[j][0] ii = i if a[ii][1] < a[jj][1]: cnt = 1 left = a[ii][1] ii += 1 else: cnt = 0 left = -1 fail = False while True: while True: if jj >= n or a[jj][0] != y: fail = True break if a[jj][1] > left: left = a[jj][1] cnt += 1 jj += 1 break jj += 1 if fail: break while True: if ii >= ni: fail = True break if a[ii][1] > left: left = a[ii][1] cnt += 1 ii += 1 break ii += 1 if fail: break r = max(r, cnt) while jj < n and a[jj][0] == y: jj += 1 j = jj i = ni print(r) solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE NUMBER IF VAR VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR WHILE NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
from sys import stdin def solve(tc): n = int(stdin.readline().strip()) seq = list(map(int, stdin.readline().split())) elems = [] idxcnt = 0 idx = dict() for i in range(n): if seq[i] not in idx: idx[seq[i]] = idxcnt idxcnt += 1 elems.append([]) elems[idx[seq[i]]].append(i) ans = 1 for li in elems: ans = max(ans, len(li)) m = len(elems) for i in range(m): for j in range(m): if i == j: continue cur = -1 cnt = 0 leni, lenj = len(elems[i]), len(elems[j]) pi, pj = 0, 0 while True: while pi < leni and elems[i][pi] < cur: pi += 1 if pi == leni: break cur = elems[i][pi] cnt += 1 pi += 1 while pj < lenj and elems[j][pj] < cur: pj += 1 if pj == lenj: break cur = elems[j][pj] cnt += 1 pj += 1 ans = max(ans, cnt) print(ans) tc = 1 solve(tc)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
n = int(input()) ls = list(map(int, input().split())) dp = [[(1) for i in range(n)] for j in range(n)] laspos = [None] * (max(ls) + 1) for i in range(n): for j in range(i): if laspos[ls[i]] is not None: dp[i][j] = 1 + dp[j][laspos[ls[i]]] else: dp[i][j] += 1 laspos[ls[j]] = j mx = -100000 for i in range(n): for j in range(n): mx = max(dp[i][j], mx) print(mx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NONE ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
n = int(input()) a = list(map(int, input().split())) dp = [[(0) for i in range(n + 1)] for i in range(n + 1)] ans = 0 pre = 0 a = [0] + a for i in range(1, n + 1): pre = 0 for j in range(i): dp[i][j] = dp[j][pre] + 1 if a[i] == a[j]: pre = j ans = max(ans, dp[i][j]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
input() a = [*map(int, input().split())] r = {} for i, x in enumerate(list(set(a))): r[x] = i n = len(r) m = 0 a = [r[x] for x in a] for i, x in enumerate(a): h = [(0) for _ in range(n)] l = [(-2) for _ in range(n)] lx = -1 for j, y in enumerate(a[i + 1 :]): if y == x: lx = j if l[y] < lx: h[y] += 1 l[y] = j for k in range(n): if k == x: h[k] = h[k] + 1 elif l[k] < lx: h[k] = 2 * h[k] + 1 else: h[k] = 2 * h[k] m = max(m, max(h + [1])) print(m)
EXPR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR LIST NUMBER EXPR FUNC_CALL VAR VAR
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
n = int(input()) t = list(map(int, input().split())) p = {a: (0) for a in set(t)} d = 0 for i in range(n): a = t[i] if not a in p: continue p.pop(a) s = t.count(a) - 1 if 2 * s < d: continue if s > d: d = s k = i + 1 for j in range(k, n): if t[j] == a: for b in set(t[k:j]): if b in p: p[b] += 2 k = j + 1 for b in set(t[k:n]): if b in p: p[b] += 1 for b in p: if p[b] > d: d = p[b] p[b] = 0 print(d + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
R = lambda: map(int, input().split()) n = int(input()) arr = list(R()) dp = [([0] * (n + 1)) for i in range(n + 1)] for i in range(n): p = -1 for j in range(i): dp[i][j] = max(dp[i][j], dp[j][p] + 1) p = j if arr[j] == arr[i] else p print(max(max(dp[i]) for i in range(n)) + 1)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, input().split())) def fn(a): mp = {} b = [] cnt = 0 for v in a: if v in mp: b += [mp[v]] else: b += [cnt] mp[v] = cnt cnt += 1 return b for i in range(1): n = nmbr() a = fn(lst()) N = max(a) + 1 ans = 1 dp = [[(1) for _ in range(N)] for j in range(n)] for i in range(1, n): for j in range(i): dp[i][a[j]] = 1 + dp[j][a[i]] ans = max(ans, dp[i][a[j]]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR LIST VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
def function(n, array): grid = [{} for i in range(n)] if n <= 2: print(n) return global_max = -10 for i in range(n - 2, -1, -1): for j in range(i + 1, n): diff = array[i] - array[j] max_val = 1 if -diff in grid[j].keys(): max_val = max(grid[j][-diff] + 1, max_val) if diff in grid[i].keys(): max_val = max(max_val, grid[i][diff]) grid[i][diff] = max_val else: grid[i][diff] = max_val global_max = max(global_max, max_val) print(global_max + 1) n = int(input()) array = [int(x) for x in input().split()] function(n, array)
FUNC_DEF ASSIGN VAR DICT VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
def get_len(a, b): if a[0] >= b[0]: c = a a = b b = c i = 0 j = 0 res = 2 while i < len(a) and j < len(b): while a[i] <= b[j]: i += 1 if i == len(a): break if i == len(a): break res += 1 while a[i] >= b[j]: j += 1 if j == len(b): break if j == len(b): break res += 1 return res n = int(input()) a = [int(e) for e in input().split()] d = dict() keys = [] for i in range(len(a)): x = a[i] if x in d: d[x].append(i) else: d[x] = [i] keys.append(x) ans = 0 for i in range(len(keys)): x = keys[i] for j in range(i + 1, len(keys)): y = keys[j] if x == y: continue i1 = 0 j1 = 0 xi = get_len(d[x], d[y]) ans = max(ans, xi) ans1 = [len(d[e]) for e in d] ans = max(ans, max(ans1)) print(ans)
FUNC_DEF IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: * a1 = p, where p is some integer; * ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≀ i1 < i2 < ... < ik ≀ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements. Input The first line contains integer n (1 ≀ n ≀ 4000). The next line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106). Output Print a single integer β€” the length of the required longest subsequence. Examples Input 2 3 5 Output 2 Input 4 10 20 10 30 Output 3 Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
def ap(arr): if len(arr) == 1: return 1 dp = [([1] * len(arr)) for i in range(len(arr))] cnst = 0 dct = {} for i in set(arr): dct[i] = cnst cnst += 1 for i in range(len(arr)): arr[i] = dct[arr[i]] ans = 0 for i in range(len(dp)): for j in range(i): dp[i][arr[j]] = max(dp[j][arr[i]] + 1, dp[i][arr[j]]) ans = max(ans, dp[i][arr[j]]) return ans a = input() lst = list(map(int, input().strip().split())) print(ap(lst))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≀ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≀ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
class Solution: def countNumbersWithUniqueDigits(self, n): if n == 0: return 1 elif n == 1: return 10 else: return ( self.countNumbersWithUniqueDigits(n - 1) - self.countNumbersWithUniqueDigits(n - 2) ) * (11 - n) + self.countNumbersWithUniqueDigits(n - 1)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≀ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≀ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
class Solution: def countNumbersWithUniqueDigits(self, n): choices = [9, 9, 8, 7, 6, 5, 4, 3, 2, 1] ans, product = 1, 1 for i in range(n if n <= 10 else 10): product *= choices[i] ans += product return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≀ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≀ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
class Solution: def countNumbersWithUniqueDigits(self, n): digits = [ 1, 10, 91, 739, 5275, 32491, 168571, 712891, 2345851, 5611771, 8877691, ] if n < len(digits): return digits[n] return digits[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN VAR NUMBER
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≀ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≀ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
class Solution: def countNumbersWithUniqueDigits(self, n): if n == 0: return 1 ans = 10 d = 9 prod = 9 for i in range(2, n + 1): prod *= d d -= 1 ans += prod return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN VAR
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≀ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≀ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
class Solution: def countNumbersWithUniqueDigits(self, n): if n == 0: return 1 if n > 10: return self.countNumbersWithUniqueDigits(10) count = 0 for i in range(1, n + 1): temp = 1 for j in range(i): temp *= 10 - j if temp > 10: temp = temp // 10 * 9 count += temp return count
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≀ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≀ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
class Solution: def countNumbersWithUniqueDigits(self, n): answer = 10**n for i in range(1, n + 1): all_count = 9 * 10 ** (i - 1) invalid = 9 for j in range(1, i): invalid *= 10 - j answer -= all_count - invalid return answer
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR RETURN VAR
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≀ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≀ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
class Solution: def countNumbersWithUniqueDigits(self, n): ls = [1, 10, 91] mul = 9 res = 0 for i in range(8): mul = 9 m = 9 for j in range(i + 2): mul *= m m -= 1 ls.append(mul + ls[-1]) if n >= 9: return ls[9] else: return ls[n]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER RETURN VAR VAR
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≀ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≀ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
class Solution: def countNumbersWithUniqueDigits(self, n): if n == 0: return 1 digits = [9, 9, 8, 7, 6, 5, 4, 3, 2, 1] ans = 0 while n > 0: if n == 1: ans += 10 else: res = 1 for i in range(n): res *= digits[i] ans += res n -= 1 return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
You have a rectangular chocolate bar consisting of n Γ— m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 Γ— 3 unit squares then you can break it horizontally and get two 1 Γ— 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 Γ— 1 and 2 Γ— 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining nΒ·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≀ t ≀ 40910)Β β€” the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≀ n, m ≀ 30, 1 ≀ k ≀ min(nΒ·m, 50))Β β€” the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 Γ— 2 bar into two pieces of 2 Γ— 1 (cost is 2^2 = 4), to split the resulting 2 Γ— 1 into two 1 Γ— 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
t = int(input()) dp = [[[(0) for i in range(51)] for j in range(31)] for k in range(31)] def cost(n, m, k): if dp[n][m][k] or k == 0 or n * m == k: return dp[n][m][k] c = 10**9 for i in range(1, n // 2 + 1): for j in range(k + 1): c = min(c, cost(n - i, m, k - j) + cost(i, m, j) + m * m) for i in range(1, m // 2 + 1): for j in range(k + 1): c = min(c, cost(n, m - i, k - j) + cost(n, i, j) + n * n) dp[n][m][k] = c return c for _ in range(t): n, m, k = map(int, input().split()) print(cost(n, m, k))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You have a rectangular chocolate bar consisting of n Γ— m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 Γ— 3 unit squares then you can break it horizontally and get two 1 Γ— 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 Γ— 1 and 2 Γ— 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining nΒ·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≀ t ≀ 40910)Β β€” the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≀ n, m ≀ 30, 1 ≀ k ≀ min(nΒ·m, 50))Β β€” the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 Γ— 2 bar into two pieces of 2 Γ— 1 (cost is 2^2 = 4), to split the resulting 2 Γ— 1 into two 1 Γ— 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
d = [0] * 49011 def g(n, m, k): t = 1000000000.0 for i in range(1, m // 2 + 1): for j in range(k + 1): t = min(t, f(n, m - i, k - j) + f(n, i, j)) return n * n + t def f(n, m, k): if n > m: n, m = m, n k = min(k, n * m - k) if k == 0: return 0 if k < 0: return 1000000000.0 q = n + 31 * m + 961 * k if d[q] == 0: d[q] = min(g(n, m, k), g(m, n, k)) return d[q] for q in range(int(input())): n, m, k = map(int, input().split()) print(f(n, m, k))
ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You have a rectangular chocolate bar consisting of n Γ— m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 Γ— 3 unit squares then you can break it horizontally and get two 1 Γ— 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 Γ— 1 and 2 Γ— 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining nΒ·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≀ t ≀ 40910)Β β€” the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≀ n, m ≀ 30, 1 ≀ k ≀ min(nΒ·m, 50))Β β€” the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 Γ— 2 bar into two pieces of 2 Γ— 1 (cost is 2^2 = 4), to split the resulting 2 Γ— 1 into two 1 Γ— 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
d = [[[(0) for i in range(51)] for j in range(31)] for g in range(31)] def rec(n, m, k): global d if k == 0 or n * m == k: return 0 if d[n][m][k] > 0: return d[n][m][k] if n * m < k: return 10**10 cost = 10**10 for i in range(1, n // 2 + 1): for j in range(k + 1): cost = min(cost, m**2 + rec(i, m, j) + rec(n - i, m, k - j)) for i in range(1, m // 2 + 1): for j in range(0, k + 1): cost = min(cost, n**2 + rec(n, i, j) + rec(n, m - i, k - j)) d[n][m][k] = cost return cost t = int(input()) a = [] for c in range(t): n, m, k = map(int, input().split()) a.append(rec(n, m, k)) print("\n".join(str(x) for x in a))
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER BIN_OP VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
You have a rectangular chocolate bar consisting of n Γ— m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 Γ— 3 unit squares then you can break it horizontally and get two 1 Γ— 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 Γ— 1 and 2 Γ— 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining nΒ·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≀ t ≀ 40910)Β β€” the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≀ n, m ≀ 30, 1 ≀ k ≀ min(nΒ·m, 50))Β β€” the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 Γ— 2 bar into two pieces of 2 Γ— 1 (cost is 2^2 = 4), to split the resulting 2 Γ— 1 into two 1 Γ— 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
mem = [[[(0) for i in range(51)] for j in range(31)] for k in range(31)] def f(n, m, k): if mem[n][m][k]: return mem[n][m][k] if n * m == k or k == 0: return 0 cost = 10**9 for x in range(1, n // 2 + 1): for z in range(k + 1): cost = min(cost, m * m + f(n - x, m, k - z) + f(x, m, z)) for y in range(1, m // 2 + 1): for z in range(k + 1): cost = min(cost, n * n + f(n, m - y, k - z) + f(n, y, z)) mem[n][m][k] = cost return cost t = int(input()) for i in range(t): n, m, k = list(map(int, input().split())) print(f(n, m, k))
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You have a rectangular chocolate bar consisting of n Γ— m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 Γ— 3 unit squares then you can break it horizontally and get two 1 Γ— 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 Γ— 1 and 2 Γ— 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining nΒ·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≀ t ≀ 40910)Β β€” the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≀ n, m ≀ 30, 1 ≀ k ≀ min(nΒ·m, 50))Β β€” the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 Γ— 2 bar into two pieces of 2 Γ— 1 (cost is 2^2 = 4), to split the resulting 2 Γ— 1 into two 1 Γ— 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
d = [[[(0) for i in range(51)] for j in range(31)] for k in range(31)] for i in range(31): d.append([]) for j in range(31): d[i].append([]) for k in range(50): d[i][j].append(0) def rec(n, m, k): global d if n * m == k or k == 0: return 0 if d[n][m][k] > 0: return d[n][m][k] if n * m < k: return 10**10 cost = 10**10 for i in range(1, n // 2 + 1): for j in range(k + 1): cost = min(cost, m * m + rec(n - i, m, k - j) + rec(i, m, j)) for i in range(1, m // 2 + 1): for j in range(k + 1): cost = min(cost, n * n + rec(n, m - i, k - j) + rec(n, i, j)) d[n][m][k] = cost return cost for i in range(int(input())): a, b, c = map(int, input().split()) print(rec(a, b, c))
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You have a rectangular chocolate bar consisting of n Γ— m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 Γ— 3 unit squares then you can break it horizontally and get two 1 Γ— 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 Γ— 1 and 2 Γ— 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining nΒ·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≀ t ≀ 40910)Β β€” the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≀ n, m ≀ 30, 1 ≀ k ≀ min(nΒ·m, 50))Β β€” the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 Γ— 2 bar into two pieces of 2 Γ— 1 (cost is 2^2 = 4), to split the resulting 2 Γ— 1 into two 1 Γ— 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
dp = {} def getDP(n, m, k): if (n, m, k) in dp: return dp[n, m, k] elif (m, n, k) in dp: return dp[m, n, k] return None def solve(n, m, k): if n == 2 and m == 3 and k == 5: h = 5 if k == m * n or k == 0: dp[n, m, k] = 0 elif k % min(n, m) == 0: dp[n, m, k] = min(n, m) ** 2 elif k == 1: dp[n, m, k] = min(n, m) ** 2 + 1 elif getDP(n, m, k) is not None: return getDP(n, m, k) else: bestAns = float("inf") for i in range(1, n): if k <= i * m: bestAns = min(bestAns, getDP(i, m, k) + m**2) else: bestAns = min(bestAns, getDP(n - i, m, k - i * m) + m**2) for i in range(1, m): if k <= i * n: bestAns = min(bestAns, getDP(i, n, k) + n**2) else: bestAns = min(bestAns, getDP(m - i, n, k - i * n) + n**2) dp[n, m, k] = bestAns for i in range(1, 31): for j in range(1, 31): for k in range(min(i * j, 50) + 1): solve(i, j, k) toPrint = [] t = int(input()) for i in range(t): n, m, k = [int(x) for x in input().split(" ")] toPrint.append(getDP(n, m, k)) for x in toPrint: print(x)
ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR RETURN NONE FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You have a rectangular chocolate bar consisting of n Γ— m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 Γ— 3 unit squares then you can break it horizontally and get two 1 Γ— 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 Γ— 1 and 2 Γ— 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining nΒ·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≀ t ≀ 40910)Β β€” the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≀ n, m ≀ 30, 1 ≀ k ≀ min(nΒ·m, 50))Β β€” the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 Γ— 2 bar into two pieces of 2 Γ— 1 (cost is 2^2 = 4), to split the resulting 2 Γ— 1 into two 1 Γ— 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
import sys input = sys.stdin.readline d = {} testnumber = int(input()) def calc(n, m, k): if k <= 0 or k == m * n: return 0 if k > n * m: return 1000000000 nonlocal d if n < m: n, m = m, n if k > m * n - m: return m * m + 1 if k < m: return m * m + 1 if k % m == 0: return m * m if (n, m, k) in d: return d[n, m, k] d[n, m, k] = min(calc2(n, m, k), calc2(m, n, k)) return d[n, m, k] def calc2(n, m, k): m2 = m * m ans = m2 * 2 + 1 for i in range(1, n): if i * m >= k: ans = min(ans, m2 + calc(m, i, k)) else: ans = min(ans, m2 + calc(m, n - i, k - i * m)) return ans for ntest in range(testnumber): n, m, k = map(int, input().split()) if k == n * m: print(0) continue print(calc(n, m, k))
IMPORT ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You have a rectangular chocolate bar consisting of n Γ— m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 Γ— 3 unit squares then you can break it horizontally and get two 1 Γ— 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 Γ— 1 and 2 Γ— 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining nΒ·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≀ t ≀ 40910)Β β€” the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≀ n, m ≀ 30, 1 ≀ k ≀ min(nΒ·m, 50))Β β€” the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 Γ— 2 bar into two pieces of 2 Γ— 1 (cost is 2^2 = 4), to split the resulting 2 Γ— 1 into two 1 Γ— 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
D = {} def h(m, n, answ, k): x = answ for i in range(1, (n + 2) // 2): if k >= i * m: if m**2 + ans(m, n - i, k - i * m) < x: x = m**2 + ans(m, n - i, k - i * m) if k <= (n - i) * m: if m**2 + ans(m, n - i, k) < x: x = m**2 + ans(m, n - i, k) if k >= (n - i) * m: if m**2 + ans(m, i, k - (n - i) * m) < x: x = m**2 + ans(m, i, k - (n - i) * m) if k <= i * m: if m**2 + ans(m, i, k) < x: x = m**2 + ans(m, i, k) return x def ans(m, n, k): if k == 0: D[m, n, k] = 0 D[n, m, k] = 0 return 0 if m * n == k: D[m, n, k] = 0 D[n, m, k] = 0 return 0 elif m == 1: D[m, n, k] = 1 D[n, m, k] = 1 return 1 elif n == 1: D[m, n, k] = 1 D[n, m, k] = 1 return 1 elif (m, n, k) in D: return D[m, n, k] else: answ = n**2 * m t = h(m, n, answ, k) if t < answ: answ = t s = h(n, m, answ, k) if s < answ: answ = s D[m, n, k] = answ D[n, m, k] = answ return answ for i in range(30, 0, -1): for j in range(i, 0, -1): for k in range(0, min(i * j, 50) + 1): ans(i, j, k) t = int(input()) for i in range(t): m, n, k = [int(x) for x in input().split()] print(D[m, n, k])
ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
You have a rectangular chocolate bar consisting of n Γ— m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 Γ— 3 unit squares then you can break it horizontally and get two 1 Γ— 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 Γ— 1 and 2 Γ— 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining nΒ·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≀ t ≀ 40910)Β β€” the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≀ n, m ≀ 30, 1 ≀ k ≀ min(nΒ·m, 50))Β β€” the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 Γ— 2 bar into two pieces of 2 Γ— 1 (cost is 2^2 = 4), to split the resulting 2 Γ— 1 into two 1 Γ— 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
import sys input = sys.stdin.readline def main(): ans = [] memo = [[[(-1) for _ in range(51)] for _ in range(31)] for _ in range(31)] def solve(n, m, k): if n * m == k or k == 0: return 0 if memo[n][m][k] > -1: return memo[n][m][k] if memo[m][n][k] > -1: memo[n][m][k] = memo[m][n][k] return memo[n][m][k] r = float("inf") for i in range(k + 1): for j in range(1, max(m, n)): if m > j: r = min(r, n**2 + solve(j, n, i) + solve(m - j, n, k - i)) if n > j: r = min(r, m**2 + solve(m, j, i) + solve(m, n - j, k - i)) memo[n][m][k] = r return r for _ in range(int(input())): n, m, k = map(int, input().split()) ans.append(str(solve(n, m, k))) print("\n".join(ans)) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
You have a rectangular chocolate bar consisting of n Γ— m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 Γ— 3 unit squares then you can break it horizontally and get two 1 Γ— 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 Γ— 1 and 2 Γ— 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining nΒ·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≀ t ≀ 40910)Β β€” the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≀ n, m ≀ 30, 1 ≀ k ≀ min(nΒ·m, 50))Β β€” the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 Γ— 2 bar into two pieces of 2 Γ— 1 (cost is 2^2 = 4), to split the resulting 2 Γ— 1 into two 1 Γ— 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
import sys mem = [] for i in range(32): mem.append([([-1] * 52) for u in range(32)]) def solve(x, y, z): if x > y: mem[x][y][z] = solve(y, x, z) return mem[x][y][z] if x * y == z or z == 0: mem[x][y][z] = 0 return 0 if x * y < z: mem[x][y][z] = -2 return -2 res = -2 for i in range(1, x // 2 + 1): for eaten in range(z + 1): t1 = mem[i][y][eaten] if mem[i][y][eaten] != -1 else solve(i, y, eaten) if t1 == -2: continue t2 = ( mem[x - i][y][z - eaten] if mem[x - i][y][z - eaten] != -1 else solve(x - i, y, z - eaten) ) if t2 == -2: continue if res == -2 or res > t1 + t2 + y * y: res = t1 + t2 + y * y for j in range(1, y // 2 + 1): for eaten in range(z + 1): t1 = mem[x][j][eaten] if mem[x][j][eaten] != -1 else solve(x, j, eaten) if t1 == -2: continue t2 = ( mem[x][y - j][z - eaten] if mem[x][y - j][z - eaten] != -1 else solve(x, y - j, z - eaten) ) if t2 == -2: continue if res == -2 or res > t1 + t2 + x * x: res = t1 + t2 + x * x mem[x][y][z] = res return mem[x][y][z] t = int(sys.stdin.readline()) for it in range(t): n, m, k = map(int, sys.stdin.readline().split()) print(solve(n, m, k))
IMPORT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You have a rectangular chocolate bar consisting of n Γ— m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2 Γ— 3 unit squares then you can break it horizontally and get two 1 Γ— 3 pieces (the cost of such breaking is 3^2 = 9), or you can break it vertically in two ways and get two pieces: 2 Γ— 1 and 2 Γ— 2 (the cost of such breaking is 2^2 = 4). For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining nΒ·m - k squares are not necessarily form a single rectangular piece. -----Input----- The first line of the input contains a single integer t (1 ≀ t ≀ 40910)Β β€” the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≀ n, m ≀ 30, 1 ≀ k ≀ min(nΒ·m, 50))Β β€” the dimensions of the chocolate bar and the number of squares you want to eat respectively. -----Output----- For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares. -----Examples----- Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Output 5 5 4 0 -----Note----- In the first query of the sample one needs to perform two breaks: to split 2 Γ— 2 bar into two pieces of 2 Γ— 1 (cost is 2^2 = 4), to split the resulting 2 Γ— 1 into two 1 Γ— 1 pieces (cost is 1^2 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
t = int(input()) d = [] for i in range(31): dd = [] for j in range(31): dd.append([0] * 51) d.append(dd) d[1][1][1] = 0 for i in range(1, 31): for j in range(1, 31): for k in range(1, min(i * j, 50) + 1): if k > i * j // 2: d[i][j][k] = d[i][j][i * j - k] elif i > j: d[i][j][k] = d[j][i][k] elif (i, j) != (1, 1): k = min(k, i * j - k) kk = i * j - k jj = i**2 * j * j**2 * i for l in range(1, i): if k <= l * j: jj = min(jj, d[l][j][k] + j**2) else: k1 = k - l * j jj = min(jj, d[i - l][j][k1] + j**2) if kk <= l * j: if kk <= 50: jj = min(jj, d[l][j][kk] + j**2) else: kk1 = kk - l * j if kk1 <= 50: jj = min(jj, d[i - l][j][kk1] + j**2) for l in range(1, j): if k <= l * i: jj = min(jj, d[i][l][k] + i**2) else: k1 = k - l * i jj = min(jj, d[i][j - l][k1] + i**2) if kk <= l * i: if kk <= 50: jj = min(jj, d[i][l][kk] + i**2) else: kk1 = kk - l * i if kk1 <= 50: jj = min(jj, d[i][j - l][kk1] + i**2) d[i][j][k] = jj for i in range(t): n, m, k = list(map(int, input().split())) jj = d[n][m][k] print(jj)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of numbers. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$. -----Output----- Print $s$Β β€” a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B". -----Examples----- Input 8 3 6 5 4 2 7 1 8 Output BAAAABAB Input 15 3 11 2 5 10 9 7 13 15 8 4 12 6 1 14 Output ABAAAABBBAABAAB -----Note----- In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately.
from itertools import combinations from sys import setrecursionlimit, stdin setrecursionlimit(200000) def reverse_check_big(string, index): temp = string[: index + 1] forward = "".join(temp) reverse = "".join(temp[::-1]) if reverse > forward: return True else: return False def change(string, index): temp = string[: index + 1] temp = temp[::-1] for i in range(index + 1): string[i] = temp[i] def reverse_check_small(string, index): temp = string[: index + 1] forward = "".join(temp) reverse = "".join(temp[::-1]) if reverse < forward: return True else: return False def find_smaller_index(string, index): malph = "z" ans = -1 for i in range(index + 1): if string[i] <= malph: malph = string[i] ans = i return ans def find_bigger_index(string, index): malph = "a" ans = -1 for i in range(index + 1): if string[i] >= malph: malph = string[i] ans = i return ans def make(first, second): return str(first) + " " + str(second) n = int(stdin.readline().strip()) arr = list(map(int, stdin.readline().split())) mydict = dict() for i in range(n): mydict[arr[i]] = i + 1 ans = dict() ans[mydict[n]] = "B" for i in range(n - 1, 0, -1): num = i index = mydict[num] flag = 0 for j in range(index + num, n + 1, num): if j in ans and ans[j] == "B": ans[index] = "A" flag = 1 break else: ans[index] = "B" if flag == 0: for j in range(index - num, 0, -num): if j in ans and ans[j] == "B": ans[index] = "A" break else: ans[index] = "B" for i in range(1, n + 1): print(ans[i], end="")
EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of numbers. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$. -----Output----- Print $s$Β β€” a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B". -----Examples----- Input 8 3 6 5 4 2 7 1 8 Output BAAAABAB Input 15 3 11 2 5 10 9 7 13 15 8 4 12 6 1 14 Output ABAAAABBBAABAAB -----Note----- In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately.
n = int(input()) a = list(map(int, input().split())) pos = [(0) for i in range(n + 1)] for i in range(n): pos[a[i]] = i dp = [(0) for i in range(n + 1)] for i in range(n - 1, 0, -1): j = pos[i] for k in range(j, n, i): if a[k] > i: if dp[a[k]] == 0: dp[i] = 1 for k in range(j, -1, -i): if a[k] > i: if dp[a[k]] == 0: dp[i] = 1 for i in range(n): if dp[a[i]] == 0: print("B", end="") else: print("A", end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of numbers. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$. -----Output----- Print $s$Β β€” a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B". -----Examples----- Input 8 3 6 5 4 2 7 1 8 Output BAAAABAB Input 15 3 11 2 5 10 9 7 13 15 8 4 12 6 1 14 Output ABAAAABBBAABAAB -----Note----- In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately.
n = int(input()) l = [(-1) for i in range(n + 1)] s = input().split() ll = [int(i) for i in s] for i in range(n): j = ll[i] l[j] = i res = ["." for i in range(n + 1)] res[n] = "B" for i in range(n - 1, 0, -1): toadd = "B" pos = l[i] % i while pos < n: j = ll[pos] if j <= i: pos += i continue if abs(l[i] - l[j]) % i == 0 and res[j] == "B": toadd = "A" break pos += i res[i] = toadd for i in ll: print(res[i], end="") print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR STRING ASSIGN VAR STRING VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of numbers. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$. -----Output----- Print $s$Β β€” a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B". -----Examples----- Input 8 3 6 5 4 2 7 1 8 Output BAAAABAB Input 15 3 11 2 5 10 9 7 13 15 8 4 12 6 1 14 Output ABAAAABBBAABAAB -----Note----- In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately.
import sys from sys import stdin n = int(stdin.readline().strip()) s = list(map(int, stdin.readline().strip().split())) dp = ["B" for i in range(n)] visited = [(False) for i in range(n)] sys.setrecursionlimit(10**9) def dfs(i): x = s[i] visited[i] = True for j in range(i - x, -1, -x): if s[j] > x: if not visited[j]: dfs(j) if dp[j] == "B": dp[i] = "A" return for j in range(i + x, n, x): if s[j] > x: if not visited[j]: dfs(j) if dp[j] == "B": dp[i] = "A" return for i in range(n): if not visited[i]: dfs(i) ans = "" for i in dp: ans += i print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING RETURN FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of numbers. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$. -----Output----- Print $s$Β β€” a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B". -----Examples----- Input 8 3 6 5 4 2 7 1 8 Output BAAAABAB Input 15 3 11 2 5 10 9 7 13 15 8 4 12 6 1 14 Output ABAAAABBBAABAAB -----Note----- In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately.
from sys import setrecursionlimit, stdin setrecursionlimit(10**7) def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) dp = [-1] * (10**5 + 10) def check(n, idx, a): global dp if dp[idx] != -1: return dp[idx] step = a[idx] ans = 0 odd = 0 for i in range(idx + step, n, step): if a[i] > a[idx]: x = check(n, i, a) + 1 if x % 2: odd = x ans = max(x, ans) for i in range(idx - step, -1, -step): if a[i] > a[idx]: x = check(n, i, a) + 1 if x % 2: odd = x ans = max(x, ans) dp[idx] = ans if odd == 0 else odd return dp[idx] def main(): n = iin() a = lin() for i in range(n): check(n, i, a) ans = [] for i in range(n): ans.append(["B", "A"][dp[i] % 2]) print("".join(ans)) main()
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST STRING STRING BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of numbers. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$. -----Output----- Print $s$Β β€” a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B". -----Examples----- Input 8 3 6 5 4 2 7 1 8 Output BAAAABAB Input 15 3 11 2 5 10 9 7 13 15 8 4 12 6 1 14 Output ABAAAABBBAABAAB -----Note----- In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately.
n = int(input()) L = list(map(int, input().split())) ans = [""] * n revL = [0] * n ans[-1] = "B" for i in range(n): revL[L[i] - 1] = i + 1 for i in range(n - 2, -1, -1): t = revL[i] - 1 counter = "B" for j in range(t, -1, -i - 1): if j == t: continue if ans[L[j] - 1] == "B": counter = "A" break if counter != "A": for k in range(t, n, i + 1): if k == t: continue if ans[L[k] - 1] == "B": counter = "A" break ans[i] = counter for i in range(n): print(ans[L[i] - 1], sep="", end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR STRING IF VAR STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR VAR NUMBER STRING ASSIGN VAR STRING ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER STRING STRING
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of numbers. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$. -----Output----- Print $s$Β β€” a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B". -----Examples----- Input 8 3 6 5 4 2 7 1 8 Output BAAAABAB Input 15 3 11 2 5 10 9 7 13 15 8 4 12 6 1 14 Output ABAAAABBBAABAAB -----Note----- In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately.
import sys next(sys.stdin) positions = {} pos2x = {} for i, x in enumerate(next(sys.stdin).split()): x = int(x) positions[x] = i pos2x[i] = x answers = ["" for _ in range(len(positions))] for x in range(len(positions), 0, -1): position = positions[x] def can_go_to_looser(): next_position = position + x while next_position < len(positions): if pos2x[next_position] > x and answers[next_position] == "B": return True next_position += x next_position = position - x while next_position >= 0: if pos2x[next_position] > x and answers[next_position] == "B": return True next_position -= x return False if can_go_to_looser(): answers[position] = "A" else: answers[position] = "B" print("".join(answers))
IMPORT EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR STRING RETURN NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR VAR STRING RETURN NUMBER VAR VAR RETURN NUMBER IF FUNC_CALL VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of numbers. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$. -----Output----- Print $s$Β β€” a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B". -----Examples----- Input 8 3 6 5 4 2 7 1 8 Output BAAAABAB Input 15 3 11 2 5 10 9 7 13 15 8 4 12 6 1 14 Output ABAAAABBBAABAAB -----Note----- In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately.
N = int(input()) a = list(map(int, input().split())) rec = ["+"] * N P = [0] * N for i in range(N): P[a[i] - 1] = i for i in range(N, 0, -1): j = P[i - 1] rec[j] = "B" for k in range(j % i, N, i): if j != k and a[j] <= a[k] and rec[k] == "B": rec[j] = "A" break print("".join(map(str, rec)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β€” the number of numbers. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$. -----Output----- Print $s$Β β€” a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B". -----Examples----- Input 8 3 6 5 4 2 7 1 8 Output BAAAABAB Input 15 3 11 2 5 10 9 7 13 15 8 4 12 6 1 14 Output ABAAAABBBAABAAB -----Note----- In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately.
n = int(input()) l = [*map(int, input().split())] p = [0] * n for i in range(n): p[l[i] - 1] = i res = ["?"] * n for e in range(n, 0, -1): i = p[e - 1] res[i] = "B" for j in range(i % e, n, e): if i != j and l[i] <= l[j] and res[j] == "B": res[i] = "A" break print("".join(res))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR