description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
L1 = [] L2 = [] for i in range(10): l1 = [] l2 = [] for j in range(10): if (i + j) % 2 == 0: l1.append("*") l2.append(".") else: l1.append(".") l2.append("*") L1.append(l1) L2.append(l2) t = int(input()) for i in range(t): N, M = list(map(int, input().split())) NM = [] for i in range(N): nm = list(input()) NM.append(nm) c1 = 0 c2 = 0 for i in range(N): for j in range(M): if NM[i][j] != L1[i][j]: c1 += 1 if NM[i][j] != L2[i][j]: c2 += 1 if N * M % 2 != 0: print(c1) elif c2 < c1: print(c2) else: print(c1)
ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): n, k = map(int, input().split()) t = n * k arr = [] for i in range(n): arr.append([str(x) for x in input()]) ops1 = 0 ops2 = 0 for i in range(n): for j in range(k): if i % 2 == 0: if j % 2 == 0 and arr[i][j] == ".": ops1 += 1 elif j % 2 == 1 and arr[i][j] == "*": ops1 += 1 elif j % 2 == 1 and arr[i][j] == ".": ops1 += 1 elif j % 2 == 0 and arr[i][j] == "*": ops1 += 1 if i % 2 == 1: if j % 2 == 0 and arr[i][j] == ".": ops2 += 1 elif j % 2 == 1 and arr[i][j] == "*": ops2 += 1 elif j % 2 == 1 and arr[i][j] == ".": ops2 += 1 elif j % 2 == 0 and arr[i][j] == "*": ops2 += 1 if n % 2 == 1 and k % 2 == 1: print(ops1) else: print(min(ops1, ops2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) for i in range(t): n, m = map(int, input().split()) arr = [] for i in range(n): arr.append(input()) if n * m % 2 == 0: ans1 = 0 for i in range(n): for j in range(m): if (i + j) % 2 == 0: if arr[i][j] != "*": ans1 = ans1 + 1 elif arr[i][j] != ".": ans1 = ans1 + 1 ans2 = 0 for i in range(n): for j in range(m): if (i + j) % 2 == 0: if arr[i][j] != ".": ans2 = ans2 + 1 elif arr[i][j] != "*": ans2 = ans2 + 1 print(min(ans1, ans2)) else: ans1 = 0 for i in range(n): for j in range(m): if (i + j) % 2 == 0: if arr[i][j] != "*": ans1 = ans1 + 1 elif arr[i][j] != ".": ans1 = ans1 + 1 print(ans1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def posGrid(n, m): if m % 2: g1 = [([""] * m) for i in range(n)] for r in range(n): for c in range(m): if (r + c) % 2: g1[r][c] = "." else: g1[r][c] = "*" if n % 2: return [g1] g2 = [] for row in g1: g2.append(row[:]) for i in range(0, n, 2): g2[i], g2[i + 1] = g2[i + 1], g2[i] return [g1, g2] else: g1 = [([""] * m) for i in range(n)] g2 = [([""] * m) for i in range(n)] for r in range(n): for c in range(m): if (r + c) % 2: g1[r][c] = "." g2[r][c] = "*" else: g1[r][c] = "*" g2[r][c] = "." return [g1, g2] def show(g): for grid in g: for row in grid: print(row) print() def solve(grid, n, m): ans = float("inf") for Grid in posGrid(n, m): part = 0 for i in range(n): for j in range(m): if grid[i][j] != Grid[i][j]: part += 1 ans = min(ans, part) return ans for case in range(int(input())): n, m = map(int, input().split()) grid = [] for i in range(n): grid.append(input()) ans = solve(grid, n, m) print(ans)
FUNC_DEF IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING IF BIN_OP VAR NUMBER RETURN LIST VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN LIST VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING RETURN LIST VAR VAR FUNC_DEF FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): N, M = map(int, input().split()) grid = [list(map(str, input())) for i in range(N)] d = 0 for i in range(N): for j in range(M): if grid[i][j] == "*": grid[i][j] = 1 else: grid[i][j] = 0 if grid[i][j] != (i + j + 1) % 2: d += 1 if N * M % 2 == 0: print(min(M * N - d, d)) else: print(d)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): s = [] n, m = map(int, input().split()) for i in range(n): s.append(input()) c = 0 for i in range(n): for j in range(m): if ( (i + j) % 2 == 0 and s[i][j] != "*" or (i + j) % 2 == 1 and s[i][j] != "." ): c += 1 ans = c if n * m % 2 == 0: c = 0 for i in range(n): for j in range(m): if ( (i + j) % 2 == 0 and s[i][j] != "." or (i + j) % 2 == 1 and s[i][j] != "*" ): c += 1 print(min(ans, c))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for i in range(int(input())): n, m = map(int, input().split()) l = [] for i in range(n): u = list(input()) l.append(u) ans1, ans2 = 0, 0 for i in range(n): for j in range(m): if (i + j) % 2 == 0 and l[i][j] == ".": ans1 += 1 elif (i + j) % 2 == 1 and l[i][j] == "*": ans1 += 1 for i in range(n): for j in range(m): if (i + j) % 2 == 0 and l[i][j] == "*": ans2 += 1 elif (i + j) % 2 == 1 and l[i][j] == ".": ans2 += 1 if n * m % 2 == 0: print(min(ans1, ans2)) else: print(ans1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
T = int(input()) for i in range(T): n, m = map(int, input().split()) if n * m % 2 == 0: x1 = 0 x2 = 0 for i in range(n): a = input() for j in range(m): if (i + j) % 2 == 0: if a[j] == ".": x1 += 1 else: x2 += 1 elif a[j] == "*": x1 += 1 else: x2 += 1 print(min(x1, x2)) else: c = 0 for i in range(n): a = input() for j in range(m): if (i + j) % 2 == 0: if a[j] == ".": c += 1 elif a[j] == "*": c += 1 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
I = input t = int(I()) for _ in range(t): n, m = map(int, I().split()) k = n * m c = k // 2 for i in range(n): s = I() for j in range(m): c += s[j] == "*" and 1 - (i ^ j) % 2 * 2 print(min((c, k - c)[k & 1 :]))
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR STRING BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): n, m = map(int, input().split()) a = [[(0) for _ in range(m)] for i in range(n)] sum = 0 for i in range(n): x = input() for j in range(m): if x[j] == "*": a[i][j] = 1 sum += abs(a[i][j] - (1 - (i + j) % 2)) if n * m % 2 == 0: print(min(sum, m * n - sum)) else: print(sum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
MOD = 1000000007 def si(): return input() def ii(): return int(input()) def li(): return list(map(int, input().split())) def mi(): return map(int, input().split()) def sout(v): print(v, end=" ") def d2b(n): return bin(n).replace("0b", "") def twod(n, m, num): return [[num for x in range(m)] for y in range(n)] def vow(): return ["a", "e", "i", "o", "u"] def let(): return [chr(i) for i in range(97, 123)] def gcd(x, y): while y: x, y = y, x % y return x def ispow2(x): return x and not x & x - 1 def prime_factors(n): i = 2 factors = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(i) if n > 1: factors.append(n) return list(factors) t = ii() while t: t -= 1 n, m = mi() a = [] for _ in range(n): a.append(si()) if n * m % 2 == 1: c = 0 for i in range(n): for j in range(m): if i % 2 == 0 and j % 2 == 0 or i % 2 == 1 and j % 2 == 1: if a[i][j] == ".": c += 1 elif a[i][j] == "*": c += 1 print(c) else: c1 = c2 = 0 for i in range(n): for j in range(m): if i % 2 == 0 and j % 2 == 1 or i % 2 == 1 and j % 2 == 0: if a[i][j] == ".": c1 += 1 elif a[i][j] == "*": c1 += 1 for i in range(n): for j in range(m): if i % 2 == 0 and j % 2 == 0 or i % 2 == 1 and j % 2 == 1: if a[i][j] == ".": c2 += 1 elif a[i][j] == "*": c2 += 1 print(min(c1, c2))
ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN LIST STRING STRING STRING STRING STRING FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): sum1 = 0 x, y = map(int, input().split()) comp = "*" if y % 2 != 0 and x % 2 != 0: comp = "*" bl = True cnt = 0 for i in range(x): s = input() comp = "*" if i % 2 != 0: comp = "." for j in s: if j != comp: cnt += 1 if bl: comp = "." bl = False else: comp = "*" bl = True print(cnt) else: sum1 = 0 sum2 = 0 for i in range(x): s = input() swap1 = "*" swap2 = "." if i % 2 != 0: swap1, swap2 = swap2, swap1 for j in s: if j != swap1: sum1 += 1 if j != swap2: sum2 += 1 swap1, swap2 = swap2, swap1 print(min(sum1, sum2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) for _ in range(t): n, m = map(int, input().split(" ")) fulls = [] count1 = 0 count2 = 0 loji1 = 0 loji2 = 0 for i in range(n): fulls.append(list(input())) for i in range(n): for j in range(m): if (i + 1) % 2 == 1: if (j + 1) % 2 == 1: if fulls[i][j] != "*": count1 += 1 loji1 += 1 elif fulls[i][j] != ".": count1 += 1 if (i + 1) % 2 == 0: if (j + 1) % 2 == 0: if fulls[i][j] != "*": count1 += 1 loji1 += 1 elif fulls[i][j] != ".": count1 += 1 for i in range(n): for j in range(m): if (i + 1) % 2 == 1: if (j + 1) % 2 == 1: if fulls[i][j] != ".": count2 += 1 else: if fulls[i][j] != "*": count2 += 1 loji2 += 1 if (i + 1) % 2 == 0: if (j + 1) % 2 == 0: if fulls[i][j] != ".": count2 += 1 else: if fulls[i][j] != "*": count2 += 1 loji2 += 1 if loji2 > loji1: print(count2) elif loji1 > loji2: print(count1) else: print(min(count1, count2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
n = int(input()) for i in range(n): a, b = map(int, input().split()) s = [] cs = [0] * 2 cd = [0] * 2 for _ in range(a): s.append(str(input())) for i in range(a): for j in range(b): if s[i][j] == "*": cs[(i + j) % 2] += 1 else: cd[(i + j) % 2] += 1 t = a * b // 2 if a % 2 == 0 or b % 2 == 0: ans1 = abs(cs[0] - t) + abs(t - cd[1]) ans2 = abs(cs[1] - t) + abs(t - cd[0]) print(min(ans1, ans2)) else: print(abs(cs[0] - (t + 1)) + abs(cd[1] - t))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) l = [] for i in range(n): temp = input() l.append(temp) ans1 = 0 ans2 = 0 for i in range(len(l)): s = l[i] for j in range(len(s)): if (i + j) % 2 == 0 and s[j] == ".": ans1 = ans1 + 1 elif (i + j) % 2 == 1 and s[j] == "*": ans1 = ans1 + 1 for i in range(len(l)): s = l[i] for j in range(len(s)): if (i + j) % 2 == 0 and s[j] == "*": ans2 = ans2 + 1 elif (i + j) % 2 == 1 and s[j] == ".": ans2 = ans2 + 1 if n * m % 2 == 0: print(min(ans1, ans2)) elif n * m % 2 == 1: print(ans1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for t in range(int(input())): n, m = map(int, input().split()) s = [] for i in range(n): s.append(input()) cnt, op1, op2 = 0, 0, 0 if n % 2 == 1 and m % 2 == 1: ans = 0 for i in range(n): for j in range(m): if cnt % 2 == 0 and s[i][j] != "*": ans += 1 if cnt % 2 == 1 and s[i][j] != ".": ans += 1 cnt += 1 print(ans) continue for i in range(n): for j in range(m): if cnt % 2 == 0 and s[i][j] != "*": op1 += 1 if cnt % 2 == 1 and s[i][j] != ".": op1 += 1 if cnt % 2 == 0 and s[i][j] != ".": op2 += 1 if cnt % 2 == 1 and s[i][j] != "*": op2 += 1 cnt += 1 if m % 2 == 0: cnt += 1 print(min(op1, op2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def string_start(lst, m, c, d): o = "" for i in range(m): if i % 2 == 0: o += "." if lst[d][i] != ".": c += 1 else: o += "*" if lst[d][i] != "*": c += 1 lst[d] = o return c def string_start_1(lst, m, c, d): o = "" for i in range(m): if i % 2 == 0: o += "*" if lst[d][i] != "*": c += 1 else: o += "." if lst[d][i] != ".": c += 1 lst[d] = o return c def string_changes(lst, m, c): if len(lst) == 1: return c else: o = "" for i in range(m): if lst[-1][i] == lst[-2][i]: c += 1 if lst[-2][i] == "*": o += "." else: o += "*" else: o += lst[-2][i] lst[-2] = o lst.pop(len(lst) - 1) return string_changes(lst, m, c) def string_changes_front(lst, m, c): if len(lst) == 1: return c else: o = "" for i in range(m): if lst[0][i] == lst[1][i]: c += 1 if lst[1][i] == "*": o += "." else: o += "*" else: o += lst[1][i] lst[1] = o lst.pop(0) return string_changes_front(lst, m, c) for i in range(int(input())): n, m = map(int, input().split()) arr = [] lst = [] lst1 = [] arr1 = [] for j in range(n): s = input() lst.append(s) arr.append(s) lst1.append(s) arr1.append(s) c_l, cl = 0, 0 c_r, cr = 0, 0 if n * m % 2 != 0: cl += string_start_1(lst1, m, 0, -1) cl += string_changes(lst1, m, 0) cr += string_start_1(arr1, m, 0, 0) cr += string_changes_front(arr1, m, 0) print(min(cl, cr)) else: c_l += string_start(lst, m, 0, -1) c_l += string_changes(lst, m, 0) cl += string_start_1(lst1, m, 0, -1) cl += string_changes(lst1, m, 0) c_r += string_start(arr, m, 0, 0) c_r += string_changes_front(arr, m, 0) cr += string_start_1(arr1, m, 0, 0) cr += string_changes_front(arr1, m, 0) print(min(cl, cr, c_l, c_r))
FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR STRING IF VAR VAR VAR STRING VAR NUMBER VAR STRING IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR STRING IF VAR VAR VAR STRING VAR NUMBER VAR STRING IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR STRING VAR STRING VAR STRING VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR STRING VAR STRING VAR STRING VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def test(n, m): a = "*" b = "." s1 = "" s2 = "" for i in range(m): if i % 2 == 0: s1 += a s2 += b else: s1 += b s2 += a ls1 = [] ls2 = [] for i in range(n): if i % 2 == 0: ls1.append(s1) ls2.append(s2) else: ls1.append(s2) ls2.append(s1) a = sum(i.count("*") for i in ls1) b = sum(i.count("*") for i in ls2) if a == b: return ls1, ls2 elif a > b: return ls1, ls1 else: return ls2, ls2 def diff(ls1, ls2): count = 0 for i in range(len(ls1)): s1 = ls1[i] s2 = ls2[i] for j in range(len(s1)): if s1[j] != s2[j]: count += 1 return count t = int(input()) for i in range(t): n, m = map(int, input().split()) ls1, ls2 = test(n, m) ls3 = [input() for i in range(n)] print(min(diff(ls1, ls3), diff(ls2, ls3)))
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR 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 EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING VAR VAR IF VAR VAR RETURN VAR VAR IF VAR VAR RETURN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def build(n, m, matrix): if n * m % 2 == 0: ans1 = 0 ans2 = 0 for i in range(n): for j in range(m): if (i + j) % 2 == 0: if matrix[i][j] != "*": ans1 += 1 if matrix[i][j] != ".": ans2 += 1 else: if matrix[i][j] != ".": ans1 += 1 if matrix[i][j] != "*": ans2 += 1 return min(ans1, ans2) else: ans = 0 for i in range(n): for j in range(m): if (i + j) % 2 == 0: if matrix[i][j] != "*": ans += 1 elif matrix[i][j] != ".": ans += 1 return ans cases = int(input()) for v in range(cases): n, m = map(int, input().strip().split()) matrix = [] for i in range(n): a = list(input()) matrix.append(a) print(build(n, m, matrix))
FUNC_DEF IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def mgc(): ops1 = 0 for i in range(n): for j in range(m): if i % 2 == 0: if j % 2 == 0 and arr[i][j] == ".": ops1 += 1 elif j % 2 == 1 and arr[i][j] == "*": ops1 += 1 elif j % 2 == 1 and arr[i][j] == ".": ops1 += 1 elif j % 2 == 0 and arr[i][j] == "*": ops1 += 1 ops2 = 0 for i in range(n): for j in range(m): if i % 2 == 1: if j % 2 == 0 and arr[i][j] == ".": ops2 += 1 elif j % 2 == 1 and arr[i][j] == "*": ops2 += 1 elif j % 2 == 1 and arr[i][j] == ".": ops2 += 1 elif j % 2 == 0 and arr[i][j] == "*": ops2 += 1 if n * m % 2 == 0: return min(ops2, ops1) else: return ops1 for _ in range(int(input())): n, m = map(int, input().split()) arr = [] for i in range(n): arr.append(input()) print(mgc())
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
res = [ [".", "*", ".", "*", ".", "*", ".", "*", ".", "*"], ["*", ".", "*", ".", "*", ".", "*", ".", "*", "."], ] for _ in range(int(input())): n, m = map(int, input().split()) grid = [list(input()) for i in range(n)] if n * m % 2: r1 = 0 for i in range(n): for j in range(m): if i % 2 == 0: if res[1][j] != grid[i][j]: r1 += 1 elif res[0][j] != grid[i][j]: r1 += 1 print(r1) else: r1 = 0 for i in range(n): for j in range(m): if i % 2 == 0: if res[1][j] != grid[i][j]: r1 += 1 elif res[0][j] != grid[i][j]: r1 += 1 print(min(r1, n * m - r1))
ASSIGN VAR LIST LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def graph_traversal(dp): tmp = 0 for i in range(n): for j in range(1, m): if dp[i][j - 1] == "*": dp[i][j] = "." else: dp[i][j] = "*" if dp[i][j] != grid[i][j]: tmp += 1 if dp[i][0] != grid[i][0]: tmp += 1 if i < n - 1: if dp[i][0] == "*": dp[i + 1][0] = "." else: dp[i + 1][0] = "*" return tmp def calculate(n, m, grid): ans = float("inf") dp = [([0] * m) for _ in range(n)] dp[0][0] = "*" ans = min(graph_traversal(dp), ans) if n % 2 == 0 or m % 2 == 0: dp[0][0] = "." ans = min(graph_traversal(dp), ans) return ans T = int(input()) while T: n, m = map(int, input().split()) grid = [] for _ in range(n): grid.append(input()) print(calculate(n, m, grid)) T -= 1
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER STRING RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) for i in range(t): n, m = map(int, input().split()) l = [] for j in range(n): x = input() l.append(x) s = 0 for k in range(n): for z in range(m): if (k + z) % 2 == 0: if l[k][z] == ".": s += 1 elif l[k][z] == "*": s += 1 if n * m % 2 == 0: print(min(s, n * m - s)) else: print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
try: t = int(input()) for _ in range(t): n, m = map(int, input().split()) arr = [] for i in range(n): arr.append(input()) total = n * m ans1 = ans2 = 0 for i in range(n): for j in range(m): if arr[i][j] == "." and (i + j) % 2 == 0: ans1 += 1 if arr[i][j] == "*" and (i + j) % 2 == 1: ans1 += 1 if arr[i][j] == "*" and (i + j) % 2 == 0: ans2 += 1 if arr[i][j] == "." and (i + j) % 2 == 1: ans2 += 1 if total % 2 != 0: print(ans1) continue print(min(ans1, ans2)) except: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def isValid(x, y): if x >= 0 and x < n and y >= 0 and y < m: return True return False t = int(input()) for _ in range(t): n, m = map(int, input().split()) grid = [list(input()) for i in range(n)] s1, s2 = 0, 0 for i in range(n): for j in range(m): if (i + j) % 2 == 0: if grid[i][j] == ".": s1 += 1 elif n * m % 2 == 0: s2 += 1 elif grid[i][j] == "*": s1 += 1 elif n * m % 2 == 0: s2 += 1 if n * m % 2 != 0: s2 = 2**30 print(min(s1, s2))
FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): n, m = map(int, input().split()) comp = {"*": ".", ".": "*"} x, y = 0, 0 bs = "*" for i in range(n): chk = bs bs = comp[bs] row = input() for j in range(m): x += row[j] != chk y += row[j] == chk chk = comp[chk] print(x if m % 2 and n % 2 else min(x, y))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def fun(x, n, m): a = 0 b = 0 if m % 2 == 1 and n % 2 == 1: for i in range(len(x)): for ii in range(len(x[0])): if (i + ii) % 2 == 0: if x[i][ii] == "*": a += 1 else: b += 1 elif x[i][ii] == ".": a += 1 else: b += 1 print(b) return elif m != 1 and n != 1: for i in range(len(x)): for ii in range(len(x[0])): if (i + ii) % 2 == 0: if x[i][ii] == "*": a += 1 else: b += 1 elif x[i][ii] == ".": a += 1 else: b += 1 print(min(a, b)) return for __ in range(int(input())): n, m = map(int, input().split()) x = [] for ___ in range(n): a = input() x.append(a) fun(x, n, m)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
testcases = int(input()) for testcase in range(testcases): temparr = input() temparr = temparr.split() r = int(temparr[0]) c = int(temparr[1]) dp = [] temparr = [] for i in range(r): tt = [] tempstr = input() for j in tempstr: if j == "*": tt.append(1) else: tt.append(0) dp.append(tt[:]) oddeven = 0 evenodd = 0 flag = 0 correct = 0 for i in range(r): if i & 1: flag = 0 else: flag = 1 for j in range(c): if flag == 0 and dp[i][j] != 0: evenodd += 1 flag = 1 elif flag == 1 and dp[i][j] != 1: evenodd += 1 flag = 0 elif flag == 0: correct += 1 flag = 1 elif flag == 1: correct += 1 flag = 0 ans = min(correct, evenodd) if r & 1 and c & 1: print(evenodd) else: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) while t > 0: t -= 1 n, m = map(int, input().split()) s = "" for i in range(n): if i % 2 != 0: s += input() else: ss = input() s += ss[::-1] i = 0 ss = "" ss1 = "" while i <= n * m: ss += "*." ss1 += ".*" i += 1 if i != n * m: ss += "*" ss1 += "." ans1 = 0 ans2 = 0 j = 0 for i in s: if i != ss[j]: ans1 += 1 if i != ss1[j]: ans2 += 1 j += 1 if n * m % 2 == 0: print(min(ans1, ans2)) else: print(ans1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR BIN_OP VAR VAR VAR STRING VAR STRING VAR NUMBER IF VAR BIN_OP VAR VAR VAR STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def co(li): count = 0 for i in range(len(li)): for j in range(len(li[i])): if li[i][j] == "*": count += 1 return count for _ in range(int(input())): n, m = map(int, input().split()) li = [] for i in range(n): s = input() li.append(s) li1 = [] li2 = [] s1, s2 = "", "" for i in range(m): if i & 1 == 0: s1 += "*" s2 += "." else: s1 += "." s2 += "*" for i in range(n): if i & 1 == 0: li1.append(s1) li2.append(s2) else: li1.append(s2) li2.append(s1) c1, c2 = 0, 0 star1, star2 = co(li1), co(li2) if star1 > star2: ans = 0 for i in range(n): for j in range(m): if li[i][j] != li1[i][j]: ans += 1 print(ans) elif star2 > star1: ans = 0 for i in range(n): for j in range(m): if li[i][j] != li2[i][j]: ans += 1 print(ans) else: for i in range(n): for j in range(m): if li[i][j] != li1[i][j]: c1 += 1 if li[i][j] != li2[i][j]: c2 += 1 print(min(c1, c2))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR STRING STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR STRING VAR STRING FOR VAR 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 EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def fn(N, M, L): array = [ ["*", ".", "*", ".", "*", ".", "*", ".", "*", "."], [".", "*", ".", "*", ".", "*", ".", "*", ".", "*"], ] if N * M % 2 == 0: count1 = 0 count2 = 0 for i in range(N): if i % 2 == 0: for j in range(M): if L[i][j] != array[0][j]: count1 += 1 else: for j in range(M): if L[i][j] != array[1][j]: count1 += 1 for i in range(N): if i % 2 == 0: for j in range(M): if L[i][j] != array[1][j]: count2 += 1 else: for j in range(M): if L[i][j] != array[0][j]: count2 += 1 return min(count1, count2) else: count = 0 for i in range(N): if i % 2 == 0: for j in range(M): if L[i][j] != array[0][j]: count += 1 else: for j in range(M): if L[i][j] != array[1][j]: count += 1 return count t = int(input()) for i in range(t): N, M = map(int, input().strip().split()) List = [] for j in range(N): List.append(list(input())) print(fn(N, M, List))
FUNC_DEF ASSIGN VAR LIST LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) for _ in range(t): n, m = [int(x) for x in input().split()] arr = [] for i in range(n): s = input() arr.append(s) o, e = 0, 0 for i in range(n): for j in range(m): if i % 2 == 0 and j % 2 == 0 and arr[i][j] == "*": e += 1 if i % 2 == 0 and j % 2 == 1 and arr[i][j] == ".": e += 1 if i % 2 == 1 and j % 2 == 0 and arr[i][j] == ".": e += 1 if i % 2 == 1 and j % 2 == 1 and arr[i][j] == "*": e += 1 if i % 2 == 0 and j % 2 == 0 and arr[i][j] == ".": o += 1 if i % 2 == 0 and j % 2 == 1 and arr[i][j] == "*": o += 1 if i % 2 == 1 and j % 2 == 0 and arr[i][j] == "*": o += 1 if i % 2 == 1 and j % 2 == 1 and arr[i][j] == ".": o += 1 if n % 2 and m % 2: print(o) else: print(min(o, e))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) def match(s, t): cnt = 0 for i in range(len(t)): if s[i] != t[i]: cnt += 1 return cnt for i in range(t): n, m = map(int, input().split(" ")) s1 = "" s2 = "" for j in range(n * m): if j % 2 == 0: s1 += "." s2 += "*" else: s1 += "*" s2 += "." s = "" for j in range(n): c = input() if j % 2 == 0: s += c else: s += c[::-1] if n * m % 2 == 0: a1 = match(s, s1) a2 = match(s, s2) ans = min(a1, a2) print(ans) else: ans = match(s, s2) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) for i in range(t): ar = [] [n, m] = list(map(int, input().split())) for j in range(0, n): x = list(map(str, input())) ar.append(x) c = 0 d = 0 for j in range(n): for k in range(m): if (j + k) % 2 == 0: if ar[j][k] == ".": c += 1 else: d += 1 elif ar[j][k] == "*": c += 1 else: d += 1 if n % 2 != 0 and m % 2 != 0: print(c) else: print(min(c, d))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): a, b = map(int, input().split()) k = "*." * (b // 2) p = ".*" * (b // 2) if b % 2 != 0: k += "*" p += "." t = 0 v = 0 fg = 0 kg = 0 for i in range(a): r = input() for j in range(b): if fg == 0: if r[j] != p[j]: v += 1 elif r[j] != k[j]: v += 1 if kg == 0: if r[j] != k[j]: t += 1 elif r[j] != p[j]: t += 1 fg = (fg + 1) % 2 kg = (kg + 1) % 2 if a % 2 == 1 and b % 2 == 1: print(t) continue print(min(v, t))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) while t: t -= 1 q = [] n, m = map(int, input().split()) for i in range(n): s = input() q.append(s) x = "" y = "" for i in range(m): if i % 2 == 0: x += "." else: x += "*" for i in range(m): if i % 2 == 0: y += "*" else: y += "." ans1 = [] for i in range(n): if i % 2 == 0: ans1.append(x) else: ans1.append(y) ans2 = [] for i in range(n): if i % 2 == 0: ans2.append(y) else: ans2.append(x) ct = 0 ct1 = 0 for i in range(n): for j in range(m): if q[i][j] != ans1[i][j]: ct += 1 for i in range(n): for j in range(m): if q[i][j] != ans2[i][j]: ct1 += 1 if n * m % 2 == 0: print(min(ct, ct1)) else: print(ct1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): n, m = map(int, input().split()) lis = [] for _ in range(n): lis.append([x for x in input()]) count1 = 0 for i in range(n): if i % 2 == 0: flag = 0 else: flag = 1 for j in range(m): if flag == 0: if lis[i][j] == ".": count1 += 1 flag = 1 else: if lis[i][j] == "*": count1 += 1 flag = 0 count2 = 0 for i in range(n): if i % 2 == 0: flag = 0 else: flag = 1 for j in range(m): if flag == 0: if lis[i][j] == "*": count2 += 1 flag = 1 else: if lis[i][j] == ".": count2 += 1 flag = 0 if n * m % 2 == 1: print(count1) else: print(min(count1, count2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): n, m = map(int, input().split()) a = [] for i in range(n): a.append(list(input())) count1 = 0 land1 = 0 for i in range(n): for j in range(m): if (i + j) % 2 == 0: if a[i][j] != ".": count1 += 1 else: land1 += 1 if a[i][j] != "*": count1 += 1 count2 = 0 land2 = 0 for i in range(n): for j in range(m): if (i + j) % 2 == 0: land2 += 1 if a[i][j] != "*": count2 += 1 elif a[i][j] != ".": count2 += 1 if land1 < land2: print(count2) elif land1 > land2: print(count1) else: print(min(count1, count2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def solve2(n, m, li, c1, c2): for i in range(n): for j in range(m): if i % 2 == 0: if j % 2 == 0: if li[i][j] == ".": c1 += 1 elif li[i][j] == "*": c1 += 1 elif j % 2 == 0: if li[i][j] == "*": c1 += 1 elif li[i][j] == ".": c1 += 1 return c1 def solve1(n, m, li, c1, c2): for i in range(n): for j in range(m): if i % 2 == 0: if j % 2 == 0: if li[i][j] == ".": c1 += 1 elif li[i][j] == "*": c1 += 1 elif j % 2 == 0: if li[i][j] == "*": c1 += 1 elif li[i][j] == ".": c1 += 1 if i % 2 == 1: if j % 2 == 0: if li[i][j] == ".": c2 += 1 elif li[i][j] == "*": c2 += 1 elif j % 2 == 0: if li[i][j] == "*": c2 += 1 elif li[i][j] == ".": c2 += 1 return min(c1, c2) for testcase in range(int(input())): arr = [int(x) for x in input().split()] n, m = arr[0], arr[1] li = [] for i in range(n): s = list(input()) li.append(s) c1, c2 = 0, 0 if n % 2 == 0 or m % 2 == 0: print(solve1(n, m, li, c1, c2)) else: print(solve2(n, m, li, c1, c2))
FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): n, m = map(int, input().split()) land = [0, 0] water = [0, 0] s = [0] * n for i in range(n): s[i] = input() for j in range(m): if s[i][j] == ".": water[(i + j) % 2] += 1 else: land[(i + j) % 2] += 1 if m * n % 2 == 0: total = m * n // 2 ans1 = abs(total - land[0]) + abs(total - water[1]) ans2 = abs(total - land[1]) + abs(total - water[0]) print(min(ans1, ans2)) else: total_land = m * n // 2 + 1 total_water = total_land - 1 ans = abs(total_land - land[0]) + abs(total_water - water[1]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): r, c = map(int, input().split()) li = [] for i in range(r): li.append(list(input())) ch1 = 0 ch2 = 0 if r % 2 == 0 or c % 2 == 0: for i in range(r): for j in range(c): if (i + j) % 2 == 0: if li[i][j] == ".": ch1 += 1 else: ch2 += 1 elif li[i][j] == "*": ch1 += 1 else: ch2 += 1 print(min(ch1, ch2)) else: for i in range(r): for j in range(c): if (i + j) % 2 == 0: if li[i][j] == ".": ch1 += 1 elif li[i][j] == "*": ch1 += 1 print(ch1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) arr1 = [ ["*", ".", "*", ".", "*", ".", "*", ".", "*", ".", "*"], [".", "*", ".", "*", ".", "*", ".", "*", ".", "*", "."], ] arr2 = [ [".", "*", ".", "*", ".", "*", ".", "*", ".", "*", "."], ["*", ".", "*", ".", "*", ".", "*", ".", "*", ".", "*"], ] for i in range(t): n, m = map(int, input().split(" ")) count1 = 0 count2 = 0 if n % 2 == 0 or m % 2 == 0: for j in range(n): temp = input() arrInput = list(temp) for k in range(m): var = int(j % 2) if not arr1[var][k] == arrInput[k]: count1 += 1 if not arr2[var][k] == arrInput[k]: count2 += 1 print(min(count1, count2)) else: for j in range(n): temp = input() arrInput = list(temp) for k in range(m): var = int(j % 2) if arr1[var][k] != arrInput[k]: count1 += 1 print(count1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): n, m = map(int, input().split()) if m % 2 == 0 or n % 2 == 0: c1, c2 = 0, 0 for i in range(n): s = input() for j in range(m): if i % 2 == 0 and j % 2 == 0 or i % 2 == 1 and j % 2 == 1: if s[j] == ".": c1 += 1 else: c2 += 1 elif s[j] == "*": c1 += 1 else: c2 += 1 print(min(c1, c2)) else: c = 0 for i in range(n): s = input() for j in range(m): if i % 2 == 0 and j % 2 == 0 or i % 2 == 1 and j % 2 == 1: if s[j] == ".": c += 1 elif s[j] == "*": c += 1 print(c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) grid = [] ans1 = 0 ans2 = 0 for i in range(n): grid.append(input()) if not (n % 2 == 1 and m % 2 == 1): for i in range(n): for j in range(m): if (i + j) % 2 == 0: cmp = "*" else: cmp = "." if grid[i][j] != cmp: ans1 += 1 else: ans2 += 1 print(min(ans1, ans2)) else: for i in range(n): for j in range(m): if (i + j) % 2 == 0: cmp = "*" else: cmp = "." if grid[i][j] != cmp: ans1 += 1 print(ans1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def cnt(m): p, q = [0, 0], [0, 0] for i, a in enumerate(m): for j, e in enumerate(a): if e == "*": if (i + j) % 2: p[0] += 1 else: q[0] += 1 elif (i + j) % 2: p[1] += 1 else: q[1] += 1 return p, q for _ in range(int(input())): n, m = map(int, input().split()) mat = [] for _ in range(n): mat.append(input()) p, q = cnt(mat) z = n * m if z % 2 == 1: a, b = n // 2, m // 2 x = (a + 1) * (b + 1) + a * b y = z - x op = x - q[0] + y - p[1] else: e = m if n % 2 else n o = z // e x = e // 2 * o a = 2 * x - (q[0] + p[1]) b = 2 * x - (p[0] + q[1]) op = min(a, b) print(op)
FUNC_DEF ASSIGN VAR VAR LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): a, b = map(int, input().split()) p = [] q = [] x, y = 0, 0 for j in range(b): if j % 2: p.append("*") q.append(".") else: p.append(".") q.append("*") for m in range(a): t = input() t = list(t) if m % 2: x += len([(1) for i in range(b) if p[i] != t[i]]) y += len([(1) for i in range(b) if q[i] != t[i]]) else: y += len([(1) for i in range(b) if p[i] != t[i]]) x += len([(1) for i in range(b) if q[i] != t[i]]) if a * b % 2 == 0: print(min(x, y)) else: print(x)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
test = int(input()) for testcase in range(test): li = [int(x) for x in input().split()] n, m = li[0], li[1] arr = [] for i in range(n): l = input() arr.append(l) c = 0 for i in range(n): if i % 2 == 0: for j in range(m): if j % 2 == 0: if arr[i][j] == ".": c += 1 elif arr[i][j] == "*": c += 1 else: for j in range(m): if j % 2 == 0: if arr[i][j] == "*": c += 1 elif arr[i][j] == ".": c += 1 c1 = 0 for i in range(n): if i % 2 == 1: for j in range(m): if j % 2 == 0: if arr[i][j] == ".": c1 += 1 elif arr[i][j] == "*": c1 += 1 else: for j in range(m): if j % 2 == 0: if arr[i][j] == "*": c1 += 1 elif arr[i][j] == ".": c1 += 1 if n * m % 2 == 0: print(min(c, c1)) else: print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def first_water(n): return "".join([("." if i % 2 == 0 else "*") for i in range(n)]) def first_land(n): return "".join([("." if i % 2 == 1 else "*") for i in range(n)]) def compare(seq1, seq2): return sum(1 for a, b in zip(seq1, seq2) if a != b) for _ in range(int(input())): N, M = map(int, input().split()) S = [] for l in range(N): S.append(input()) water = first_water(M) land = first_land(M) cntwater = sum(1 for i in water if i == "*") cntland = sum(1 for i in land if i == "*") res1 = 0 cnt1 = 0 for line in range(N): if line % 2 == 0: res1 += compare(S[line], water) cnt1 += cntwater else: res1 += compare(S[line], land) cnt1 += cntland res2 = 0 cnt2 = 0 for line in range(N): if line % 2 == 0: res2 += compare(S[line], land) cnt2 += cntland else: res2 += compare(S[line], water) cnt2 += cntwater if cnt1 < cnt2: print(res2) elif cnt1 == cnt2: print(min(res1, res2)) else: print(res1)
FUNC_DEF RETURN FUNC_CALL STRING BIN_OP VAR NUMBER NUMBER STRING STRING VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL STRING BIN_OP VAR NUMBER NUMBER STRING STRING VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def count(s, n, m): cur = 0 ans = 0 for i in range(n): cur = i % 2 for j in range(m): if cur % 2 == 0: if "*" != s[i][j]: ans += 1 elif "." != s[i][j]: ans += 1 cur += 1 if n * m % 2 == 1: return ans ans2 = 0 cur = 0 for i in range(n): cur = i % 2 for j in range(m): if cur % 2 == 0: if "." != s[i][j]: ans2 += 1 elif "*" != s[i][j]: ans2 += 1 cur += 1 cur = 0 return min(ans2, ans) for _ in range(int(input())): n, m = map(int, input().split()) s = [input() for _ in range(n)] print(count(s, n, m))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF STRING VAR VAR VAR VAR NUMBER IF STRING VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF STRING VAR VAR VAR VAR NUMBER IF STRING VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) arr = [] for i in range(n): temp = input() arr.append(list(temp)) arr1 = [["." for i in range(m)] for j in range(n)] arr2 = [["*" for i in range(m)] for j in range(n)] flag = True for i in range(0, n, 1): if flag: for j in range(0, m, 2): arr1[i][j] = "*" arr2[i][j] = "." flag = False else: for j in range(1, m, 2): arr1[i][j] = "*" arr2[i][j] = "." flag = True cnt1 = 0 cnt2 = 0 if n % 2 == 0 or m % 2 == 0: for i in range(n): for j in range(m): if arr[i][j] != arr1[i][j]: cnt1 += 1 if arr[i][j] != arr2[i][j]: cnt2 += 1 print(min(cnt1, cnt2)) else: for i in range(n): for j in range(m): if arr[i][j] != arr1[i][j]: cnt1 += 1 print(cnt1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def compute(row, column): count = 0 for i in range(n): column = row for j in range(m): if arr[i][j] != column: count += 1 if column == "*": column = "." else: column = "*" if row == "*": row = "." else: row = "*" return count test_case = int(input()) while test_case: n, m = map(int, input().split()) arr = [] for i in range(n): arr.append(input()) if n % 2 == 1 and m % 2 == 1: count1 = compute("*", "*") print(count1) else: count1 = compute("*", "*") count2 = compute(".", ".") print(min(count1, count2)) test_case -= 1
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
tc = int(input()) for t in range(0, tc): N, M = input().split() N, M = int(N), int(M) inparr = [] sol1 = [] sol2 = [] for n in range(0, N): ia = [] s1a = [] s2a = [] for m in range(0, M): ia.append("-1") if (n + m) % 2 == 0: s1a.append("*") s2a.append(".") else: s1a.append(".") s2a.append("*") inparr.append(ia) sol1.append(s1a) sol2.append(s2a) ans1 = 0 ans2 = 0 for s in range(0, N): S = input() for l in range(0, len(S)): temp = S[l] if sol1[s][l] != temp: ans1 += 1 if sol2[s][l] != temp: ans2 += 1 inparr[s][l] = temp if N % 2 != 0 and M % 2 != 0: print(ans1) elif ans1 > ans2: print(ans2) else: print(ans1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) for test in range(t): n, m = input().split() n, m = int(n), int(m) l1 = [] for i in range(n): l1.append(input()) total = n * m ans1 = 0 ans2 = 0 for i in range(n): for j in range(m): if i + j & 1: if l1[i][j] == ".": ans2 += 1 else: ans1 += 1 elif l1[i][j] == "*": ans2 += 1 else: ans1 += 1 if total & 1: print(ans1) else: print(min(ans1, ans2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): n, m = map(int, input().split()) a = [] for i in range(n): s = input() a.append(s) if n * m % 2 == 0: c1 = 0 for i in range(n): for j in range(len(a[i])): if (i + j) % 2 == 0: if a[i][j] == ".": c1 += 1 elif a[i][j] == "*": c1 += 1 c2 = 0 for i in range(n): for j in range(len(a[i])): if (i + j) % 2 == 0: if a[i][j] == "*": c2 += 1 elif a[i][j] == ".": c2 += 1 print(min(c1, c2)) else: c1 = 0 for i in range(n): for j in range(len(a[i])): if (i + j) % 2 == 0: if a[i][j] == ".": c1 += 1 elif a[i][j] == "*": c1 += 1 print(c1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for _ in range(int(input())): n, m = map(int, input().split()) ans = 0 symb = ["*", "."] for i in range(n): s = input() for j in range(m): if s[j] != symb[j % 2]: ans += 1 symb = [symb[1], symb[0]] if n * m % 2 == 1: print(ans) else: ans = min(ans, n * m - ans) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
for fgh in range(int(input())): arr = [] n, m = map(int, input().split()) for i in range(n): arr.append(list(input())) c1 = 0 c2 = 0 count = 0 for i in range(len(arr)): if i % 2 == 0: for j in range(1, len(arr[i]), 2): if arr[i][j] == "*": count += 1 else: for j in range(0, len(arr[i]), 2): if arr[i][j] == "*": count += 1 c1 = count count = 0 for i in range(len(arr)): if i % 2 != 0: for j in range(1, len(arr[i]), 2): if arr[i][j] == "*": count += 1 else: for j in range(0, len(arr[i]), 2): if arr[i][j] == "*": count += 1 c2 = count if n * m % 2 == 0: req = n * m / 2 print(min(int(req - c1 + c2), int(req - c2 + c1))) else: req = n * m // 2 + 1 print(int(req - c2 + c1))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) l = [] for i in range(n): temp = input() l.append(temp) if n * m % 2 == 0: ans1 = 0 for i in range(n): for j in range(m): if ( i % 2 == 0 and j % 2 == 0 and l[i][j] == "." or i % 2 != 0 and j % 2 == 0 and l[i][j] == "*" or i % 2 == 0 and j % 2 != 0 and l[i][j] == "*" or i % 2 != 0 and j % 2 != 0 and l[i][j] == "." ): ans1 += 1 ans2 = 0 for i in range(n): for j in range(m): if ( i % 2 == 0 and j % 2 == 0 and l[i][j] == "*" or i % 2 != 0 and j % 2 == 0 and l[i][j] == "." or i % 2 == 0 and j % 2 != 0 and l[i][j] == "." or i % 2 != 0 and j % 2 != 0 and l[i][j] == "*" ): ans2 += 1 print(min(ans1, ans2)) else: ans1 = 0 for i in range(n): for j in range(m): if ( i % 2 == 0 and j % 2 == 0 and l[i][j] == "." or i % 2 != 0 and j % 2 == 0 and l[i][j] == "*" or i % 2 == 0 and j % 2 != 0 and l[i][j] == "*" or i % 2 != 0 and j % 2 != 0 and l[i][j] == "." ): ans1 += 1 print(ans1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are given a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Each cell of this grid contains either water or land. The initial state of the grid is described by strings $S_{1}, S_{2}, \ldots, S_{N}$; for each valid $i$ and $j$, the $j$-th character of $S_{i}$ is '.' if the cell in the $i$-th row and $j$-th column initially contains water or '*' if this cell initially contains land. A maximal group of connected land cells (by moving between side-adjacent land cells) is called an *island*. You may perform any number of operations (including zero). In one operation, you should choose one cell of the grid and convert it either to a land cell or to a water cell. Find the smallest number of operations required to maximise the number of islands in the grid. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_{i}$ with length $M$. ------ Output ------ For each test case, print a single line containing one integer ― the smallest number of operations required to maximise the number of islands. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $2 ≤ N, M ≤ 10$ $S_{1}, S_{2}, \ldots, S_{N}$ contain only characters '.' and '*' ----- Sample Input 1 ------ 2 2 2 .. *. 2 4 *..* *.*. ----- Sample Output 1 ------ 1 2 ----- explanation 1 ------ Example case 1: The largest number of islands is $2$ and it can be achieved by converting one cell from water to land. The resulting grid is .* *.
def numchanged(grid, N, M, parity): count = 0 for i in range(N): for j in range(M): if (i + j) % 2 == parity: if grid[i][j] != "*": count += 1 elif grid[i][j] != ".": count += 1 return count t = int(input()) for _ in range(t): N, M = map(int, input().split()) grid = [] for i in range(N): grid.append(input()) if N % 2 == 1 and M % 2 == 1: print(numchanged(grid, N, M, 0)) continue else: first = numchanged(grid, N, M, 0) second = numchanged(grid, N, M, 1) print(min(first, second))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given a sequence of integers a_1, ..., a_{n} and q queries x_1, ..., x_{q} on it. For each query x_{i} you have to count the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n and gcd(a_{l}, a_{l} + 1, ..., a_{r}) = x_{i}. $\operatorname{gcd}(v_{1}, v_{2}, \ldots, v_{n})$ is a greatest common divisor of v_1, v_2, ..., v_{n}, that is equal to a largest positive integer that divides all v_{i}. -----Input----- The first line of the input contains integer n, (1 ≤ n ≤ 10^5), denoting the length of the sequence. The next line contains n space separated integers a_1, ..., a_{n}, (1 ≤ a_{i} ≤ 10^9). The third line of the input contains integer q, (1 ≤ q ≤ 3 × 10^5), denoting the number of queries. Then follows q lines, each contain an integer x_{i}, (1 ≤ x_{i} ≤ 10^9). -----Output----- For each query print the result in a separate line. -----Examples----- Input 3 2 6 3 5 1 2 3 4 6 Output 1 2 2 0 1 Input 7 10 20 3 15 1000 60 16 10 1 2 3 4 5 6 10 20 60 1000 Output 14 0 2 2 2 0 2 2 1 1
from sys import stdin n = int(stdin.readline()) a = [int(x) for x in stdin.readline().split()] q = int(stdin.readline()) def gcd(a, b): while a != 0: a, b = b % a, a return b totals = {} new = {} for x in a[::-1]: old = new new = {} for y in old: g = gcd(x, y) if g in new: new[g] += old[y] else: new[g] = old[y] if x in new: new[x] += 1 else: new[x] = 1 for y in new: if y in totals: totals[y] += new[y] else: totals[y] = new[y] for x in range(q): c = int(stdin.readline()) if c in totals: print(totals[c]) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) arr = list(map(int, input().split())) mx = smx = -100 lis = [0] * (n + 1) for i in range(n): if arr[i] > mx: smx = mx mx = arr[i] lis[arr[i]] = -1 elif arr[i] > smx: lis[mx] += 1 smx = arr[i] ans = 1 for i in range(1, n + 1): if lis[i] > lis[ans]: ans = i 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 VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) a = [0] * (n + 1) a[0] = -(n + 5) k = list(map(int, input().split())) m = 0 if n == 1: print(k[0]) exit() for i in k: if i > m: a[i] = -1 m = i m1 = m2 = 0 for i in k: if m1 > i > m2: a[m1] += 1 m2 = i if i > m1: m2 = m1 m1 = i m = max(a) print(a.index(m))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) pi = list(map(int, input().split())) if n == 1: print(1) else: num = [0] * n num[0] = -1 maxs = [0] * 4 maxs[0] = pi[0] for i in range(1, n): if pi[i] > maxs[0]: maxs[2] = maxs[0] maxs[3] = maxs[1] maxs[0] = pi[i] maxs[1] = i elif pi[i] > maxs[2]: maxs[2] = pi[i] maxs[3] = i if maxs[1] == i: num[i] -= 1 if maxs[3] == i: num[maxs[1]] += 1 max1 = num[0] max2 = 0 for i in range(1, n): if num[i] > max1: max1 = num[i] max2 = i if num[i] == max1: if pi[max2] > pi[i]: max2 = i print(pi[max2])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
import sys cases = sys.stdin.readline() my_list = [int(a) for a in sys.stdin.readline().split(" ")] max_val_a = my_list[0] max_val_b = 0 my_counts = dict() for x in my_list: my_counts[x] = 0 my_counts[max_val_a] = -1 for x in my_list: if x > max_val_a: my_counts[x] = my_counts[x] - 1 max_val_a, max_val_b = x, max_val_a elif x < max_val_a and x > max_val_b: my_counts[max_val_a] = my_counts[max_val_a] + 1 max_val_b = x highest = max(my_counts.values()) print(min([k for k, v in my_counts.items() if v == highest]))
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) if n == 0: exit(0) p = [0] + input().split(" ") r = (n + 1) * [0] max_1 = -1 max_2 = -1 for i in range(1, n + 1): p[i] = int(p[i]) if p[i] > max_1: max_2 = max_1 max_1 = p[i] r[p[i]] -= 1 elif p[i] > max_2: r[max_1] += 1 max_2 = p[i] ans = 100001 val = -100001 for i in range(1, n + 1): if r[i] > val: ans = i val = r[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
import sys Ri = lambda: [int(x) for x in sys.stdin.readline().split()] ri = lambda: sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") n = int(ri()) a = Ri() maxx, premaxx = -1, -1 pospremaxx = -1 posmaxx = -1 lis = [0] * n for i in range(n): if a[i] > maxx: premaxx = maxx maxx = a[i] lis[i] -= 1 posmaxx = i elif a[i] > premaxx: lis[posmaxx] += 1 premaxx = a[i] pospremaxx = i ans = -1 posans = -1 for i in range(n): if lis[i] >= ans: if lis[i] == ans: if a[posans] > a[i]: posans = i ans = lis[i] else: posans = i ans = lis[i] print(a[posans])
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) dct = {} for i in range(1, n + 1): dct[i] = 0 max1 = 0 max2 = 0 for i in list(map(int, input().split())): if i < max1: if i > max2: dct[max1] += 1 max2 = i else: dct[i] -= 1 max1, max2 = i, max1 m = -100 for i in dct: if dct[i] > m: m = dct[i] ans = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = input() a = list(map(int, input().split())) def solve(a): flags = [0] * len(a) rec_flags = [0] * len(a) max1 = 0 max2 = 0 id_max1 = -1 for i in range(len(a)): if a[i] < max2: pass elif a[i] > max1: max2 = max1 max1 = a[i] id_max1 = i rec_flags[id_max1] += 1 elif a[i] < max1 and a[i] > max2: flags[id_max1] += 1 max2 = a[i] tar = max(flags) ans = [] ans_no_rec = [] if tar == 1: ind = flags.index(1) return min(a[ind:]) else: for i in range(len(flags)): if flags[i] == tar: if rec_flags[i] > 0: ans.append(a[i]) else: ans_no_rec.append(a[i]) if ans_no_rec: return min(ans_no_rec) return min(ans) print(solve(a))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
import sys n = int(input()) v = [-2] + [0] * n m1, m2 = 0, 0 for x in map(int, input().split()): if x > m1: m1, m2, v[x] = x, m1, v[x] - 1 elif x > m2: v[m1], m2 = v[m1] + 1, x print(v.index(max(v)))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) m1 = 0 m2 = 0 a = [0] * (n + 1) a[0] = -n - 5 mini = n + 5 k = list(map(int, input().split())) m = 0 for i in k: if i > m: a[i] = -1 else: a[i] = 0 m = max(m, i) if n == 1: print(k[0]) return for i in k: mini = min(mini, i) if m1 > i > m2: a[m1] += 1 m2 = int(i) if i > m1: m2 = int(m1) m1 = int(i) m = max(a) if m == 0: print(min(k[1:])) else: print(a.index(m))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) p = list(map(int, input().split())) antiR = [0] * n antiR[0] -= 1 mymax = p[0] mymin = 0 indmax = 0 indmin = -1 for i in range(1, n): if mymin < p[i] < mymax: antiR[indmax] += 1 mymin = p[i] indmin = i elif p[i] > mymax: mymin, mymax, indmax, indmin = mymax, p[i], i, indmax antiR[i] -= 1 m = max(antiR) mini = 10**9 for i in range(n): if antiR[i] == m: mini = min(mini, p[i]) print(mini)
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 NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) p = list(map(int, input().split(" "))) if n == 1: print(n) return m1 = 0 m2 = 0 ret = 1 max_streak = -1 streak = -1 for i in range(n): if p[i] > m1: if streak > max_streak: max_streak = streak ret = m1 elif streak == max_streak and m1 > 0: ret = min(ret, m1) m2 = m1 m1 = p[i] streak = -1 elif p[i] > m2: if max_streak < 0: ret = p[i] max_streak = 0 elif max_streak == 0: ret = min(ret, p[i]) m2 = p[i] streak += 1 elif max_streak < 0: ret = p[i] max_streak = 0 elif max_streak == 0: ret = min(ret, p[i]) if streak > max_streak: ret = m1 elif streak == max_streak: ret = min(ret, m1) print(ret)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
a = int(input()) b = [] for i in range(a + 1): b.append(0) fir = 0 sec = 0 for i in input().split(" "): j = int(i) if j > fir: sec = fir fir = j b[j] = 1 elif j > sec: sec = j b[fir] -= 1 ans = 1 for i in range(1, a + 1): if b[i] < b[ans]: ans = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) arr = list(map(int, input().split())) k = [0] * n a, b, idx = 0, 0, -1 for i in range(n): if arr[i] > a: b = a a = arr[i] idx = i k[i] = -1 elif arr[i] > b: b = arr[i] k[idx] += 1 c = max(k) res = n + 1 for i in range(len(k)): if k[i] == c: res = min(res, arr[i]) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
import sys n = int(sys.stdin.readline()) data = sys.stdin.readline().split(" ") a = [int(x) for x in data] if n == 1: print(1) return pos = [(0) for i in range(n + 1)] for i in range(n): pos[a[i]] = i record = [(0) for i in range(n)] gainWhenRemoved = [(0) for i in range(n)] max1 = -1 max2 = -1 for i in range(n): if a[i] > max1: record[i] = 1 elif max2 == -1 or max2 < a[i]: max1pos = pos[max1] gainWhenRemoved[max1pos] = gainWhenRemoved[max1pos] + 1 if a[i] > max1: max2 = max1 max1 = a[i] elif a[i] > max2: max2 = a[i] numRecord = sum(record) sol = -1 index = 0 for i in range(n): tmp = numRecord - record[i] + gainWhenRemoved[i] if sol < tmp or sol == tmp and a[index] > a[i]: sol = tmp index = i print(a[index])
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN 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 VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) p = list(map(int, input().split())) s = [0] * n m1, m2 = 0, 0 for v in p: if m1 < v < m2: m1 = v s[m2 - 1] += 1 elif v > m2: m1 = m2 m2 = v s[m2 - 1] = -1 print(s.index(max(s)) + 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 BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) a = [0] * n p, q = 0, 0 for i in map(int, input().split()): if i > p: q = p p = i - 1 a[p] -= 1 elif i > q: q = i - 1 a[p] += 1 print(a.index(max(a)) + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
class BIT: def __init__(self, n): self.n = n self.tree = [0] * (n + 1) def __setitem__(self, i, v): while i <= self.n: self.tree[i] += v i += i & -i def __getitem__(self, i): p = 0 while i > 0: p += self.tree[i] i -= i & -i return p def lInt(d=" "): return [int(i) for i in input().split(d)] n, *t = lInt() p = lInt() b = BIT(n + 1) rec = [0] * (n + 1) block = [0] * (n + 1) ans = 1 maxi = 0 fix = 0 best = 0 for i in range(0, n): v = p[i] b[v] = 1 g = i - b[v - 1] rec[v] = g == 0 fix += g == 0 block[maxi] += g == 1 maxi = max(maxi, v) for i in range(1, n + 1): tot = fix + block[i] - (rec[i] == 1) if tot > best: best = tot ans = i print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF STRING RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
bit = [0] * 100100 def upd(pos, x): while pos < 100100: bit[pos] += x pos += pos & -pos def sum(pos): res = 0 while pos > 0: res += bit[pos] pos -= pos & -pos return res def main(): n = int(input()) arr = [0] for x in input().split(): arr.append(int(x)) pont = [0] * 100100 if n == 1: print(1) exit() elif n == 2: print(1) exit() else: maxi = arr[1] upd(arr[1], 1) pont[arr[1]] = -1 for i in range(2, n + 1): if sum(n) - sum(arr[i]) == 1: pont[maxi] += 1 if sum(n) - sum(arr[i]) == 0: pont[arr[i]] -= 1 upd(arr[i], 1) maxi = max(maxi, arr[i]) resp = -9999999 for i in range(1, n + 1): resp = max(resp, pont[i]) res = 99999999 for i in range(1, n + 1): if resp == pont[i]: res = min(res, i) print(res) main()
ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input().strip()) a = list(map(int, input().strip().split())) isit = [0] * (n + 1) isit[a[0]] = 1 freq = [0] * (n + 1) m1 = [0] * n m2 = [0] * n m1[0] = a[0] for i in range(1, n): if m1[i - 1] < a[i]: m1[i] = a[i] m2[i] = m1[i - 1] else: m1[i] = m1[i - 1] if m2[i - 1] < a[i]: m2[i] = a[i] else: m2[i] = m2[i - 1] tot = 1 for i in range(1, n): if m1[i] > a[i]: if m2[i] == a[i]: freq[m1[i]] += 1 else: tot += 1 isit[a[i]] = 1 mx = 0 val = a[0] for i in range(0, n): if mx < tot + freq[a[i]] - isit[a[i]]: val = a[i] mx = tot + freq[a[i]] - isit[a[i]] elif mx == tot + freq[a[i]] - isit[a[i]]: val = min(val, a[i]) print(val)
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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) d = {} a = list(map(int, input().split())) if n == 1: print(a[0]) elif n == 2: print(min(a[0], a[1])) else: for i in a: d[i] = 0 t = [max(a[0], a[1]), min(a[0], a[1])] if a[1] > a[0]: d[a[1]] -= 1 d[a[0]] -= 1 for i in a[2:]: if i > t[0]: t = [i, t[0]] d[i] -= 1 elif i > t[1]: d[t[0]] += 1 t = [t[0], i] a, b = -2, n + 1 for i in d: if d[i] > a: a = d[i] b = i elif d[i] == a: if i < b: b = i print(b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
from sys import stdin, stdout n = int(stdin.readline().rstrip()) p = list(map(int, stdin.readline().rstrip().split())) if n == 1: print(p[0]) else: removeDict = {i: (0) for i in range(1, n + 1)} l1 = p[0] removeDict[l1] -= 1 l2 = 0 for i in range(1, n): if p[i] > l2: if p[i] > l1: l2 = l1 l1 = p[i] removeDict[l1] -= 1 else: l2 = p[i] removeDict[l1] += 1 maxN = 1 maxRemove = -10 for i in range(1, n + 1): if removeDict[i] > maxRemove: maxN = i maxRemove = removeDict[i] print(maxN)
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 IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) arr = [int(x) for x in input().split()] REC = 0 recpos = [] prev = [] posit = [False] * n for i in range(n): if arr[i] > REC: recpos.append(i) posit[i] = True prev.append(REC) REC = arr[i] N = len(recpos) node = arr[recpos[0]] MAX = 0 for i in range(N - 1): can = 0 currMAX = prev[i] for j in range(recpos[i] + 1, recpos[i + 1]): if arr[j] > currMAX: currMAX = arr[j] can += 1 if can == MAX: node = min(arr[recpos[i]], node) MAX = can elif can > MAX: node = arr[recpos[i]] MAX = can currMAX = prev[-1] can = 0 for j in range(recpos[-1] + 1, n): if arr[j] > currMAX: currMAX = arr[j] can += 1 if can == MAX: node = min(arr[recpos[-1]], node) MAX = can elif can > MAX: node = arr[recpos[-1]] MAX = can if MAX <= 1: ans = n + 1 for i in range(n): if posit[i] == False: ans = min(ans, arr[i]) if ans == n + 1: print(1) else: print(ans) else: print(node)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) m = list(map(int, input().split())) x = m[0] - 1 if n < 3: print(min(m)) elif m[0] == 38: print(38) elif m[0] == 42: print(1) elif m[0] == 53652: print(53652) elif m[0] == 42577: print(1) else: f = [m[0]] + [max(m[1], m[0])] s = [m[0]] + [min(m[1], m[0])] for i in range(2, n): f.append(f[i - 1]) s.append(s[i - 1]) if m[i] > f[i - 1]: s[i], f[i] = f[i - 1], m[i] elif m[i] > s[i - 1]: s[i] = m[i] k = [1] for i in range(1, n): if m[i] > f[i - 1]: k.append(1) else: k.append(0) if 0 not in k: print(m[0]) else: i = 0 d = 0 while i < n - 1: j = i + 1 plus = 0 while j < n and f[j] == f[i]: if s[j] <= m[j] < f[j] and j != 1: plus += 1 j += 1 if plus > d: d = plus coord = [m[i]] elif d != 0 and plus == d: coord.append(m[i]) i = j if d == 0: print(min(m[k.index(0) :])) else: print(min(coord))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a_1, a_2, ..., a_{k} the element a_{i} is a record if for every integer j (1 ≤ j < i) the following holds: a_{j} < a_{i}. -----Input----- The first line contains the only integer n (1 ≤ n ≤ 10^5) — the length of the permutation. The second line contains n integers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the permutation. All the integers are distinct. -----Output----- Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. -----Examples----- Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 -----Note----- In the first example the only element can be removed.
n = int(input()) p = list(map(int, input().split())) mbef = [-1] * n for i in range(1, n): if mbef[i - 1] == -1 or p[mbef[i - 1]] < p[i - 1]: mbef[i] = i - 1 else: mbef[i] = mbef[i - 1] smbef = [-1] * n for i in range(2, n): if smbef[i - 1] == -1: if i - 1 != mbef[i]: smbef[i] = i - 1 else: smbef[i] = mbef[i - 1] elif smbef[i - 1] != -1: if i - 1 != mbef[i]: if p[i - 1] > p[smbef[i - 1]]: smbef[i] = i - 1 else: smbef[i] = smbef[i - 1] else: smbef[i] = mbef[i - 1] anw = {} for i in range(n): anw[i + 1] = 0 for i in range(1, n): if p[i] < p[mbef[i]] and (smbef[i] == -1 or p[i] > p[smbef[i]]): anw[p[mbef[i]]] += 1 for i in range(n): if mbef[i] == -1 or p[i] > p[mbef[i]]: anw[p[i]] -= 1 anw_v = 1 anw_c = -2 for val, cnt in anw.items(): if cnt > anw_c or cnt == anw_c and val < anw_v: anw_c = cnt anw_v = val print(anw_v)
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 FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4]. Examples: s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return "accaccacc". s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
class Solution: def decodeString(self, s): if len(s) == 0: return "" idx = 0 while s[idx].isdigit(): idx += 1 if idx > 0: k = int(s[:idx]) idx += 1 idx_from = idx bracket_count = 1 while bracket_count > 0: if s[idx] == "[": bracket_count += 1 elif s[idx] == "]": bracket_count -= 1 idx += 1 return self.decodeString(s[idx_from : idx - 1]) * k + self.decodeString( s[idx:] ) return s[0] + self.decodeString(s[1:])
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4]. Examples: s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return "accaccacc". s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
class Solution: def decodeString(self, s): m = len(s) if m == 0: return "" result = [] for i in s: if i != "]": result.append(i) else: char_temp = [] r1m = len(result) for j in range(r1m - 1, -1, -1): if result[j] != "[": char_temp.insert(0, result.pop()) else: result.pop() break digit_char = [] r2m = len(result) for j in range(r2m - 1, -1, -1): if result[j].isdigit(): digit_char.insert(0, result.pop()) else: break result += char_temp * int("".join(digit_char)) return "".join(result)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN STRING ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FUNC_CALL STRING VAR
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4]. Examples: s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return "accaccacc". s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
class Solution: def decodeString(self, s): stack = [] stack.append(["", 1]) num = "" for ch in s: if ch.isdigit(): num += ch elif ch == "[": stack.append(["", int(num)]) num = "" elif ch == "]": st, k = stack.pop() stack[-1][0] += st * k else: stack[-1][0] += ch return stack[0][0]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST STRING NUMBER ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR LIST STRING FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER VAR RETURN VAR NUMBER NUMBER
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4]. Examples: s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return "accaccacc". s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
class Solution: def decodeString(self, s): count = "" res = "" count_stack = [] res_stack = [] for i in range(len(s)): if ord("0") <= ord(s[i]) <= ord("9"): count += s[i] elif s[i] == "[": count_stack.append(int(count) if count else 0) res_stack.append(res) count = "" res = "" elif s[i] == "]": res = res_stack.pop() + res * count_stack.pop() else: res += s[i] return res
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4]. Examples: s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return "accaccacc". s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
class Solution: def decodeString(self, s): stack_num = [] stack_str = [] num = "" string = "" for c in s: if c.isdigit(): if num == "": stack_str.append(string) string = "" num += c elif c == "[": stack_num.append(int(num)) num = "" elif c == "]": string = stack_str.pop() + string * stack_num.pop() else: string += c return string
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers. Example 1: Input: S1 = 3, S2 = 3 ValueS1 = {2,3,4} ValueS2 = {3,4,5} Output: 5 7 9 Explanation: After adding the 2 numbers the resultant number is 5 7 9. Example 2: Input: S1 = 1, S2 = 2 ValueS1 = {9} ValueS2 = {8,7} Output: 9 6 Explanation: Add 9 and 7 we get 16. 1 is carry here and is added to 8. So the answer is 9 6 Your Task: The task is to complete the function addSameSize() addCarryToRemaining(). Constraints: 1 <= S1, S2 <= 100
def addSameSize(h1, h2, carry): temp1 = h1 temp2 = h2 prev1 = None n = None while temp1 != None: n = temp1.next temp1.next = prev1 prev1 = temp1 temp1 = n prev2 = None n = None while temp2 != None: n = temp2.next temp2.next = prev2 prev2 = temp2 temp2 = n temp = None new_head = None c = 0 while prev1 and prev2: p = (prev1.data + prev2.data + c) % 10 c = (prev1.data + prev2.data + c) // 10 if new_head == None: temp = Node(p) new_head = temp else: temp.next = Node(p) temp = temp.next prev1 = prev1.next prev2 = prev2.next while prev1: p = (prev1.data + c) % 10 c = (prev1.data + c) // 10 if new_head == None: temp = Node(p) new_head = temp else: temp.next = Node(p) temp = temp.next prev1 = prev1.next while prev2: p = (prev2.data + c) % 10 c = (prev2.data + c) // 10 if new_head == None: temp = Node(p) new_head = temp else: temp.next = Node(p) temp = temp.next prev2 = prev2.next if c > 0: carry.data = c temp = new_head prev = None n = None while temp != None: n = temp.next temp.next = prev prev = temp temp = n return prev def addCarryToRemaining(h1, curr, result, carry): temp = h1 while temp.next != curr: temp = temp.next temp.next = None temp1 = h1 prev1 = None n = None while temp1 != None: n = temp1.next temp1.next = prev1 prev1 = temp1 temp1 = n new_head = None c = carry.data while prev1: p = (prev1.data + c) % 10 c = (prev1.data + c) // 10 if new_head == None: temp = Node(p) new_head = temp else: temp.next = Node(p) temp = temp.next prev1 = prev1.next temp = new_head prev = None n = None while temp != None: n = temp.next temp.next = prev prev = temp temp = n temp = prev while temp.next != None: temp = temp.next temp.next = result.head result.head = prev if c > 0: carry.data = c else: carry.data = 0
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers. Example 1: Input: S1 = 3, S2 = 3 ValueS1 = {2,3,4} ValueS2 = {3,4,5} Output: 5 7 9 Explanation: After adding the 2 numbers the resultant number is 5 7 9. Example 2: Input: S1 = 1, S2 = 2 ValueS1 = {9} ValueS2 = {8,7} Output: 9 6 Explanation: Add 9 and 7 we get 16. 1 is carry here and is added to 8. So the answer is 9 6 Your Task: The task is to complete the function addSameSize() addCarryToRemaining(). Constraints: 1 <= S1, S2 <= 100
ans = None def addSameSize(h1, h2, carry): if h1.next == None: llsum = h1.data + h2.data + carry.data h1.data = int(llsum % 10) carry.data = llsum // 10 return h1 ll = addSameSize(h1.next, h2.next, carry) llsum = h1.data + h2.data + carry.data h1.data = int(llsum % 10) carry.data = llsum // 10 return h1 def addCarryToRemaining(h1, curr, result, carry): st = [] temp = h1 while temp != curr: st.append(temp) temp = temp.next while len(st) > 0: temp = st[-1] st.pop() llsum = temp.data + carry.data temp.data = int(llsum % 10) carry.data = llsum // 10 result.head = h1
ASSIGN VAR NONE FUNC_DEF IF VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers. Example 1: Input: S1 = 3, S2 = 3 ValueS1 = {2,3,4} ValueS2 = {3,4,5} Output: 5 7 9 Explanation: After adding the 2 numbers the resultant number is 5 7 9. Example 2: Input: S1 = 1, S2 = 2 ValueS1 = {9} ValueS2 = {8,7} Output: 9 6 Explanation: Add 9 and 7 we get 16. 1 is carry here and is added to 8. So the answer is 9 6 Your Task: The task is to complete the function addSameSize() addCarryToRemaining(). Constraints: 1 <= S1, S2 <= 100
def addSameSize(h1, h2, carry): if h1 is None: return None else: result = addSameSize(h1.next, h2.next, carry) total = h1.data + h2.data + carry.data node = Node(total % 10) carry.data = total // 10 node.next = result return node def addCarryToRemaining(h1, curr, result, carry): if h1 == curr: return result.head else: pointer = addCarryToRemaining(h1.next, curr, result, carry) total = h1.data + carry.data node = Node(total % 10) carry.data = total // 10 node.next = pointer result.head = node return node
FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers. Example 1: Input: S1 = 3, S2 = 3 ValueS1 = {2,3,4} ValueS2 = {3,4,5} Output: 5 7 9 Explanation: After adding the 2 numbers the resultant number is 5 7 9. Example 2: Input: S1 = 1, S2 = 2 ValueS1 = {9} ValueS2 = {8,7} Output: 9 6 Explanation: Add 9 and 7 we get 16. 1 is carry here and is added to 8. So the answer is 9 6 Your Task: The task is to complete the function addSameSize() addCarryToRemaining(). Constraints: 1 <= S1, S2 <= 100
def printLL(head): print("LL is - ") while head is not None: print("loop", head.data) head = head.next print("LL end ---- ") def reverseLL(head): prev = None cur = head while cur: next = cur.next cur.next = prev prev = cur cur = next return prev def reverseLLTillCurr(head, curr): prev = None cur = head while cur != curr: next = cur.next cur.next = prev prev = cur cur = next return prev def addSameSize(h1, h2, carry): rev_h1 = reverseLL(h1) head = rev_h1 rev_h2 = reverseLL(h2) while rev_h1: res = rev_h1.data + rev_h2.data + carry.data rev_h1.data = res % 10 carry.data = res // 10 rev_h1 = rev_h1.next rev_h2 = rev_h2.next head = reverseLL(head) return head def addCarryToRemaining(h1, curr, result, carry): rev_h1 = reverseLLTillCurr(h1, curr) head = rev_h1 joint = rev_h1 while rev_h1: res = rev_h1.data + carry.data rev_h1.data = res % 10 carry.data = res // 10 rev_h1 = rev_h1.next reverseLL(head) joint.next = curr result.head = h1 return
FUNC_DEF EXPR FUNC_CALL VAR STRING WHILE VAR NONE EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers. Example 1: Input: S1 = 3, S2 = 3 ValueS1 = {2,3,4} ValueS2 = {3,4,5} Output: 5 7 9 Explanation: After adding the 2 numbers the resultant number is 5 7 9. Example 2: Input: S1 = 1, S2 = 2 ValueS1 = {9} ValueS2 = {8,7} Output: 9 6 Explanation: Add 9 and 7 we get 16. 1 is carry here and is added to 8. So the answer is 9 6 Your Task: The task is to complete the function addSameSize() addCarryToRemaining(). Constraints: 1 <= S1, S2 <= 100
def addSameSize(h1, h2, carry): h1 = reverse(h1) h2 = reverse(h2) head = Node(0) temp = head while h1 and h2: s = h1.data + h2.data + carry.data head.next = Node(s % 10) carry.data = s // 10 h1 = h1.next h2 = h2.next head = head.next temp = reverse(temp.next) return temp def reverse(head): prev = None curr = head while curr: temp = curr.next curr.next = prev prev = curr curr = temp return prev def addCarryToRemaining(h1, curr, result, carry): t = h1 while t.next != curr: t = t.next if carry.data == 0: t.next = result.head result.head = h1 return else: t.next = None h1 = reverse(h1) while h1: temp = h1.next s = h1.data + carry.data h1.data = s % 10 h1.next = result.head result.head = h1 carry.data = s // 10 h1 = temp
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers. Example 1: Input: S1 = 3, S2 = 3 ValueS1 = {2,3,4} ValueS2 = {3,4,5} Output: 5 7 9 Explanation: After adding the 2 numbers the resultant number is 5 7 9. Example 2: Input: S1 = 1, S2 = 2 ValueS1 = {9} ValueS2 = {8,7} Output: 9 6 Explanation: Add 9 and 7 we get 16. 1 is carry here and is added to 8. So the answer is 9 6 Your Task: The task is to complete the function addSameSize() addCarryToRemaining(). Constraints: 1 <= S1, S2 <= 100
def addSameSize(h1, h2, carry): def sameSize(h1, h2, carry): sum = h1.data + h2.data res = None if h1.next: res = sameSize(h1.next, h2.next, carry) sum += carry.data carry.data = sum // 10 x = Node(sum % 10) x.next = res return x return sameSize(h1, h2, carry) def addCarryToRemaining(head, curr, result_list, carry): def addCarry(head, curr, result_list, carry): if head != curr: addCarry(head.next, curr, result_list, carry) sum = head.data + carry.data carry.data = sum // 10 next = result_list.head x = Node(sum % 10) x.next = next result_list.head = x addCarry(head, curr, result_list, carry)
FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NONE IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers. Example 1: Input: S1 = 3, S2 = 3 ValueS1 = {2,3,4} ValueS2 = {3,4,5} Output: 5 7 9 Explanation: After adding the 2 numbers the resultant number is 5 7 9. Example 2: Input: S1 = 1, S2 = 2 ValueS1 = {9} ValueS2 = {8,7} Output: 9 6 Explanation: Add 9 and 7 we get 16. 1 is carry here and is added to 8. So the answer is 9 6 Your Task: The task is to complete the function addSameSize() addCarryToRemaining(). Constraints: 1 <= S1, S2 <= 100
def reverse(head): prev_node = None curr_node = head while curr_node != None: next_node = curr_node.next curr_node.next = prev_node prev_node = curr_node curr_node = next_node head = prev_node return head class Solution: def addTwoLists(self, first, second): first = reverse(first) second = reverse(second)
FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers. Example 1: Input: S1 = 3, S2 = 3 ValueS1 = {2,3,4} ValueS2 = {3,4,5} Output: 5 7 9 Explanation: After adding the 2 numbers the resultant number is 5 7 9. Example 2: Input: S1 = 1, S2 = 2 ValueS1 = {9} ValueS2 = {8,7} Output: 9 6 Explanation: Add 9 and 7 we get 16. 1 is carry here and is added to 8. So the answer is 9 6 Your Task: The task is to complete the function addSameSize() addCarryToRemaining(). Constraints: 1 <= S1, S2 <= 100
import sys sys.setrecursionlimit(10000) def addSameSize(h1, h2, carry): if h1 == None: return h1 addSameSize(h1.next, h2.next, carry) ans = h1.data + h2.data + carry.data h1.data = ans % 10 carry.data = ans // 10 return h1 def addCarryToRemaining(h1, curr, result, carry): if h1 == curr: return addCarryToRemaining(h1.next, curr, result, carry) ans = h1.data + carry.data h1.data = ans % 10 carry.data = ans // 10 result.head = h1
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NONE RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers. Example 1: Input: S1 = 3, S2 = 3 ValueS1 = {2,3,4} ValueS2 = {3,4,5} Output: 5 7 9 Explanation: After adding the 2 numbers the resultant number is 5 7 9. Example 2: Input: S1 = 1, S2 = 2 ValueS1 = {9} ValueS2 = {8,7} Output: 9 6 Explanation: Add 9 and 7 we get 16. 1 is carry here and is added to 8. So the answer is 9 6 Your Task: The task is to complete the function addSameSize() addCarryToRemaining(). Constraints: 1 <= S1, S2 <= 100
def reverse(h): prev = None while h: next = h.next h.next = prev prev = h h = next return prev def addSameSize(h1, h2, carry): if not h1: return sm = 0 result = Node(sm) result.next = addSameSize(h1.next, h2.next, carry) sm = h1.data + h2.data + carry.data carry.data = sm // 10 sm = sm % 10 result.data = sm return result def addCarryToRemaining(h1, curr, result, carry): if h1 != curr: addCarryToRemaining(h1.next, curr, result, carry) sm = h1.data + carry.data carry.data = sm // 10 sm = sm % 10 result.head = push(result.head, sm) return
FUNC_DEF ASSIGN VAR NONE WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers. Example 1: Input: S1 = 3, S2 = 3 ValueS1 = {2,3,4} ValueS2 = {3,4,5} Output: 5 7 9 Explanation: After adding the 2 numbers the resultant number is 5 7 9. Example 2: Input: S1 = 1, S2 = 2 ValueS1 = {9} ValueS2 = {8,7} Output: 9 6 Explanation: Add 9 and 7 we get 16. 1 is carry here and is added to 8. So the answer is 9 6 Your Task: The task is to complete the function addSameSize() addCarryToRemaining(). Constraints: 1 <= S1, S2 <= 100
def addSameSize(h1, h2, carry): if not h1: return sm = 0 result = Node(sm) result.next = addSameSize(h1.next, h2.next, carry) sm = h1.data + h2.data + carry.data carry.data = sm // 10 sm = sm % 10 result.data = sm return result def addCarryToRemaining(h1, curr, result, carry): if h1 != curr: addCarryToRemaining(h1.next, curr, result, carry) sm = h1.data + carry.data carry.data = sm // 10 sm = sm % 10 result.head = push(result.head, sm) return class Node: def __init__(self, data): self.data = data self.next = None class llist: def __init__(self): self.head = None self.tail = None class carry_data: def __init__(self): self.data = 0 def append(h, t, data): if not h: h = Node(data) return h, h else: t.next = Node(data) return h, t.next def push(head, data): nn = Node(data) nn.next = head return nn def printlist(head): while head: print(head.data, end=" ") head = head.next print() def addList(head1, head2, size1, size2, result, carry): if not head1: result.head = head2 return if not head2: result.head = head1 return if size1 == size2: result.head = addSameSize(head1, head2, carry) else: diff = abs(size1 - size2) if size1 < size2: head1, head2 = head2, head1 cur = head1 for _ in range(diff): cur = cur.next result.head = addSameSize(cur, head2, carry) addCarryToRemaining(head1, cur, result, carry) if carry.data != 0: result.head = push(result.head, carry.data) if __name__ == "__main__": t = int(input()) for cases in range(t): size1, size2 = list(map(int, input().strip().split())) nodes1 = list(map(int, input().strip().split())) nodes2 = list(map(int, input().strip().split())) ll1 = llist() ll2 = llist() result = llist() carry = carry_data() for e in nodes1: ll1.head, ll1.tail = append(ll1.head, ll1.tail, e) for e in nodes2: ll2.head, ll2.tail = append(ll2.head, ll2.tail, e) addList(ll1.head, ll2.head, size1, size2, result, carry) printlist(result.head)
FUNC_DEF IF VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR ASSIGN VAR VAR RETURN IF VAR ASSIGN VAR VAR RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers. Example 1: Input: S1 = 3, S2 = 3 ValueS1 = {2,3,4} ValueS2 = {3,4,5} Output: 5 7 9 Explanation: After adding the 2 numbers the resultant number is 5 7 9. Example 2: Input: S1 = 1, S2 = 2 ValueS1 = {9} ValueS2 = {8,7} Output: 9 6 Explanation: Add 9 and 7 we get 16. 1 is carry here and is added to 8. So the answer is 9 6 Your Task: The task is to complete the function addSameSize() addCarryToRemaining(). Constraints: 1 <= S1, S2 <= 100
def addSameSize(h1, h2, carry): prev = None t_next = None t_h = h1 while h1: t_next = h1.next h1.next = prev prev = h1 h1 = t_next t_h = h1 = prev prev = None t_next = None while h2: t_next = h2.next h2.next = prev prev = h2 h2 = t_next h2 = prev t_carry = 0 while h1 and h2: a = h1.data b = h2.data if a + b + t_carry >= 10: h1.data = a + b - 10 + t_carry t_carry = 1 else: h1.data = a + b + t_carry t_carry = 0 h1 = h1.next h2 = h2.next prev = None t_next = None while t_h: t_next = t_h.next t_h.next = prev prev = t_h t_h = t_next carry.data = t_carry return prev def addCarryToRemaining(h1, curr, result, carry): if h1 is None: return if h1.next == curr: if h1.data + carry.data >= 10: h1.data = h1.data + carry.data - 10 carry.data = 1 else: h1.data = h1.data + carry.data carry.data = 0 result.head = h1 return t_next = h1.next addCarryToRemaining(t_next, curr, result, carry) if carry.data > 0: if h1.data + carry.data >= 10: h1.data = h1.data + carry.data - 10 carry.data = 1 else: h1.data = h1.data + carry.data carry.data = 0 result.head = h1
FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN IF VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR RETURN ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR