description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for _ in range(int(input())): s = " " + input() n = len(s) - 1 ans = 0 prefix = [0] * (n + 1) for i in range(1, n + 1): prefix[i] += prefix[i - 1] if s[i] == "1": prefix[i] += 1 last = n for i in range(n, 0, -1): if s[i] == "1": ans += last - i last -= 1 for i in range(1, n + 1): if s[i] == "0" and i + 1 <= n and s[i + 1] == "1": ans += prefix[i] if s[-1] == "0": ans += prefix[-1] print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING VAR VAR VAR IF VAR NUMBER STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
T = int(input()) for i in range(T): data = [x.strip() for x in input()] cnt = 0 los = len(data) - 1 f = -1 zf = -1 pc = [0] * (los + 1) cost = 0 for i2 in range(los, -1, -1): if data[i2] == "0" and zf == -1: zf += 1 if data[i2] == "1" and f == -1 and i != los and zf != -1: pc[i2] = 1 f += 1 frontC = 1 elif data[i2] == "1" and not f == -1: if data[i2 + 1] == "1": pc[i2] = frontC else: pc[i2] = frontC + 1 frontC = pc[i2] f = -1 for i2 in range(los, -1, -1): if f == -1 and data[i2] == "0": moveTo = i2 f += 1 elif data[i2] == "1" and f != -1: data[moveTo] = "1" data[i2] = "0" cost += moveTo - i2 moveTo -= 1 for i2 in range(0, los): cost += int(pc[i2]) print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
def calculate(n, N): red = [] b = N.index(1) s = 1 for i in range(b, n): if N[i] == 0: s += 1 if N[i] == 1 or i == n - 1: if s == 1: s = 0 red.append(s) s = 1 for i in range(len(red) - 2, 0, -1): red[i] = red[i] + red[i + 1] return sum(red) T = int(input()) for t in range(T): N = [int(i) for i in input()] n = len(N) if 1 not in N: print(0) else: print(calculate(n, N))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
def rowsold(): T = int(input()) for _ in range(T): count1 = 0 sec = 0 flag = 0 l = [int(d) for d in str(input())] for i in range(len(l)): if l[i] == 1: count1 += 1 flag = 1 elif l[i - 1] == 1 and l[i] == 0: sec = sec + count1 * 2 elif l[i - 1] == 0 and l[i] == 0 and flag == 1: sec = sec + count1 print(sec) rowsold()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) while t: t -= 1 s = input()[::-1] zeroes = 0 zero_batches = 0 result = 0 last = -1 for i in s: if "0" == i: zeroes += 1 if "1" == i: if "0" == last: zero_batches += 1 result += zeroes result += zero_batches last = i print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF STRING VAR VAR NUMBER IF STRING VAR IF STRING VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for _ in range(int(input())): s = list(input()) score = 0 ones = 0 consz = 0 for i in range(len(s)): if i < len(s) - 1: if s[i] == "0" and s[i + 1] == "1": score += ones * (consz + 2) consz = 0 elif s[i] == "1": ones += 1 elif s[i] == "0": consz += 1 elif s[i] == "0": score += ones * (consz + 2) print(score)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN 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 VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
T = int(input()) for t1 in range(T): s = input() n = len(s) sec = 0 block = 0 streak = 0 for q1 in range(n): if s[q1] == "1": streakover = True if block > 0 and streak > 0 and streakover: sec += block * (streak + 1) streak = 0 if s[q1] == "1": block += 1 streakover = True streak = 0 elif s[q1] == "0": streak += 1 streakover = False if q1 == n - 1 and s[q1] == "0": sec += block * (streak + 1) print(sec)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR STRING VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) while t > 0: t -= 1 s = input() ls = len(s) c = 0 tot = 0 i = 0 l = [] while i < ls: k = 0 if s[i] == "0": while i + 1 < ls and s[i + 1] == "0": i += 1 k += 1 l.append(k + 1) else: l.append(-1) i += 1 ll = len(l) i = 0 while i < ll: if l[i] != -1: tot += (1 + l[i]) * c else: c += 1 i += 1 print(tot)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
T = int(input()) while T > 0: a = input() s1 = 0 c1 = 0 h1 = 0 l = len(a) for i in range(0, l): if a[i] == "0": h1 = h1 + 1 else: break k = h1 while k < l: s = 0 while a[k] != "0": c1 = c1 + 1 k = k + 1 if k == l: break if k == l: break while a[k] != "1": s = s + 1 k = k + 1 if k == l: break s1 = s1 + c1 * (s + 1) print(s1) T = T - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR WHILE VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for t0 in range(t): str1 = str(input()) list1 = [] for i in str1: list1.append(int(i)) lis2 = [] lis3 = [] list2 = [] c = 0 for j in list1: if j == 1: list2.append(c) c += 1 sum1 = 0 for j in range(1, len(list2)): if list2[j] - list2[j - 1] > 1: sum1 += j + j * (list2[j] - list2[j - 1] - 1) if len(list2) > 0: if len(list1) - 1 > list2[-1]: sum1 += len(list2) + len(list2) * (len(list1) - 1 - list2[-1]) print(sum1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for i in range(t): s = input() s = s + "1" c = 0 one = 0 j = 0 while True: if s[j] == "1": one += 1 ind = s.index("1", j + 1) if ind - j == 1: j += 1 if j == len(s) - 1: break continue else: c += one * (ind - j) j += 1 if j == len(s) - 1: break print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
h = int(input()) for k in range(0, h): s = input() l = len(s) counter0 = 0 counter1 = 0 value = 0 for i in range(0, l): if i == l - 1 and s[l - 1] == "0": counter0 += 1 value += counter0 * counter1 + counter1 elif s[i] == "0": counter0 += 1 else: if s[i - 1] == "0": value += counter0 * counter1 + counter1 counter0 = 0 counter1 += 1 print(value)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for _ in range(int(input())): s = input() + " " output, zeroes, ones = 0, 0, 0 for i in range(len(s)): if (s[i] == "1" or s[i] == " ") and zeroes >= 1: output += ones * (zeroes + 1) zeroes = 0 ones += 1 elif s[i] == "1": ones += 1 elif s[i] == "0": zeroes += 1 print(output)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for _ in range(int(input())): row = [i for i in input()] L, ans, pos = len(row), 0, -1 cnt = [0] * L if row[L - 1] == "0": cnt[L - 1] = 1 for i in range(L - 2, -1, -1): cnt[i] = cnt[i + 1] if row[i] == "0" and row[i + 1] == "1": cnt[i] += 1 for i in range(L - 1, -1, -1): if row[i] == "0": pos = i break for i in range(pos - 1, -1, -1): if row[i] == "1": ans, pos = ans + pos - i + cnt[i], pos - 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
T = int(input()) for t in range(T): string = input() ones = 0 dists = [] first = True dist = 1 for c in reversed(string): if c == "1": dists.append(dist) dist = 1 ones += 1 else: dist += 1 sums = 0 for i, dist in enumerate(dists): if dist > 1: sums += dist * (ones - i) print(sums)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
n = int(input()) for ii in range(n): s = list(input()) if set(s) == set(["1"]): print("0") else: while s[-1] == "1": s.pop() a = [] for i in range(0, len(s)): if s[i] == "1": a.append(i + 1) c = 0 ans = 0 if len(a): for i in range(0, len(a) - 1): c += 1 space = a[i + 1] - a[i] - 1 if space: ans += space * c + c c += 1 space = len(s) - a[len(a) - 1] ans += space * c + c print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR LIST STRING EXPR FUNC_CALL VAR STRING WHILE VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for I in range(int(input())): s = str(input()) soldier_number = 0 ans = 0 i = 0 while i < len(s): if s[i] == "1": soldier_number += 1 i += 1 else: zero_number = 0 while i < len(s) and s[i] == "0": zero_number += 1 i += 1 ans += soldier_number * (zero_number + 1) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
T = int(input()) n = 1 while n <= T: st = str(input()) d = {} ones = 1 for i in range(len(st)): if st[i] == "1": d[i] = ones ones += 1 i = 0 time = 0 pos = len(st) for pos in sorted(d): if i == 0: ppos = pos i = 1 continue if ppos + 1 != pos: time = time + d[ppos] * (pos - ppos - 1) + d[ppos] ppos = pos if pos < len(st) - 1: time = time + d[pos] * (len(st) - pos - 1) + d[pos] print(time) n += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for x in range(int(input())): a = [int(i) for i in input()] a.append(1) su = 0 b = [] for i in a: if i == 1: su += 1 b.append(su) hh = 0 cu = 0 for i in range(len(b)): if a[i] == 0: cu += 1 elif cu != 0: hh += (cu + 1) * b[i - 1] cu = 0 print(hh)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for xaxa in range(int(input())): o = 0 m = 0 s = input() n = len(s) for i in range(n): c = s[i] if c == "1": o += 1 else: m += o if i < n - 1 and s[i + 1] == "1": m += o if s[-1] == "0": m += o print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR VAR IF VAR NUMBER STRING VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
def rowsold(): T = int(input()) for _ in range(T): count1 = 0 sec = 0 l = [int(d) for d in str(input())] try: while l[-1] == 1: del l[-1] while l[0] == 0: del l[0] except IndexError: pass for i in range(len(l)): if l[i] == 1: count1 += 1 elif l[i - 1] == 1 and l[i] == 0: sec = sec + count1 * 2 elif l[i - 1] == 0 and l[i] == 0: sec = sec + count1 print(sec) rowsold()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER NUMBER VAR NUMBER WHILE VAR NUMBER NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
from itertools import groupby for i in range(int(input())): ls = [] N = input() k = 0 for i, j in groupby(N): ls.append(list(j)) sum = 0 temp = 0 for i in range(len(ls)): if ls[i][0] == "1": temp += len(ls[i]) if i != len(ls) - 1: sum += temp * (len(ls[i + 1]) + 1) print(sum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
t = int(input()) for y in range(t): s = list(map(int, list(input()))) b = a = ans = 0 z = 0 ek = -1 for x in range(len(s)): if s[x] == 1 and a != 0: ans = ans + b * (a + 1) ek = 1 a = 0 z = 1 if s[x] == 1 and ek == 1: b += 1 z = 1 if s[x] == 1 and z != 1: ek = b = 1 if s[x] == 0 and ek == 1: a += 1 if s[x] == 0 and x == len(s) - 1: ans = ans + b * (a + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for _ in range(int(input())): s = input() n = len(s) i = 0 ans = 0 n1 = 0 while i < n: if s[i] == "1": i += 1 n1 += 1 while i < n and s[i] == "1": i += 1 n1 += 1 n0 = 0 while i < n and s[i] == "0": i += 1 n0 += 1 if n0 != 0: ans += (n0 + 1) * n1 else: i += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for _ in range(int(input())): string = list(input()) time = 0 n = string.count("1") k = len(string) for _ in range(n): for i in range(k): if string[i] == "1": if i == k - 1: break j = i + 1 while string[j] != "1": string[j - 1] = "0" string[j] = "1" if j - i == 1: time += 1 time += 1 if j < k - 1: j += 1 print(time)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little polar bear. He is playing a video game and he needs your help. There is a row with N cells, each either empty or occupied by a soldier, denoted by '0' and '1' respectively. The goal of the game is to move all soldiers to the right (they should occupy some number of rightmost cells). The only possible command is choosing a soldier and telling him to move to the right as far as possible. Choosing a soldier takes 1 second, and a soldier moves with the speed of a cell per second. The soldier stops immediately if he is in the last cell of the row or the next cell is already occupied. Limak isn't allowed to choose a soldier that can't move at all (the chosen soldier must move at least one cell to the right). Limak enjoys this game very much and wants to play as long as possible. In particular, he doesn't start a new command while the previously chosen soldier moves. Can you tell him, how many seconds he can play at most? -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S describing the row with N cells. Each character is either '0' or '1', denoting an empty cell or a cell with a soldier respectively. -----Output----- For each test case, output a single line containing one integer β€” the maximum possible number of seconds Limak will play the game. -----Constraints----- - 1 ≀ T ≀ 5 - 1 ≀ N ≀ 105 (N denotes the length of the string S) -----Subtasks----- - Subtask #1 (25 points): 1 ≀ N ≀ 10 - Subtask #2 (25 points): 1 ≀ N ≀ 2000 - Subtask #3 (50 points): Original constraints. -----Example----- Input: 4 10100 1100001 000000000111 001110100011010 Output: 8 10 0 48 -----Explanation----- Test case 1. The initial string is 10100. There are N = 5 cells. There is one soldier in the first cell, and one soldier in the third cell. The following scenario maximizes the total time: - Limak chooses the soldier in the first cell. This soldier can move only one cell to the right. It takes 1 second to choose a soldier and 1 second for a soldier to move to the next cell (2 seconds in total). The string is 01100 now. - Limak has only one choice. He must choose the soldier in the third cell because the other soldier can't move at all (the soldier in the second cell can't move to the right because the next cell is already occupied). Choosing a soldier takes 1 second. The chosen soldier moves from the third cell to the fifth cell, which takes 2 seconds. This operation takes 1 + 2 = 3 seconds in total. The string is 01001 now. - Limak has only one choice again. Since the soldier in the last row can't move further to the right, the soldier in the second cell must be chosen. He will move 2 cells to the right. This operation takes 1 + 2 = 3 seconds in total. The string become 00011 and the game is over. The total time is 2 + 3 + 3 = 8. Test case 2. The initial string is 1100001. There is only one possible scenario: - 1100001 is changed to 1000011 in 5 seconds (1 second to choose a soldier and 4 seconds for the soldier to move 4 cells to the right). - 1000011 is changed to 0000111 in 5 seconds. The total time is 5 + 5 = 10 seconds. Test case 3. The game is over immediately because all soldiers occupy rightmost cells already. The answer is 0.
for _ in range(int(input())): l = list(map(int, input().strip())) for i in range(len(l) - 1, -1, -1): if l[i] == 1: l.pop() else: break if l.count(1): time, prev, z, c = 0, 0, 0, 0 for i in range(len(l) - 1, -1, -1): if l[i] == 0: z += 1 continue if prev != z: prev = z c += 1 time += c + z print(time) else: print(0)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
n, m = map(int, input().split()) chs = [] for i in range(n): chs.append(list(input())) test = [] for i in range(n): if i % 2 == 0: test.append(list("01" * (m // 2 + 1))) else: test.append(list("10" * (m // 2 + 1))) inv = [([0] * (m + 1)) for i in range(n + 1)] for i in range(n): for j in range(m): if chs[i][j] != test[i][j]: inv[i + 1][j + 1] = inv[i + 1][j] + 1 else: inv[i + 1][j + 1] = inv[i + 1][j] col_sum = [([0] * (m + 1)) for i in range(n + 1)] for i in range(n): for j in range(m): if chs[i][j] != test[i][j]: col_sum[i + 1][j + 1] = col_sum[i][j + 1] + 1 else: col_sum[i + 1][j + 1] = col_sum[i][j + 1] min_req = [1000000000] * min(n + 1, m + 1) min_req[1] = min_req[0] = 0 for i in range(2, n + 1): for j in range(2, m + 1): dim = min(i, j) s = inv[i][j] - inv[i][j - 1] for k in range(2, dim + 1): s += ( inv[i - k + 1][j] - inv[i - k + 1][j - k] + col_sum[i][j - k + 1] - col_sum[i - k + 1][j - k + 1] ) min_req[k] = min(s, k * k - s, min_req[k]) Q = int(input()) q = list(map(int, input().split())) for i in q: ans = 0 for j in range(len(min_req)): if i >= min_req[j]: ans = j print(ans)
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 LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
def preProcess(mat, aux): for i in range(M): aux[0][i] = mat[0][i] for i in range(1, N): for j in range(M): aux[i][j] = mat[i][j] + aux[i - 1][j] for i in range(N): for j in range(1, M): aux[i][j] += aux[i][j - 1] def sumQuery(aux, li, lj, ri, rj): res = aux[ri][rj] if li > 0: res = res - aux[li - 1][rj] if lj > 0: res = res - aux[ri][lj - 1] if li > 0 and lj > 0: res = res + aux[li - 1][lj - 1] return res def solve(): min_size = min(M, N) board = [[(0) for _ in range(M)] for _ in range(N)] aux = [[(0) for _ in range(M)] for _ in range(N)] cell = 1 for i in range(N): col = [int(k) for k in input().strip()] if M % 2 == 0: cell = not cell for j in range(M): board[i][j] = 1 if col[j] == cell else 0 cell = not cell preProcess(board, aux) prev = sumQuery(aux, 0, 0, N - 1, M - 1) prev = min(prev, min_size**2 - prev) flips = [0] * (N * M + 1) flips[prev:] = [min_size for _ in range(len(flips[prev:]))] cont = True for size in range(min(N, M) - 1, 1, -1): flips_needed = 10**10 for i in range(N - size + 1): for j in range(M - size + 1): val = sumQuery(aux, i, j, i + size - 1, j + size - 1) flips_needed = min(flips_needed, val, size * size - val) if val == 0: flips[0:prev] = [size for _ in range(len(flips[0:prev]))] cont = False break if not cont: break if not cont: break flips[flips_needed:prev] = [size for _ in range(len(flips[flips_needed:prev]))] prev = flips_needed Q = int(input()) buff = [] for q in input().split(): q = int(q) if q >= N * M - flips[0]: buff.append(min_size) continue buff.append(flips[q]) print("\n".join(str(q) for q in buff)) N, M = map(int, input().split()) solve()
FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR IF VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
n, m = [int(i) for i in input().split()] sqsets = list(range(2, min(n, m) + 1)) ci = [] for i in range(n): ci.append(list(input())) cc = [[(0) for i in range(m + 1)] for j in range(n + 1)] def cache_creation_part(x, y): cc[y][x] = cc[y][x - 1] + cc[y - 1][x] - cc[y - 1][x - 1] if (y + x) % 2 == 0: cc[y][x] += int(ci[y - 1][x - 1] != "1") else: cc[y][x] += int(ci[y - 1][x - 1] != "0") def cache_creation(m, n): for y in range(1, n + 1): for x in range(1, m + 1): cache_creation_part(x, y) def operation(x, y, k): return cc[y + k][x + k] + cc[y][x] - cc[y + k][x] - cc[y][x + k] c = 0 kd = {k: c for k in sqsets} def kd_creation(m, n, sqsets): cache_creation(m, n) for k in sqsets: kd[k] = 10**9 + 1 for y in range(n - k + 1): for x in range(m - k + 1): cu = operation(x, y, k) kd[k] = min(kd[k], cu, k * k - cu) kd_creation(m, n, sqsets) del cc del ci del sqsets tc = int(input()) tc = [int(x) for x in input().split()] for u in tc: p = 0 for i in kd: if kd[i] <= u: p = max(i, p) print(p)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
def toggle_elem(elem): if elem == "0": return "1" else: return "0" def printGrid(grid, n, m): for i in range(n): for j in range(m): print(grid[i][j], end=" ") print() _input = input().split() N = int(_input[0]) M = int(_input[1]) dp_data = [([0] * M) for i in range(N)] grid = [] for i in range(N): _input = input() grid.append(_input) elem = "1" for i in range(N): elem2 = elem prev = 0 for j in range(M): if elem2 != grid[i][j]: dp_data[i][j] = prev + 1 else: dp_data[i][j] = prev prev = dp_data[i][j] elem2 = toggle_elem(elem2) elem = toggle_elem(elem) for i in range(1, N): for j in range(M): dp_data[i][j] += dp_data[i - 1][j] max_size = min(N, M) min_for_size_list = [0] * (max_size + 1) for size in range(1, max_size + 1): min_for_size = -1 for i in range(N + 1 - size): for j in range(M + 1 - size): l_corner = i, j r_corner = i + size - 1, j + size - 1 ABCD = dp_data[r_corner[0]][r_corner[1]] if l_corner[0] - 1 >= 0 and l_corner[1] - 1 >= 0: A = dp_data[l_corner[0] - 1][l_corner[1] - 1] else: A = 0 if l_corner[0] - 1 >= 0: AB = dp_data[l_corner[0] - 1][r_corner[1]] else: AB = 0 if l_corner[1] - 1 >= 0: AC = dp_data[r_corner[0]][l_corner[1] - 1] else: AC = 0 D = ABCD - (AB - A) - (AC - A) - A min_shuffle = min(D, size * size - D) if min_shuffle < min_for_size or min_for_size == -1: min_for_size = min_shuffle min_for_size_list[size] = min_for_size Q = int(input()) _input = input().split() for i in range(Q): query = int(_input[i]) for j in range(len(min_for_size_list) - 1, 0, -1): if min_for_size_list[j] <= query: print(j) break
FUNC_DEF IF VAR STRING RETURN STRING RETURN STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
def hz_arr(arr, N, M): ans = [] for g in range(N): pref = [0] for h in range(M): if g % 2 == 0: if h % 2 == 0 and int(arr[g][h]) == 1: pref.append(pref[h] + 1) elif h % 2 == 1 and int(arr[g][h]) == 0: pref.append(pref[h] + 1) else: pref.append(pref[h]) elif h % 2 == 0 and int(arr[g][h]) == 0: pref.append(pref[h] + 1) elif h % 2 == 1 and int(arr[g][h]) == 1: pref.append(pref[h] + 1) else: pref.append(pref[h]) ans.append(pref) return ans def ho_arr(arr, N, M): ans = [] for g in range(N): pref = [0] for h in range(M): if g % 2 == 0: if h % 2 == 0 and int(arr[g][h]) == 0: pref.append(pref[h] + 1) elif h % 2 == 1 and int(arr[g][h]) == 1: pref.append(pref[h] + 1) else: pref.append(pref[h]) elif h % 2 == 0 and int(arr[g][h]) == 1: pref.append(pref[h] + 1) elif h % 2 == 1 and int(arr[g][h]) == 0: pref.append(pref[h] + 1) else: pref.append(pref[h]) ans.append(pref) return ans def vz_arr(hz_arr, N, M): ans = [] for y in range(M + 1): pref = [0] for x in range(N): pref.append(hz_arr[x][y] + pref[x]) ans.append(pref) return ans def vo_arr(ho_arr, N, M): ans = [] for y in range(M + 1): pref = [0] for x in range(N): pref.append(ho_arr[x][y] + pref[x]) ans.append(pref) return ans def ret_max(arr, c): for d in range(len(arr) - 1, -1, -1): if arr[d] <= c: return d + 1 def zero(vz, p, q, l): return vz[p + l][q + l] - vz[p][q + l] - vz[p + l][q] + vz[p][q] def one(vo, p, q, l): return vo[p + l][q + l] - vo[p][q + l] - vo[p + l][q] + vo[p][q] [N, M] = [int(i) for i in input().split()] arr = [] s = "" for x in range(N): s = input() arr.append(s) Q = int(input()) qry = [int(i) for i in input().split()] lookup = [] hz = hz_arr(arr, N, M) ho = ho_arr(arr, N, M) vz = vz_arr(hz, N, M) vo = vo_arr(ho, N, M) for w in range(1, min(N, M) + 1): done = 0 ans = pow(10, 9) + 1 for p in range(N - w + 1): for q in range(M - w + 1): temp = min(zero(vz, p, q, w), one(vo, p, q, w)) ans = min(ans, temp) lookup.append(ans) for e in range(len(qry)): print(ret_max(lookup, qry[e]))
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
n, m = list(map(int, input().split())) grid = [list(map(int, list(input()))) for i in range(n)] obs0 = [[(0) for j in range(m)] for i in range(n)] obs1 = [[(0) for j in range(m)] for i in range(n)] max_side = min(m, n) side_c = [(40000) for i in range(max_side + 1)] side_c[0] = 0 side_c[1] = 0 for i in range(n): for j in range(m): if (i + j) % 2 == 0: obs0[i][j] = 0 obs1[i][j] = 1 else: obs0[i][j] = 1 obs1[i][j] = 0 for i in range(n): for j in range(m): if obs0[i][j] != grid[i][j]: obs0[i][j] = 1 else: obs0[i][j] = 0 if obs1[i][j] != grid[i][j]: obs1[i][j] = 1 else: obs1[i][j] = 0 for i in range(n): for j in range(1, m): obs0[i][j] += obs0[i][j - 1] obs1[i][j] += obs1[i][j - 1] for j in range(m): for i in range(1, n): obs0[i][j] += obs0[i - 1][j] obs1[i][j] += obs1[i - 1][j] for side in range(2, max_side + 1): for i in range(n): for j in range(m): if i + side > n or j + side > m: continue count0 = ( obs0[i + side - 1][j + side - 1] + (i != 0 and j != 0) * obs0[i - 1][j - 1] - (j != 0) * obs0[i + side - 1][j - 1] - (i != 0) * obs0[i - 1][j + side - 1] ) count1 = ( obs1[i + side - 1][j + side - 1] + (i != 0 and j != 0) * obs1[i - 1][j - 1] - (j != 0) * obs1[i + side - 1][j - 1] - (i != 0) * obs1[i - 1][j + side - 1] ) side_c[side] = min(count0, count1, side_c[side]) input() list_c = list(map(int, input().split())) for i in range(len(list_c)): c = list_c[i] answer = 1 for side in range(2, max_side + 1): if c < side_c[side]: answer = side - 1 break if c >= side_c[max_side]: answer = max_side print(answer)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN 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 VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
n, m = map(int, input().split()) dp = [[(0) for x in range(m + 1)] for y in range(n + 1)] l1 = [[(0) for x in range(m)] for y in range(n)] l2 = [[(0) for x in range(m)] for y in range(n)] for ii in range(n): for jj in range(m): if (ii + jj) % 2 == 0: l2[ii][jj] = 1 else: l2[ii][jj] = 0 for i in range(n): s = input() for j in range(m): l1[i][j] = int(s[j]) l = [0] * min(n, m) sq = min(n, m) l3 = [0] * sq for i in range(n): s = 0 for j in range(m): if l1[i][j] != l2[i][j]: s += 1 dp[i + 1][j + 1] = s else: dp[i + 1][j + 1] = s for i in range(1, n + 1): for j in range(1, m + 1): dp[i][j] = dp[i - 1][j] + dp[i][j] for no in range(sq, 1, -1): mymin = 1000000 for i in range(n): for j in range(m): if i + no <= n and j + no <= m: ans = dp[i + no][j + no] - dp[i][j + no] - dp[i + no][j] + dp[i][j] ans = min(ans, no * no - ans) if ans < mymin: mymin = ans l3[no - 1] = mymin if mymin == 0: break q = int(input()) lq = list(map(int, input().split())) for i in lq: if i > sq * sq // 2: print(sq) else: for j in range(len(l3) - 1, -1, -1): if l3[j] <= i: print(j + 1) break
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
def sum_up(row1, col1, row2, col2): return ( sums[row2 + 1][col2 + 1] - sums[row2 + 1][col1] - sums[row1][col2 + 1] + sums[row1][col1] ) n, m = map(int, input().strip().split()) c = [] arr1 = [[(0) for i in range(205)] for j in range(205)] arr2 = [[(0) for i in range(205)] for j in range(205)] for _ in range(n): c.append(list(input())) s1, s2 = {}, {} for i in range(n): for j in range(m): if (i + j) % 2 == 0: if c[i][j] == "1": arr1[i][j] = 0 else: arr1[i][j] = 1 s1[i] = j elif c[i][j] == "0": arr1[i][j] = 0 else: arr1[i][j] = 1 s1[i] = j if (i + j) % 2 != 0: if c[i][j] == "1": arr2[i][j] == 0 else: arr2[i][j] = 1 s2[i] = j elif c[i][j] == "0": arr2[i][j] = 0 else: arr2[i][j] = 1 s2[i] = j max_poss = min(n, m) dp1 = [-1] * 1000005 dp2 = [-1] * 1000005 last1, last2 = 0, 0 row, col = n, m sums = [[(0) for i in range(m + 1)] for j in range(n + 1)] for i in range(1, row + 1): for j in range(1, col + 1): sums[i][j] = ( arr1[i - 1][j - 1] + sums[i - 1][j] + sums[i][j - 1] - sums[i - 1][j - 1] ) for l in range(2, max_poss + 1): mini = 1000005 for i in range(n - l + 1): for j in range(m - l + 1): inversions = sum_up(i, j, i + l - 1, j + l - 1) if inversions < mini: mini = inversions dp1[mini] = l last1 = max(last1, mini) sums = [[(0) for i in range(m + 1)] for j in range(n + 1)] for i in range(1, row + 1): for j in range(1, col + 1): sums[i][j] = ( arr2[i - 1][j - 1] + sums[i - 1][j] + sums[i][j - 1] - sums[i - 1][j - 1] ) for l in range(2, max_poss + 1): mini = 1000005 for i in range(n - l + 1): for j in range(m - l + 1): inversions = sum_up(i, j, i + l - 1, j + l - 1) if inversions < mini: mini = inversions dp2[mini] = l last2 = max(last2, mini) pre = 0 for i in range(last1 + 1): if dp1[i] == -1 and i == 0: while dp[i] == -1: dp1[i] = 1 i += 1 elif dp1[i] == -1: while i <= last1 and dp1[i] == -1: dp1[i] = pre i += 1 else: pre = dp1[i] i += 1 for i in range(last2 + 1): if dp2[i] == -1 and i == 0: while dp2[i] == -1: dp2[i] = 1 i += 1 elif dp2[i] == -1: while i <= last2 and dp2[i] == -1: dp2[i] = pre i += 1 else: pre = dp2[i] i += 1 q = int(input()) query = list(map(int, input().strip().split())) for i in query: if n == 1 or m == 1: print(1) elif i <= min(last1, last2): if dp1[i] == 0 and dp2[i] == 0: print(1) else: print(max(dp1[i], dp2[i])) else: print(max_poss)
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR DICT DICT 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 VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING EXPR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
n, m = map(int, input().split(" ")) CZM = 0 COM = 0 IVM0 = [([0] * m) for i in range(n)] IVM1 = [([0] * m) for i in range(n)] PSA0 = [([0] * m) for i in range(n)] PSA1 = [([0] * m) for i in range(n)] def findsum(PSA, i, j, k): x, y = i - k, j - k if x < 0 and y < 0: return PSA[i][j] if x < 0 and y >= 0: return PSA[i][j] - PSA[i][j - k] if x >= 0 and y < 0: return PSA[i][j] - PSA[i - k][j] if x >= 0 and y >= 0: return PSA[i][j] - PSA[i][j - k] - PSA[i - k][j] + PSA[i - k][j - k] def calPAS(PSA, IVM, n, m): PSA[0][0] = IVM[0][0] for i in range(1, m): PSA[0][i] = PSA[0][i - 1] + IVM[0][i] for i in range(0, n): PSA[i][0] = PSA[i - 1][0] + IVM[i][0] for i in range(1, n): for j in range(1, m): PSA[i][j] = PSA[i - 1][j] + PSA[i][j - 1] - PSA[i - 1][j - 1] + IVM[i][j] ans = 1 for i in range(n): x = input() for j in range(m): b = int(x[j]) if (i + j) % 2 == 0: p = ans q = 1 - ans else: p = 1 - ans q = ans if b != p: IVM1[i][j] = 1 if b != q: IVM0[i][j] = 1 calPAS(PSA0, IVM0, n, m) calPAS(PSA1, IVM1, n, m) ans0 = [-1] * (n * m + 1) ans1 = [-1] * (n * m + 1) p, q = 0, 0 v = min(m, n) for k in range(2, v + 1): p = q = k - 1 for i in range(p, n): for j in range(q, m): x0 = findsum(PSA0, i, j, k) x1 = findsum(PSA1, i, j, k) ans0[x0] = max(k, ans0[x0]) ans1[x1] = max(k, ans1[x1]) if ans0[0] == -1: ans0[0] = 1 if ans1[0] == -1: ans1[0] = 1 for i in range(1, n * m + 1): if ans0[i] == -1: ans0[i] = ans0[i - 1] elif ans0[i] < ans0[i - 1]: ans0[i] = ans0[i - 1] if ans1[i] == -1: ans1[i] = ans1[i - 1] elif ans1[i] < ans1[i - 1]: ans1[i] = ans1[i - 1] q = int(input()) for q in input().split(" "): p = int(q) if p > n * m: print(max(ans0[n * m], ans1[n * m])) else: print(max(ans1[p], ans0[p]))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
a, b = map(int, input().split()) arr2d = [] for i in range(a): arr2d.append(list(map(int, input()))) gi = int(1000000000.0) acc = [([0] * (b + 1)) for i in range(a + 1)] szm = [gi] * (min(a, b) + 1) def citr(shift=0): for i in range(a): for j in range(b): acc[i + 1][j + 1] = 0 if i + j & 1: if arr2d[i][j] == shift: acc[i + 1][j + 1] += 1 elif arr2d[i][j] == (not shift): acc[i + 1][j + 1] += 1 acc[i + 1][j + 1] += acc[i + 1][j] for j in range(b): acc[i + 1][j + 1] += acc[i][j + 1] def szopp(x, y, p, q): return acc[p + 1][q + 1] - acc[x][q + 1] - acc[p + 1][y] + acc[x][y] def citr2(): for sz in range(0, min(a, b)): for i in range(0, a - sz): for j in range(0, b - sz): szm[sz + 1] = min(szm[sz + 1], szopp(i, j, i + sz, j + sz)) citr() citr2() citr(shift=1) citr2() q = int(input()) arr5 = map(int, input().split(" ")) for c in arr5: for j in range(len(szm) - 1, 0, -1): if szm[j] <= c: print(j) break
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
n, m = map(int, input().split()) C = [] for i in range(n): R_ = list(map(int, list(input().strip()))) C.append(R_) vo = [[(0) for _ in range(m)] for _ in range(n)] ve = [[(0) for _ in range(m)] for _ in range(n)] vo[0][0] = C[0][0] ve[0][0] = (C[0][0] + 1) % 2 for i in range(1, m): if C[0][i] == i % 2: vo[0][i] = vo[0][i - 1] ve[0][i] = ve[0][i - 1] + 1 else: vo[0][i] = vo[0][i - 1] + 1 ve[0][i] = ve[0][i - 1] for i in range(1, n): if C[i][0] == i % 2: vo[i][0] = vo[i - 1][0] ve[i][0] = ve[i - 1][0] + 1 else: vo[i][0] = vo[i - 1][0] + 1 ve[i][0] = ve[i - 1][0] for i in range(1, n): for j in range(1, m): if C[i][j] == (i + j) % 2: vo[i][j] = vo[i - 1][j] + vo[i][j - 1] - vo[i - 1][j - 1] ve[i][j] = ve[i - 1][j] + ve[i][j - 1] - ve[i - 1][j - 1] + 1 else: vo[i][j] = vo[i - 1][j] + vo[i][j - 1] - vo[i - 1][j - 1] + 1 ve[i][j] = ve[i - 1][j] + ve[i][j - 1] - ve[i - 1][j - 1] vv = [(100000) for _ in range(min(n, m) + 1)] vv[1] = 0 for i in range(2, min(n, m) + 1): for j in range(n - i + 1): for k in range(m - i + 1): oo = vo[j + i - 1][k + i - 1] ee = ve[j + i - 1][k + i - 1] if j - 1 >= 0: oo = oo - vo[j - 1][k + i - 1] ee = ee - ve[j - 1][k + i - 1] if k - 1 >= 0: oo = oo - vo[j + i - 1][k - 1] ee = ee - ve[j + i - 1][k - 1] if k - 1 >= 0 and j - 1 >= 0: oo = oo + vo[j - 1][k - 1] ee = ee + ve[j - 1][k - 1] vv[i] = min(vv[i], oo, ee) q = int(input()) quer = list(map(int, list(input().split()))) for j in quer: for i in range(min(n, m), 0, -1): if vv[i] <= j: print(i) break
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 VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
n, m = map(int, input().split()) b = [[int(x) for x in list(input())] for x in range(n)] c = int(input()) size = min(n, m) res = [(size**2) for x in range(size + 1)] def invert(i, j, r, ele): count = 0 for index, x in enumerate(range(i, i + r)): temp = ele if index % 2: temp = 0 if ele else 1 for y in range(j, j + r): count += 0 if temp == b[x][y] else 1 temp = 0 if temp else 1 return count for s in range(size, 1, -1): flag = 0 for x in range(n - s + 1): for y in range(m - s + 1): ic = min(invert(x, y, s, 0), invert(x, y, s, 1)) res[s] = min(res[s], ic) if not ic: flag = 1 break if flag: break if flag: break for t in map(int, input().split()): for index, e in reversed(list(enumerate(res))): if e <= t: print(index) break res = list(map(lambda x: 0 if x == size**2 else x, res))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR IF VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
n, m = map(int, input().split()) R = n C = m a = [None] * n z = [0] * n o = [1] * n for i in range(m): if i % 2 != 0: z[i] = 1 o[i] = 0 zi = [None] * n oi = [None] * n for i in range(n): a[i] = input() a[i] = list(a[i]) for j in range(m): a[i][j] = int(a[i][j]) if i % 2 == 0: zi[i] = z[:] oi[i] = o[:] else: zi[i] = o[:] oi[i] = z[:] forzi = 0 foroi = 0 psao = [[(0) for x in range(C)] for y in range(R)] psaz = [[(0) for x in range(C)] for y in range(R)] noioo = 0 noizz = 0 noio = 0 noiz = 0 for i in range(n): noiz = 0 noio = 0 for j in range(m): if i == 0: if a[i][j] != oi[i][j]: noio += 1 else: noiz += 1 psao[i][j] = noio psaz[i][j] = noiz else: if a[i][j] != oi[i][j]: noio += 1 else: noiz += 1 psaz[i][j] = noiz + psaz[i - 1][j] psao[i][j] = noio + psao[i - 1][j] mmm = [] for i in range(2, min(m, n) + 1): c = 0 ap = [] for j in range(n - i + 1): for k in range(m - i + 1): czz = 0 czz1 = 0 czz2 = 0 coo = 0 cor = 0 czz += psaz[j + i - 1][k + i - 1] coo += psao[j + i - 1][k + i - 1] if k - 1 >= 0: czz -= psaz[j + i - 1][k - 1] coo -= psao[j + i - 1][k - 1] else: czz += 0 if j - 1 >= 0: czz -= psaz[j - 1][k + i - 1] coo -= psao[j - 1][k + i - 1] else: czz += 0 if j - 1 >= 0 and k - 1 >= 0: czz += psaz[j - 1][k - 1] coo += psao[j - 1][k - 1] else: czz += 0 ap.append(min(abs(coo), abs(czz))) mmm.append(min(ap)) q = int(input()) lo = [int(pp) for pp in input().split()] maxq = max(mmm) for i in range(q): if lo[i] > maxq: print(min(n, m)) else: j = len(mmm) - 1 ind = 0 while j >= 0: if lo[i] >= mmm[j]: ind = j + 2 break j -= 1 print(ind)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
n, m = map(int, input().split()) L = [] for i in range(n): s = input() L.append(s) cost = [] h2 = [0] * (m + 1) cost.append(h2) for i in range(n): h = [0] for j in range(m): if L[i][j] == "0" and (i + j) % 2 != 0: h.append(1) elif L[i][j] == "1" and (i + j) % 2 == 0: h.append(1) else: h.append(0) cost.append(h) pre = [] h2 = [0] * (m + 1) pre.append(h2) for i in range(1, n + 1): h = [0] c = 0 for j in range(1, m + 1): c += cost[i][j] c2 = c if i > 0: c2 += pre[i - 1][j] h.append(c2) pre.append(h) bs = [0] * (m * n + 10) for i in range(1, n + 1): for j in range(1, m + 1): for k in range(1, min(m, n) + 1): if i - k >= 0 and j - k >= 0: c = pre[i][j] - pre[i - k][j] - pre[i][j - k] + pre[i - k][j - k] c = min(c, k * k - c) bs[c] = max(bs[c], k) mx = bs[0] for i in range(1, len(bs)): mx = max(mx, bs[i]) bs[i] = mx Q = int(input()) q = [int(x) for x in input().split()] for i in range(0, len(q)): qr = min(m * n, q[i]) print(bs[qr])
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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
def get_aux_matrix(C, n, m): mat = [[(0) for j in range(0, m)] for i in range(0, N)] for i in range(0, m): mat[0][i] = C[0][i] for i in range(1, n): for j in range(0, m): mat[i][j] = C[i][j] + mat[i - 1][j] for i in range(0, n): for j in range(1, m): mat[i][j] += mat[i][j - 1] return mat def sum_sub_matrix(mat, i1, j1, i2, j2): ret = mat[i2][j2] if i1 > 0: ret -= mat[i1 - 1][j2] if j1 > 0: ret -= mat[i2][j1 - 1] if i1 > 0 and j1 > 0: ret += mat[i1 - 1][j1 - 1] return ret def get_nb_intersects(aux_C, side): ret = 2**64 for i in range(0, len(aux_C) - side + 1): for j in range(0, len(aux_C) - side + 1): ret = min(ret, sum_sub_matrix(aux_C, i, j, i + side - 1, j + side - 1)) return ret N, M = [int(s) for s in input().split(" ")] board = [] for i in range(0, N): board.append([int(s) for s in input()]) C1 = [[(0) for j in range(0, M)] for i in range(0, N)] C2 = [[(0) for j in range(0, M)] for i in range(0, N)] for i in range(0, N): for j in range(0, M): if board[i][j] != (j % 2 + i % 2) % 2: C1[i][j] = 1 if board[i][j] != (j % 2 + (i + 1) % 2) % 2: C2[i][j] = 1 aux_C1 = get_aux_matrix(C1, N, M) aux_C2 = get_aux_matrix(C2, N, M) cTab = [0] * (min(N, M) + 1) cTab[1] = 0 for i in range(2, len(cTab)): c1 = get_nb_intersects(aux_C1, i) c2 = get_nb_intersects(aux_C2, i) cTab[i] = min(c1, c2) Q = int(input()) for c in [int(s) for s in input().split(" ")]: side = len(cTab) - 1 while cTab[side] > c: side = side - 1 print(side)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
n, m = map(int, input().split()) chess = [] for _ in range(n): chess.append([int(x) for x in list(input())]) chessO = [] chessL = [] fixO = [] fixL = [] for _ in range(n): p = [0] * m fixL.append(p) q = [0] * m fixO.append(q) tm = [0] * m chessO.append(tm) to = [0] * m chessL.append(to) for i in range(n): for j in range(m): chessO[i][j] = (i + j) % 2 ^ chess[i][j] chessL[i][j] = (i + j + 1) % 2 ^ chess[i][j] fixO[i][j] = chessO[i][j] fixL[i][j] = chessL[i][j] for i in range(n): for j in range(1, m): fixO[i][j] = fixO[i][j - 1] + chessO[i][j] fixL[i][j] = fixL[i][j - 1] + chessL[i][j] ans = [-1] * min(m, n) for i in range(n): for j in range(m): for k in range(min(n - i, m - j)): errorO, errorL = 0, 0 for l in range(i, i + k + 1): errorO += fixO[l][j + k] - fixO[l][j] + chessO[l][j] errorL += fixL[l][j + k] - fixL[l][j] + chessL[l][j] if ans[k] == -1 or ans[k] > min(errorO, errorL): ans[k] = min(errorO, errorL) final = [0] * 50000 maxo = 9999999999 for i in range(min(m, n) - 1, -1, -1): if ans[i] != -1 and ans[i] < maxo: maxo = ans[i] final[maxo] = i tmp = final[0] for i in range(1, 50000): if final[i] > tmp: tmp = final[i] final[i] = tmp input() q = [int(x) for x in input().split()] for x in q: if x > 50000 - 2: print(final[45000] + 1) else: print(final[x] + 1)
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 VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
n, m = input().split(" ") n = int(n) m = int(m) lst = [None] * n for i in range(0, n): lst[i] = input() c1 = [[(0) for x in range(m + 2)] for y in range(n + 2)] c2 = [[(0) for x in range(m + 2)] for y in range(n + 2)] c3 = [[(0) for x in range(m + 2)] for y in range(n + 2)] c4 = [[(0) for x in range(m + 2)] for y in range(n + 2)] a = 1 b = 0 cn1 = 0 cn2 = 0 cn3 = 0 for i in range(n): if i % 2 == 0: a = 1 else: a = 0 if i % 2 == 0: b = 0 else: b = 1 for j in range(m): x = int(lst[i][j]) if x != a: if j == 0: c1[i][j] = 1 else: c1[i][j] = c1[i][j - 1] + 1 elif j != 0: c1[i][j] = c1[i][j - 1] if x != b: if j == 0: c2[i][j] = 1 else: c2[i][j] = c2[i][j - 1] + 1 elif j != 0: c2[i][j] = c2[i][j - 1] if a == 0: a = 1 else: a = 0 if b == 0: b = 1 else: b = 0 for i in range(m): c3[0][i] = c1[0][i] c4[0][i] = c2[0][i] for i in range(1, n): for j in range(m): c3[i][j] = c3[i - 1][j] + c1[i][j] c4[i][j] = c4[i - 1][j] + c2[i][j] query = [99999999] * 2000 query[0] = 0 for x in range(1, min(m, n)): for i in range(x, n): for j in range(x, m): query[x] = min( query[x], c3[i][j] - c3[i - x - 1][j] - (c3[i][j - x - 1] - c3[i - x - 1][j - x - 1]), c4[i][j] - c4[i - x - 1][j] - (c4[i][j - x - 1] - c4[i - x - 1][j - x - 1]), ) q = input() q = int(q) arr = [int(x) for x in input().split(" ")] for i in range(0, q): if arr[i] >= query[min(m, n) - 1]: print(min(m, n)) else: for j in range(min(m, n) - 1, -1, -1): if arr[i] >= query[j]: break print(j + 1)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN 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 IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
def pre(): for i in range(n): for j in range(m): if a[i][j] == "1": b[i][j].append((1, 0)) else: b[i][j].append((0, 1)) hr = 1 vr = 1 co = 0 if b[i][j][0][1]: co = 1 for k in range(min(i, j)): hr = int(not hr) vr = int(not vr) if a[i - k - 1][j] != str(vr): co += 1 if a[i][j - k - 1] != str(hr): co += 1 wb = b[i - 1][j - 1][k][1] + co b[i][j].append(((k + 2) ** 2 - wb, wb)) n, m = [int(x) for x in input().split()] a = [] for i in range(n): a.append(input()) b = [[[] for i in range(m)] for j in range(n)] pre() ans = [0] * 40000 for i in range(n): for j in range(m): for k in range(min(i, j) + 1): ans[b[i][j][k][0]] = max(ans[b[i][j][k][0]], k + 1) ans[b[i][j][k][1]] = max(ans[b[i][j][k][1]], k + 1) for i in range(1, len(ans)): ans[i] = max(ans[i], ans[i - 1]) q = int(input()) qu = [int(x) for x in input().split()] for el in qu: if el > 20000: print(min(n, m)) else: print(ans[el])
FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR 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 LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Chef loves to play chess, so he bought a new chessboard with width M$M$ and height N$N$ recently. Chef considers a chessboard correct if its width (number of columns) is equal to its height (number of rows) and each cell has no side-adjacent cell of the same color (this is the so-called "chess order" which you can see in real-world chessboards). Chef's chessboard does not have to be a correct chessboard (in particular, it may have Nβ‰ M$N \neq M$). A sub-board of Chef's chessboard is a rectangular piece of this board with an arbitrarily chosen top left and bottom right cell (possibly equal to the original chessboard). Every sub-board is also a chessboard. Chef can invert some cells; inverting a cell means changing its color from white to black or from black to white. After inverting those cells, he wants to cut the maximum correct sub-board out of the original chessboard. Chef has not yet decided how many cells he would like to invert. Now he wonders about the answers to Q$Q$ question. In the i$i$-th question (1≀i≀Q$1 \le i \le Q$), he is allowed to invert at most ci$c_i$ cells (possibly zero); he would like to know the side length of the largest possible correct sub-board of his chessboard. Help Chef answer these questions. -----Input----- - The first line of the input contains two space-separated integers N$N$ and M$M$. - N$N$ lines follow. For each valid i$i$, the i$i$-th of these lines contains a string with length M$M$ describing the i$i$-th row of Chef's chessboard. Each character of this string is either '0', representing a black cell, or '1', representing a white cell. - The next line contains a single integer Q$Q$. - The last line contains Q$Q$ space-separated integers c1,c2,…,cQ$c_1, c_2, \dots, c_Q$. -----Output----- For each question, print a single line containing one integer β€” the maximum size of a correct sub-board. -----Constraints----- - 1≀N,M≀200$1 \le N, M \le 200$ - 1≀Q≀105$1 \le Q \le 10^5$ - 0≀ci≀109$0 \le c_i \le 10^9$ for each valid i$i$ -----Subtasks----- Subtask #1 (20 points): - 1≀N,M≀20$1 \le N, M \le 20$ - 1≀Q≀100$1 \le Q \le 100$ Subtask #2 (30 points): 1≀N,M≀20$1 \le N, M \le 20$ Subtask #3 (50 points): original constraints -----Example Input----- 8 8 00101010 00010101 10101010 01010101 10101010 01010101 10101010 01010101 4 1 2 0 1001 -----Example Output----- 7 8 6 8 -----Explanation----- If we don't change the board, the best answer here is the 6x6 bottom right sub-board. We can invert cells (2,2)$(2, 2)$ and (1,1)$(1, 1)$ to get a better answer.
import sys dp = [[(0) for i in range(201)] for i in range(201)] Q = [sys.maxsize for i in range(201)] Q1 = [sys.maxsize for i in range(201)] def compareWith(A, B, n, m, f): for i in range(1, n + 1): for j in range(1, m + 1): a = 0 if A[i - 1][j - 1] != B[i - 1][j - 1]: a = 1 dp[i][j] = a + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] for i in range(1, n + 1): for j in range(1, m + 1): k = i l = j r = 1 while k <= n and l <= m: a = findError(i, j, k, l) Q[r] = min(Q[r], a) k += 1 l += 1 r += 1 if f == 1: Q1 = Q[:] def findError(i, j, k, l): return dp[k][l] - dp[k][j - 1] - dp[i - 1][l] + dp[i - 1][j - 1] n, m = map(int, input().split()) A = [] C = [] B = [] for i in range(n): s = input() A.append(s) for i in range(n): sb = "" sc = "" for j in range(m): if i % 2 == 0: if j % 2 == 0: sb += "1" sc += "0" else: sb += "0" sc += "1" elif j % 2 == 1: sb += "1" sc += "0" else: sb += "0" sc += "1" B.append(sb) C.append(sc) compareWith(A, B, n, m, 1) compareWith(A, C, n, m, 0) q = int(input()) c = list(map(int, input().split())) for i in c: a = 0 for j in range(1, min(m, n) + 1): if i >= Q[j] or i >= Q1[j]: a = max(a, j) print(a)
IMPORT ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR STRING VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. There are $N$ circles in a plane. Circle $i$ and circle $j$ form a *good pair* if it is possible to choose a point $P_{1}$ on the perimeter of circle $i$ and a point $P_{2}$ on the perimeter of circle $j$ such that the Euclidean distance between $P_{1}$ and $P_{2}$ is exactly $K$. (Note that $P_{1}$ and $P_{2}$ do not need to have integer coordinates.) You should answer $Q$ queries. In each query, you are given the required distance $K$. Chef is interested in finding the number of good pairs of distinct circles for each query. Please help him. Note: Circle $i$ and circle $j$ are distinct when $i \neq j$. There may be any number of circles that coincide (have identical centers and radii). ------ Input ------ The first line of the input contains two space-separated integers $N$ and $Q$ denoting the number of circles and the number of queries respectively. Each of the following $N$ lines contains three space-separated integers $X$, $Y$ and $R$ describing a circle with radius $R$ and center $(X, Y)$. Each of the next $Q$ lines contains one integer $K$ describing a query. ------ Output ------ For each query, print a single line containing one integer β€” the number of good pairs of circles. ------ Constraints ------ $2 ≀ N ≀ 10^{3}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $X, Y ≀ |2\cdot 10^{5}|$ $1 ≀ R ≀ 2\cdot 10^{5}$ $0 ≀ K ≀ 10^{6}$ ------ Subtasks ------ Subtask #1 (30 points): $2 ≀ N ≀ 10^{2}$ $1 ≀ Q ≀ 5\cdot 10^{2}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 2 3 0 0 5 8 3 2 0 10 20 ----- Sample Output 1 ------ 0 1 0 ----- explanation 1 ------ The distance between the point $(0.00, -5.00)$ on circle $1$ and the point $(8.00, 1.00)$ on circle $2$ is $10$. There is no pair of points on these two circles with distance $0$ or $20$.
N, Q = list(map(int, input().split())) g = [] karray = [(0) for z in range(10**6 + 1)] for i in range(N): X, Y, R = list(map(int, input().split())) g.append((X, Y, R)) for i in range(0, len(g) - 1): for j in range(i + 1, len(g)): x1, y1, r1 = g[i] x2, y2, r2 = g[j] c = ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) ** 0.5 if c >= r1 + r2: mini = c - r1 - r2 maxi = c + r1 + r2 elif c < r1 + r2 and c > abs(r1 - r2): mini = 0 maxi = c + r1 + r2 elif c == abs(r1 - r2): mini = 0 maxi = c + r1 + r2 elif c < abs(r1 - r2): mini = abs(r1 - r2) - c maxi = c + r1 + r2 try: if mini == int(mini): karray[int(mini)] += 1 else: karray[int(mini) + 1] += 1 except: pass try: karray[int(maxi + 1)] -= 1 except: pass sum_arr = [karray[0]] for w in range(1, len(karray)): sum_arr.append(sum_arr[-1] + karray[w]) for query in range(Q): que = int(input()) print(sum_arr[que])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
A famous student of AESC MSU, as you know, comes from Kostomuksha. Kostomuksha has a popular game called Doka. The essence of Doka is as follows: You are given an array A and an integer X. You want to calculate how many subarrays of this array have a geometric mean of X. Formally, calculate the number of pairs of integers (L, R) such that 1 ≀ L ≀ R ≀ N and \sqrt[R-L+1]{A_{L} \cdot A_{L + 1} \cdot \ldots \cdot A_{R}} = X ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line of each test case contains two space-separated integers N, X β€” the size of the array A and the required geometric mean. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case, on a new line print the number of subarrays that satisfy the condition. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $1 ≀ X, A_{i} ≀ 10^{9}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 3 3 3 3 3 3 4 4 1 2 3 4 4 54 36 81 54 54 ----- Sample Output 1 ------ 6 1 6 ----- explanation 1 ------ Test case $1$: Every subarray has a geometric mean of $3$, and there are $6$ subarrays in total so the answer is $6$. Test case $2$: The only subarray with a geometric mean of $4$ is the singleton array $[4]$, obtained with $L = R = 4$. Test case $3$: The $6$ pairs of $(L, R)$ which have a geometric mean of $54$ are: - $(1, 2)$, giving the subarray $[36, 81]$ - $(1, 3)$, giving the subarray $[36, 81, 54]$ - $(1, 4)$, giving the subarray $[36, 81, 54, 54]$ - $(3, 3)$, giving the subarray $[54]$ - $(3, 4)$, giving the subarray $[54, 54]$ - $(4, 4)$, giving the subarray $[54]$
for _ in range(int(input())): list1 = list(map(int, input().split())) N, x = list1[0], list1[1] A = list(map(int, input().split())) flag = True for i in range(1, N): if A[i] != A[i - 1]: flag = False break if flag: print(N * (N + 1) // 2) continue else: count = 0 for i in range(N): product = A[i] / x if product == 1: count += 1 for j in range(i + 1, N): product = product * A[j] / x if product == 1: count += 1 print(count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A famous student of AESC MSU, as you know, comes from Kostomuksha. Kostomuksha has a popular game called Doka. The essence of Doka is as follows: You are given an array A and an integer X. You want to calculate how many subarrays of this array have a geometric mean of X. Formally, calculate the number of pairs of integers (L, R) such that 1 ≀ L ≀ R ≀ N and \sqrt[R-L+1]{A_{L} \cdot A_{L + 1} \cdot \ldots \cdot A_{R}} = X ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line of each test case contains two space-separated integers N, X β€” the size of the array A and the required geometric mean. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case, on a new line print the number of subarrays that satisfy the condition. ------ Constraints ------ $1 ≀ T ≀ 10^{3}$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $1 ≀ X, A_{i} ≀ 10^{9}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 3 3 3 3 3 3 4 4 1 2 3 4 4 54 36 81 54 54 ----- Sample Output 1 ------ 6 1 6 ----- explanation 1 ------ Test case $1$: Every subarray has a geometric mean of $3$, and there are $6$ subarrays in total so the answer is $6$. Test case $2$: The only subarray with a geometric mean of $4$ is the singleton array $[4]$, obtained with $L = R = 4$. Test case $3$: The $6$ pairs of $(L, R)$ which have a geometric mean of $54$ are: - $(1, 2)$, giving the subarray $[36, 81]$ - $(1, 3)$, giving the subarray $[36, 81, 54]$ - $(1, 4)$, giving the subarray $[36, 81, 54, 54]$ - $(3, 3)$, giving the subarray $[54]$ - $(3, 4)$, giving the subarray $[54, 54]$ - $(4, 4)$, giving the subarray $[54]$
from sys import stdin input = stdin.readline PRIME = 1000000007 def solve(N, X, A): count = 0 visited = set() for i in range(N): if i not in visited: product = 1 power = 1 n = 1 for j in range(i, N): product = product * A[j] % PRIME power = power * X % PRIME if product == power: count += n n += 1 visited.add(j + 1) return count T = int(input().strip()) for problem in range(1, T + 1): N, X = [int(x) for x in input().strip().split()] A = [int(x) for x in input().strip().split()] print(solve(N, X, A))
ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
from itertools import accumulate def check_valid(acc, ind, i): res = ind ind += 1 mult = 2 sm = acc[-1] try: while ind < len(acc) and abs(i * mult) <= abs(sm): ind2 = acc.index(i * mult, ind) res += ind2 - ind mult += 1 ind = ind2 + 1 res += len(acc) - ind return res except ValueError: return -1 for _ in range(int(input())): n = int(input()) li = list(map(int, input().split())) acc = list(accumulate(li)) acc_set = set(acc) sm = acc[-1] ans = [] if sm != 0: for ind, i in enumerate(acc): if ( i != 0 and sm % i == 0 and sm * i > 0 and set(range(i, sm + 1, i)) - acc_set == set() ): res = check_valid(acc, ind, i) if res != -1: ans.append(res) print(min(ans)) else: ind = acc.index(0) res = check_valid(acc, ind, 0) print(res)
FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = [] s = 0 for i in range(n): s += a[i] b.append(s) if s == 0: c = 0 for i in range(n): if b[i] != 0: c += 1 print(c) else: for i in range(n, 0, -1): if s % i == 0: d = s // i e = d for j in range(n): if b[j] == e: e += d if e - d == s: break if e - d == s: print(n - i) break
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
from itertools import accumulate from sys import stdin inp = stdin.readline T = int(inp()) for tx in range(T): N = int(inp()) S = list(map(int, inp().split())) acS = list(accumulate(S)) tot = acS[-1] if tot == 0: print(N - acS.count(0)) continue atot = abs(tot) partn = min(N, atot) while partn > 1: if tot % partn == 0: inc = tot // partn ix = 0 for v in range(inc, tot, inc): try: ix = acS.index(v, ix) except ValueError: break else: break partn = atot // (1 + atot // partn) print(N - partn)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
NEG = -(10**9) - 7 def factors(n): neg = False if n < 0: neg = True n = abs(n) ret = set() if n == 0: ret.add(0) return ret for i in range(1, n, 1): if i * i > n: break if n % i == 0: ret.add(i) ret.add(int(n / i)) if neg: nret = set() for i in ret: nret.add(0 - i) return nret return ret def checkComb(a, sm): ts = NEG sec = 0 for i in range(len(a)): if ts == NEG: ts = 0 ts += a[i] if ts == sm: sec += 1 ts = NEG if ts == NEG or ts == 0: return len(a) - sec if ts % abs(sm) == 0: mul = int(abs(ts) / abs(sm)) if sm * mul == ts: return -1 return len(a) - (sec - mul) return -1 t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] sm = sum(a) ans = len(a) - 1 if len(a) <= 1: print("0") continue fac = factors(sm) for f in fac: st = checkComb(a, f) if st >= 0 and st < ans: ans = st print(int(ans))
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
def partitions2(array, n, K): if e % K != 0: return False temp = 0 count = n - K c = False imp = True cursum = 0 for i in range(n): if count < 0: imp = False break if cursum + array[i] == e / K and temp <= K: temp += 1 c = False cursum = 0 elif cursum + array[i] == -e / K and temp > K: temp = temp - 1 c = False cursum = 0 else: cursum += array[i] if c: count = count - 1 c = True if count < 0: imp = False return temp == K and imp x = int(input()) for i in range(x): N = int(input()) l = list(map(int, input().split())) e = sum(l) for i in range(N, 0, -1): k = i if partitions2(l, N, k): val = k break print(N - val)
FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
T = int(input()) for _ in range(T): n = int(input()) A = list(map(int, input().split())) SUM = sum(A) op = -1 for i in range(n, 0, -1): if SUM % i == 0: if SUM != 0: target = SUM // i SS = SUM j = 0 sd = 0 flag = 0 while j < n: if sd + A[j] == target: SS = SS - (sd + A[j]) flag += 1 sd = 0 if SS == 0: break else: sd += A[j] j += 1 if flag * target == SUM: op = n - flag break else: target = SUM // i j = 0 sd = 0 flag = 0 while j < n: sd += A[j] if sd == target: flag += 1 sd = 0 j += 1 if flag * target == SUM: op = n - flag break if op != -1: print(op)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
import itertools import sys MAXN = 30000 SQRTMAXN = 175 ISPRIME = [1] * (MAXN + 1) PRIMES = [2] def sieve(): for i in range(MAXN // 2): ISPRIME[2 * i] = 0 ISPRIME[1] = 0 ISPRIME[2] = 1 for i in range(3, SQRTMAXN): if ISPRIME[i]: for j in range(i * i, MAXN + 1, 2 * i): ISPRIME[j] = 0 for i in range(3, MAXN + 1, 2): if ISPRIME[i]: PRIMES.append(i) sieve() def decomp(N): ANS = {} if N < 2: return {} for P in PRIMES: while N % P == 0: ANS[P] = ANS.get(P, 0) + 1 N //= P if N == 1: break if N > 1: ANS[N] = 1 return ANS def divisors(N): if N == 0: return set() D = set() dec = decomp(N) for xxx in itertools.product(*(range(x + 1) for x in dec.values())): M = 1 for p, powa in zip(dec.keys(), xxx): M *= p**powa D.add(M) return D def issubsequence(subseq, arr): a = 0 for s in range(len(subseq)): try: a = arr.index(subseq[s], a) + 1 except: return False return True def main(N, A): cumS = [0] * N for i in range(N): cumS[i] = cumS[i - 1] + A[i] if cumS[N - 1] == 0: return N - cumS.count(0) if cumS[N - 1] != 0: divi = sorted([d for d in divisors(abs(cumS[N - 1])) if d <= N], reverse=True) else: divi = sorted([d for d in range(1, N + 1)], reverse=True) for d in divi: subseq = [(cumS[N - 1] // d * k) for k in range(1, d + 1)] if issubsequence(subseq, cumS): return N - d T = int(input().strip()) for _ in range(T): N = int(input().strip()) A = [int(x) for x in input().strip().split()[:N]] print(main(N, A))
IMPORT IMPORT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT IF VAR NUMBER RETURN DICT FOR VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
from itertools import accumulate from sys import stdin inp = stdin.readline T = int(inp()) for tx in range(T): N = int(inp()) accA = list(accumulate(map(int, inp().split()))) tot = accA[-1] if tot == 0: print(N - accA.count(0)) continue atot = abs(tot) partn = min(N, atot) fails = [] best = 1 for partn in range(2, min(N, atot) + 1): if tot % partn == 0: for f in fails: if partn % f == 0: break else: inc = tot // partn ix = 0 for v in range(inc, tot, inc): try: ix = accA.index(v, ix) except ValueError: fails.append(partn) break else: best = partn print(N - best)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
def cal(x, ps): n = len(ps) mul = 0 i = 0 while i < n and mul // x < ps[-1] // x: if mul + x == ps[i]: mul += x i += 1 cnt = ps[-1] // x if mul == ps[-1] else 1 return n - cnt for t in range(int(input().strip())): n = int(input().strip()) a = tuple(map(int, input().strip().split())) ps = [a[0]] for i in range(1, n): ps.append(ps[i - 1] + a[i]) cnt0 = ps.count(0) ans = n - (cnt0 if ps[-1] == 0 else 1) f = 1 while f * f <= abs(ps[-1]): if abs(ps[-1]) % f == 0: ans = min(ans, cal(f * ps[-1] // abs(ps[-1]), ps), cal(ps[-1] // f, ps)) f += 1 print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR 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 LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
t = int(input()) for y in range(t): n = int(input()) arr = list(map(int, input().split())) adj = [] main_count = float("-inf") main_number = float("inf") big_fail = float("-inf") adj.append(arr[0]) i = 0 for x in arr[1:]: i = i + 1 adj.append(adj[i - 1] + arr[i]) i = -1 l = len(adj) if adj[-1] == 0: z = adj.count(0) if z > main_count: main_count = z else: for x in adj: i += 1 if x != 0 and (abs(x) > big_fail or len(adj) < 10000): if ( adj[-1] % x == 0 and adj[-1] / x > 0 and abs(x) < main_number and abs(adj[-1]) // abs(x) <= l - i ): num = x count = 1 for y in adj[i + 1 :]: if y - num == x: count += 1 num = y if num == adj[-1]: break if num == adj[-1]: if count > main_count: main_count = count main_number = abs(x) else: big_fail = abs(x) print(len(arr) - main_count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
def func(n, arr): total = sum(arr) if total == 0: a = [arr[0]] for i in range(1, n): a.append(a[i - 1] + arr[i]) i = a.index(total) moves = i curr = i for j in range(i + 1, n): if a[j] == total: moves += j - curr - 1 curr = j return moves curr = 0 d = {} for i in range(n): curr += arr[i] if d.get(curr) == None: d[curr] = [i] else: d[curr].append(i) for i in range(n, 0, -1): if total % i == 0: num = total // i curr = total index = n while curr != 0: if d.get(curr) != None: found = False for j in range(len(d[curr]) - 1, -1, -1): if d[curr][j] < index: index = d[curr][j] found = True break if not found: break curr -= num else: break if curr == 0: return n - i t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) print(func(n, arr))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
def poss(res, elem, len): cnt = 0 for i in res: if i == elem * (cnt + 1): cnt += 1 return cnt >= len for _ in range(int(input())): n = int(input()) ar = list(map(int, input().split())) res = [sum(ar[: i + 1]) for i in range(len(ar))] s = res[-1] ans = 0 if s == 0: cn = res.count(0) ans = n - cn else: sn = 1 if s > 0 else -1 s = abs(s) ans = n - 1 i = 1 while i * i <= s: if s % i != 0: i += 1 continue ln1 = i elem1 = s // i * sn ln2 = s // i elem2 = i * sn i += 1 if poss(res, elem1, ln1): ans = min(ans, n - ln1) if poss(res, elem2, ln2): ans = min(ans, n - ln2) print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A of length N. You can perform the following operation on the array, as long as it has more than one element: Choose any two adjacent elements, remove them from the array and insert their sum at that position. Formally, if the current length of the array is |A|, you can choose an index 1 ≀ i < |A|, and transform the array into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} + A_{i+1}, A_{i+2}, \ldots, A_{N}]. Note that after each operation, the length of array decreases by exactly 1. Print the minimum number of operations to be applied on array A such that all the elements in the resulting array are equal. See sample explanation for more details. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case consists of two lines of input. - The first line contains an integer N. - The second line contains N space-separated integers, the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of operations required to make all the elements equal. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 3 \cdot 10^{4}$ $-3 \cdot 10^{4} ≀ A_{i} ≀ 3 \cdot 10^{4}$ - Sum of $N$ over all test cases does not exceed $3 \cdot 10^{4}$ ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 4 3 5 2 3 2 6 9 5 3 6 5 4 9 3 3 3 3 ----- Sample Output 1 ------ 1 1 2 0 ----- explanation 1 ------ Test case $1$: It is optimal to remove $A_{2}$ and $A_{3}$ in the first operation, after which the array becomes $[5,5]$ β€” all of whose elements are equal. Test case $2$: Remove $A_{1}$ and $A_{2}$ after which the array becomes $[15]$, which contains equal elements because it is of length $1$. Test case $3$: First remove $A_{3}$ and $A_{4}$ after which the updated array $A$ becomes $[3,6,9,9]$. Now remove $A_{1}$ and $A_{2}$ after which the array becomes $[9,9,9]$. Test case $4$: The array elements are already equal.
import sys input = sys.stdin.readline def check(part_sum, k, A): curr_sum = None for i, a in enumerate(A): curr_sum = (0 if curr_sum is None else curr_sum) + a if k > 1 and curr_sum == part_sum: curr_sum = None k -= 1 return k == 1 and curr_sum == part_sum for _ in range(int(input().strip())): N = int(input().strip()) A = list(map(int, input().strip().split())) prefix = [0] * (N + 1) for i, a in enumerate(A): prefix[i + 1] = a + prefix[i] total = prefix[-1] for k in reversed(range(1, N + 1)): if total % k != 0: continue part_sum = total // k if check(part_sum, k, A): print(N - k) break
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NONE NUMBER VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NONE VAR NUMBER RETURN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR 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 FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for _ in range(int(input())): n = int(input()) s = input() x, y = map(int, input().split(" ")) curr = 0 for i in range(n): if s[i] == "S": curr = curr + min(x, 3 * y) if s[i] == "E": curr = curr + min(2 * x, 2 * y) if s[i] == "N": curr = curr + min(3 * x, y) if s[i] == "W": curr = curr + 0 ans = curr for i in range(n): if s[i] == "S": curr = curr - min(x, 3 * y) + min(y, 3 * x) if s[i] == "E": curr = curr - min(2 * x, 2 * y) if s[i] == "N": curr = curr - min(3 * x, y) + min(x, 3 * y) if s[i] == "W": curr = curr + min(2 * y, 2 * x) ans = min(ans, curr) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
def calc(ini, fin, x, y): if ini == fin: return 0 if ini == "W" and fin == "E" or ini == "E" and fin == "W": return min(2 * x, 2 * y) if ini == "N" and fin == "E" or ini == "S" and fin == "W": return min(x, 3 * y) return min(3 * x, y) for _ in range(int(input())): n = int(input()) titans = input() x, y = map(int, input().split()) leftTurn = [calc(titans[0], "W", x, y)] for i in range(1, n): leftTurn.append(leftTurn[-1] + calc(titans[i], "W", x, y)) rightTurn = [calc(titans[0], "E", x, y)] for i in range(1, n): rightTurn.append(rightTurn[-1] + calc(titans[i], "E", x, y)) eren = 0 m = leftTurn[-1] while eren < n: energy = leftTurn[n - 1] - leftTurn[eren] + rightTurn[eren] m = min(m, energy) eren += 1 print(m)
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR STRING VAR STRING VAR STRING VAR STRING RETURN FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER STRING VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR STRING VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER STRING VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR STRING VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
from sys import stdin input = stdin.readline def answer(): prefix, suffix = [0] * (n + 1), [0] * (n + 2) for i in range(n): if s[i] == "S": energy = min(y, 3 * x) elif s[i] == "N": energy = min(x, 3 * y) elif s[i] == "W": energy = min(2 * x, 2 * y) else: energy = 0 prefix[i + 1] = prefix[i] + energy for i in range(n - 1, -1, -1): if s[i] == "N": energy = min(y, 3 * x) elif s[i] == "S": energy = min(x, 3 * y) elif s[i] == "E": energy = min(2 * x, 2 * y) else: energy = 0 suffix[i + 1] = suffix[i + 2] + energy ans = float("inf") for i in range(n + 1): ans = min(ans, prefix[i] + suffix[i + 1]) return ans for T in range(int(input())): n = int(input()) s = input().strip() x, y = map(int, input().split()) print(answer())
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for i in range(int(input())): ui = int(input()) s = str(input()) x, y = map(int, input().split()) ans = [] dire = {"N": 0, "S": 0, "E": 0, "W": 0} for j in s: dire[j] += 1 t = min(x, y) if x > 3 * y: a = 3 * y else: a = x if y > 3 * x: b = 3 * x else: b = y dire2 = {"N": 0, "S": 0, "E": 0, "W": 0} for k in s: ans.append( dire2["N"] * a + dire2["S"] * b + dire2["W"] * 2 * t + dire["N"] * b + dire["S"] * a + dire["E"] * 2 * t ) dire2[k] += 1 dire[k] -= 1 ans.append( dire2["N"] * a + dire2["S"] * b + dire2["W"] * 2 * t + dire["N"] * b + dire["S"] * a + dire["E"] * 2 * t ) print(min(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR STRING VAR BIN_OP VAR STRING VAR BIN_OP BIN_OP VAR STRING NUMBER VAR BIN_OP VAR STRING VAR BIN_OP VAR STRING VAR BIN_OP BIN_OP VAR STRING NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR STRING VAR BIN_OP VAR STRING VAR BIN_OP BIN_OP VAR STRING NUMBER VAR BIN_OP VAR STRING VAR BIN_OP VAR STRING VAR BIN_OP BIN_OP VAR STRING NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for _ in range(int(input())): n = int(input()) s = input() x, y = map(int, input().split()) arr = [0] sum = 0 for i in range(n): if s[i] == "N": sum += min(x, 3 * y) arr.append(sum) elif s[i] == "W": sum += min(2 * x, 2 * y) arr.append(sum) elif s[i] == "S": sum += min(3 * x, y) arr.append(sum) elif s[i] == "E": arr.append(sum) sum = 0 for i in range(n - 1, -1, -1): if s[i] == "N": sum += min(3 * x, y) arr[i] += sum elif s[i] == "W": arr[i] += sum elif s[i] == "S": sum += min(x, 3 * y) arr[i] += sum elif s[i] == "E": sum += min(x * 2, y * 2) arr[i] += sum print(min(arr))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR IF VAR VAR STRING VAR VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
t = int(input()) for i in range(t): N = int(input()) s = str(input()) x, y = map(int, input().split()) a = min(x, 3 * y) b = min(y, 3 * x) c = min(x, y) east = list() west = list() min_ene = 0 for i in range(len(s)): if s[i] == "N": east.append(a) west.append(b) elif s[i] == "S": east.append(b) west.append(a) elif s[i] == "E": east.append(0) west.append(2 * c) elif s[i] == "W": east.append(2 * c) west.append(0) f = sum(west) k = [f] minimum = f for i in range(N): f = f - west[i] + east[i] k.append(f) if f < minimum: minimum = f print(minimum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
def distance(ch, x, y): if ch == "E": east = 0 west = min(2 * x, 2 * y) elif ch == "W": east = min(2 * x, 2 * y) west = 0 elif ch == "N": east = min(x, 3 * y) west = min(y, 3 * x) else: east = min(y, 3 * x) west = min(x, 3 * y) return east, west t = int(input()) for _ in range(t): n = int(input()) arr = list(input()) x, y = map(int, input().split()) dist = [distance(arr[i], x, y) for i in range(n)] prefix_east = [0] prefix_west = [0] for i in range(n): prefix_east.append(prefix_east[-1] + dist[i][0]) prefix_west.append(prefix_west[-1] + dist[n - 1 - i][1]) prefix_west = prefix_west[::-1] print(min(prefix_west[i] + prefix_east[i] for i in range(n + 1)))
FUNC_DEF IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
T = int(input()) for i in range(T): clockwise = {"W": (0, 2), "N": (3, 1), "E": (2, 0), "S": (1, 3)} anti_clockwise = {"W": (0, 2), "N": (1, 3), "E": (2, 0), "S": (3, 1)} N = int(input()) directions = input() X, Y = map(int, input().split()) base_energy = 0 for direction in directions: base_energy += min( clockwise[direction][0] * X, anti_clockwise[direction][0] * Y ) min_energy = base_energy enrg = base_energy for direction in directions: enrg -= min(clockwise[direction][0] * X, anti_clockwise[direction][0] * Y) enrg += min(clockwise[direction][1] * X, anti_clockwise[direction][1] * Y) if enrg < min_energy: min_energy = enrg print(min_energy)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for _ in range(int(input())): n = int(input()) a = input() x, y = map(int, input().split()) east = [0] west = [0] * n + [0] for i in range(n): if a[i] == "E": east.append(east[-1] + 0) elif a[i] == "W": east.append(east[-1] + 2 * min(x, y)) elif a[i] == "N": east.append(east[-1] + min(x, 3 * y)) else: east.append(east[-1] + min(y, 3 * x)) for i in range(n - 1, -1, -1): if a[i] == "E": west[i] = west[i + 1] + 2 * min(x, y) elif a[i] == "W": west[i] = west[i + 1] elif a[i] == "N": west[i] = west[i + 1] + min(y, 3 * x) else: west[i] = west[i + 1] + min(x, 3 * y) ans = min(east[-1], west[0]) for i in range(1, n): ans = min(ans, east[i] + west[i]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for _ in range(int(input())): N = int(input()) S = input() X, Y = map(int, input().split()) to_west, to_east = [], [] directions = ["N", "E", "S", "W"] for i in range(N): titan = directions.index(S[i]) clockwise_west = (3 - titan) * X counter_clockwise_west = (titan + 1) % 4 * Y clockwise_east = (1 - titan) % 4 * X counter_clockwise_east = (titan - 1) % 4 * Y west_power = min(clockwise_west, counter_clockwise_west) east_power = min(clockwise_east, counter_clockwise_east) to_west.append(west_power) to_east.append(east_power) w, e = sum(to_west), 0 ans = w + e for i in range(N): w -= to_west[i] e += to_east[i] ans = min(ans, w + e) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR LIST STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
from sys import stdin, stdout nmbr = lambda: int(input()) lst = lambda: list(map(int, input().split())) for _ in range(nmbr()): n = nmbr() s = input() clock, anti = lst() east = {"E": 0} east["W"] = 2 * min(clock, anti) east["N"] = min(clock, 3 * anti) east["S"] = min(3 * clock, anti) west = {"W": 0} west["E"] = 2 * min(clock, anti) west["N"] = min(3 * clock, anti) west["S"] = min(clock, 3 * anti) pre = [0] * n pre[0] = east[s[0]] for i in range(1, n): pre[i] = pre[i - 1] + east[s[i]] suf = [0] * n suf[n - 1] = west[s[-1]] for i in range(n - 2, -1, -1): suf[i] = suf[i + 1] + west[s[i]] ans = min(pre[-1], suf[0]) for i in range(n - 1): ans = min(ans, pre[i] + suf[i + 1]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR DICT STRING NUMBER ASSIGN VAR STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR STRING FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR DICT STRING NUMBER ASSIGN VAR STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR STRING FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
k = int(input()) for x in range(0, k): n = input() string = str(input()) a1 = input().split() clock = int(a1[0]) anticlock = int(a1[1]) toeast = [] towest = [] for i in string: if i == "E": toeast.append(0) elif i == "S": toeast.append(min(anticlock, 3 * clock)) elif i == "N": toeast.append(min(clock, 3 * anticlock)) elif i == "W": toeast.append(min(clock * 2, anticlock * 2)) for i in string: if i == "W": towest.append(0) elif i == "S": towest.append(min(clock, 3 * anticlock)) elif i == "N": towest.append(min(anticlock, 3 * clock)) elif i == "E": towest.append(min(2 * anticlock, 2 * clock)) minn = sum(towest) sumk = sum(towest) for i, j in enumerate(toeast): sumk += toeast[i] - towest[i] minn = min(sumk, minn) print(minn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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 VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for _ in range(int(input())): n = int(input()) s = input() x, y = map(int, input().split()) left = [0] * n right = [0] * n for i in range(n): if s[i] == "N": left[i] = min(x * 3, y) right[i] = min(x, y * 3) elif s[i] == "S": left[i] = min(x, y * 3) right[i] = min(x * 3, y) elif s[i] == "E": left[i] = min(x * 2, y * 2) else: right[i] = min(x * 2, y * 2) for i in range(1, n): right[i] += right[i - 1] for i in range(n - 2, -1, -1): left[i] += left[i + 1] ans = float("inf") ans = min(right[-1], left[0]) for i in range(n - 1): ans = min(ans, right[i] + left[i + 1]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
t = int(input()) for _ in range(t): n = int(input()) S = list(input()) X, Y = map(int, input().split()) arr = [] for i in range(n): a = [0, 0] if S[i] == "S": a[0] = min(3 * X, Y) a[1] = min(X, 3 * Y) elif S[i] == "N": a[0] = min(X, 3 * Y) a[1] = min(3 * X, Y) elif S[i] == "W": a[0] = min(2 * X, 2 * Y) a[1] = 0 elif S[i] == "E": a[1] = min(2 * X, 2 * Y) a[0] = 0 arr.append(a) a = 0 b = 0 for i in range(n): arr[i][0] += a a = arr[i][0] arr[n - i - 1][1] += b b = arr[n - i - 1][1] ans = min(arr[0][1], arr[-1][0]) for i in range(n - 1): ans = min(ans, arr[i][0] + arr[i + 1][1]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
T = int(input()) for t in range(T): n = int(input()) s = input() x, y = [int(i) for i in input().split()] prefse = [(0) for i in range(n + 2)] prefsw = [(0) for i in range(n + 2)] prefsn = [(0) for i in range(n + 2)] prefss = [(0) for i in range(n + 2)] sufse = [(0) for i in range(n + 2)] sufsw = [(0) for i in range(n + 2)] sufsn = [(0) for i in range(n + 2)] sufss = [(0) for i in range(n + 2)] reqd = 4 * x * n for i in range(n): prefse[i + 1] = prefse[i] + int(s[i] == "E") prefsw[i + 1] = prefsw[i] + int(s[i] == "W") prefsn[i + 1] = prefsn[i] + int(s[i] == "N") prefss[i + 1] = prefss[i] + int(s[i] == "S") for i in range(n - 1, -1, -1): sufse[i] = sufse[i + 1] + int(s[i] == "E") sufsw[i] = sufsw[i + 1] + int(s[i] == "W") sufsn[i] = sufsn[i + 1] + int(s[i] == "N") sufss[i] = sufss[i + 1] + int(s[i] == "S") for eren in range(n + 1): left = 0 right = 0 easttoeast = 0 westtoeast = min(2 * x, 2 * y) * prefsw[eren] northtoeast = min(x, 3 * y) * prefsn[eren] southtoeast = min(y, 3 * x) * prefss[eren] left = westtoeast + northtoeast + southtoeast easttowest = min(2 * x, 2 * y) * sufse[eren] northtowest = min(y, 3 * x) * sufsn[eren] southtowest = min(x, 3 * y) * sufss[eren] right = easttowest + northtowest + southtowest reqd = min(reqd, left + right) print(reqd)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
import sys input = sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip("\n") def lis(): return list(map(int, input().split())) def ma(): return map(int, input().split()) t = inp() while t: t -= 1 n = inp() s = st() x, y = ma() pre = [0] suf = [0] for i in range(n): if s[i] == "N": pre.append(pre[-1] + min(x, 3 * y)) if s[i] == "S": pre.append(pre[-1] + min(x * 3, y)) if s[i] == "E": pre.append(pre[-1] + min(x, 3 * y, 0)) if s[i] == "W": pre.append(pre[-1] + min(2 * x, 2 * y)) for i in range(n - 1, -1, -1): if s[i] == "N": suf.append(suf[-1] + min(x * 3, y)) if s[i] == "S": suf.append(suf[-1] + min(x, y * 3)) if s[i] == "E": suf.append(suf[-1] + min(2 * x, 2 * y)) if s[i] == "W": suf.append(suf[-1] + min(2 * x, 2 * y, 0)) suf = suf[1:][::-1] suf.append(0) mi = 1000000000000000000000 for i in range(n + 1): mi = min(mi, pre[i] + suf[i]) print(mi)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
def solve(N, S, X, Y): east_align, west_align = [0] * N, [0] * N cost = { "EE": 0, "WW": 0, "NE": min(X, 3 * Y), "NW": min(Y, 3 * X), "SE": min(Y, 3 * X), "SW": min(X, 3 * Y), "WE": 2 * min(X, Y), "EW": 2 * min(X, Y), } for i in range(N): east_align[i] = cost[S[i] + "E"] west_align[i] = cost[S[i] + "W"] cost = sum(west_align) min_cost = cost for i in range(N): cost = cost + east_align[i] - west_align[i] if cost < min_cost: min_cost = cost return min_cost T = int(input()) for _ in range(T): N = int(input()) S = input() X, Y = list(map(int, input().split())) print(solve(N, S, X, Y))
FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
def func(i, X, Y): if i == "N": return min(X * 3, Y) elif i == "E": return min(X * 2, Y * 2) elif i == "W": return 0 else: return min(X * 1, Y * 3) def func2(i, X, Y): if i == "N": return min(X * 1, Y * 3) elif i == "E": return 0 elif i == "W": return min(X * 2, Y * 2) else: return min(X * 3, Y * 1) for t in range(int(input())): N = int(input()) S = list(input()) X, Y = map(int, input().split()) W, E = [], [] for i in S: W.append(func(i, X, Y)) for i in S: E.append(func2(i, X, Y)) ans = min(sum(E), sum(W)) suf = [] for i in range(len(W) - 1, -1, -1): if not suf: suf.append(W[i]) else: suf.append(W[i] + suf[-1]) suf = suf[::-1] sm = 0 for i in range(N - 1): sm += E[i] ans = min(sm + suf[i + 1], ans) print(ans)
FUNC_DEF IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
def min_energy(p): if p == "N": return min(x, 3 * y), min(3 * x, y) elif p == "E": return 0, min(2 * x, 2 * y) elif p == "S": return min(3 * x, y), min(x, 3 * y) else: return min(2 * x, 2 * y), 0 t = int(input()) for _ in range(t): n = int(input()) q = list(input()) a = [] x, y = map(int, input().split()) for p in q: e, w = min_energy(p) a.append([e, w]) for i in range(1, n): a[i][0] += a[i - 1][0] for i in range(n - 2, -1, -1): a[i][1] += a[i + 1][1] b = min(a[0][1], a[-1][0]) for i in range(n - 1): b = min(b, a[i][0] + a[i + 1][1]) print(b)
FUNC_DEF IF VAR STRING RETURN FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR IF VAR STRING RETURN NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR STRING RETURN FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
import sys for tc in range(int(input())): n = int(input()) ls = list(input()) lk = list(map(int, input().split())) x = lk[0] y = lk[1] nums = [] pre = [] dis = {"E": 0, "W": 1, "N": 2, "S": 3} lp = [] lp.append("#") te = tw = tn = ts = 0 for g in ls: lp.append(g) lp.append("#") if g == "S": ts += 1 if g == "N": tn += 1 if g == "E": te += 1 if g == "W": tw += 1 ce = cw = cn = cs = 0 mincost = sys.maxsize for g in range(len(lp)): if lp[g] == "S": cs += 1 if lp[g] == "N": cn += 1 if lp[g] == "E": ce += 1 if lp[g] == "W": cw += 1 if lp[g] == "#": if g == 0: coste = min(te * x * 2, te * y * 2) costn = min(tn * x * 3, tn * y) costs = min(ts * x, ts * y * 3) mincost = min(mincost, coste + costn + costs) elif g == len(lp) - 1: costw = min(cw * x * 2, cw * y * 2) costn = min(cn * x, cn * y * 3) costs = min(cs * x * 3, cs * y) mincost = min(mincost, costw + costn + costs) else: cost0e = min((te - ce) * x * 2, (te - ce) * y * 2) cost0n = min((tn - cn) * x * 3, (tn - cn) * y) cost0s = min((ts - cs) * x, (ts - cs) * y * 3) cost1w = min(cw * x * 2, cw * y * 2) cost1n = min(cn * x, cn * y * 3) cost1s = min(cs * x * 3, cs * y) mincost = min( mincost, cost1w + cost1n + cost1s + cost0e + cost0n + cost0s ) print(mincost)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for _ in range(int(input())): N = int(input()) S = input() R, L = list(map(int, input().split())) energy = [] east_vals = {"N": min(R, 3 * L), "S": min(L, 3 * R), "E": 0, "W": min(2 * L, 2 * R)} west_vals = {"N": min(L, 3 * R), "S": min(R, 3 * L), "W": 0, "E": min(2 * L, 2 * R)} sm1 = 0 sm2 = 0 for i in range(N): sm1 += east_vals[S[i]] ans = sm1 for i in range(N - 1, -1, -1): ans = min(ans, sm1 + sm2) sm1 -= east_vals[S[i]] sm2 += west_vals[S[i]] ans = min(ans, sm1 + sm2) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT STRING STRING STRING STRING FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR DICT STRING STRING STRING STRING FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
def cost(a, b): if a == "W": if b == "W": return 0 elif b == "N": return 1 elif b == "E": return 2 elif b == "S": return 3 if a == "E": if b == "E": return 0 elif b == "N": return 3 elif b == "W": return 2 elif b == "S": return 1 t = int(input()) while t > 0: t -= 1 n = int(input()) s = input() ip = input().split() x, y = [int(i) for i in ip] en = 0 for i in s: en += min(cost("W", i) * y, (4 - cost("W", i)) * x) newen = en + 0 for i in s: newen -= min(cost("W", i) * y, (4 - cost("W", i)) * x) newen += min(cost("E", i) * y, (4 - cost("E", i)) * x) if newen < en: en = newen print(en)
FUNC_DEF IF VAR STRING IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR STRING VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR STRING VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
def theRumbling(N, D, X, Y): E = [] W = [] for i in range(N): if D[i] == "S": W.append(min(X, 3 * Y)) E.append(min(Y, 3 * X)) elif D[i] == "N": W.append(min(Y, 3 * X)) E.append(min(X, 3 * Y)) elif D[i] == "W": W.append(0) E.append(min(2 * X, 2 * Y)) else: E.append(0) W.append(min(2 * X, 2 * Y)) ans = sum(W) m = sum(W) for i in range(1, N + 1): m += E[i - 1] - W[i - 1] ans = min(ans, m) return ans for _ in range(int(input())): N = int(input()) D = input() X, Y = map(int, input().split()) print(theRumbling(N, D, X, Y))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
T = int(input()) for i in range(T): N = int(input()) a = input() [X, Y] = [int(k) for k in input().split()] r = [] q = 0 for j in range(N): if a[j] == "S": p = min(3 * X, Y) if a[j] == "W": p = min(2 * X, 2 * Y) if a[j] == "N": p = min(X, 3 * Y) if a[j] == "E": p = 0 q = q + p r.append(q) l = [0] * N q = 0 for j in range(N - 1, -1, -1): if a[j] == "S": p = min(X, 3 * Y) if a[j] == "W": p = 0 if a[j] == "N": p = min(3 * X, Y) if a[j] == "E": p = min(2 * X, 2 * Y) q = q + p l[j] = q l = l + [0] r = [0] + r mx = l[0] + r[0] for j in range(N + 1): p = l[j] + r[j] if p < mx: mx = p print(mx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for i in range(int(input())): n = int(input()) s = input() x, y = map(int, input().split()) def turne(s, x, y): if s == "E": return 0 if s == "N": return min(x, 3 * y) if s == "S": return min(y, 3 * x) else: return min(2 * y, 2 * x) def turnw(s, x, y): if s == "W": return 0 if s == "N": return min(y, 3 * x) if s == "S": return min(x, 3 * y) else: return min(2 * y, 2 * x) lis = [0] su = 0 for i in range(len(s)): su += turne(s[i], x, y) lis.append(su) su = 0 for i in range(len(s), 0, -1): su += turnw(s[i - 1], x, y) lis[i - 1] += su print(min(lis))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR STRING RETURN FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR STRING RETURN FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR STRING RETURN FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR STRING RETURN FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for _ in range(int(input())): n = int(input()) s = input() x, y = map(int, input().split()) total = 0 a = [0] for i in range(n): if s[i] == "N": total += min(x, 3 * y) a.append(total) elif s[i] == "S": total += min(3 * x, y) a.append(total) elif s[i] == "E": a.append(total) elif s[i] == "W": total += min(2 * x, 2 * y) a.append(total) total = 0 for i in reversed(range(n)): if s[i] == "S": total += min(x, 3 * y) a[i] += total elif s[i] == "N": total += min(3 * x, y) a[i] += total elif s[i] == "W": a[i] += total elif s[i] == "E": total += min(2 * x, 2 * y) a[i] += total print(min(a))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR IF VAR VAR STRING VAR VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
T = int(input("")) for _ in range(T): N = int(input("")) line = input("") X, Y = map(int, input("").rstrip().split(" ")) costs = {} DIRECTIONS = {"N": 0, "E": 1, "S": 2, "W": 3} for direction_val in DIRECTIONS.values(): for dest_direction in ("E", "W"): dest_direction_val = DIRECTIONS[dest_direction] costs[direction_val, dest_direction_val] = min( X * ((dest_direction_val - direction_val) % 4), Y * ((direction_val - dest_direction_val) % 4), ) min_sum = 0 for i in range(N): min_sum += costs[DIRECTIONS[line[i]], DIRECTIONS["W"]] curr_sum = min_sum for i in range(N): curr_sum = ( curr_sum - costs[DIRECTIONS[line[i]], DIRECTIONS["W"]] + costs[DIRECTIONS[line[i]], DIRECTIONS["E"]] ) if min_sum > curr_sum: min_sum = curr_sum print(min_sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR DICT ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FOR VAR STRING STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR STRING VAR VAR VAR VAR VAR STRING IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
def check(n, string, a, b): arr = [0] add = 0 for i in range(n): if string[i] is "N": add += min(a, 3 * b) arr.append(add) elif string[i] is "W": add += min(2 * a, 2 * b) arr.append(add) elif string[i] is "S": add += min(3 * a, b) arr.append(add) elif string[i] is "E": arr.append(add) add = 0 for i in range(n - 1, -1, -1): if string[i] is "N": add += min(3 * a, b) arr[i] += add elif string[i] is "E": add += min(2 * a, 2 * b) arr[i] += add elif string[i] is "S": add += min(a, 3 * b) arr[i] += add elif string[i] is "W": arr[i] += add return min(arr) for _ in range(int(input())): n = int(input()) string = input() a, b = map(int, input().split()) ans = check(n, string, a, b) print(ans)
FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR IF VAR VAR STRING VAR VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for i in range(int(input())): n = int(input()) direction = input()[:n] x, y = map(int, input().split()) energy = 0 for item in direction: if item == "E": energy += min(x * 2, y * 2) elif item == "W": energy += 0 elif item == "N": energy += min(y, 3 * x) elif item == "S": energy += min(3 * y, x) ans = energy for item in direction: if item == "E": energy = energy - min(x * 2, y * 2) + 0 elif item == "W": energy = energy - 0 + min(2 * y, 2 * x) elif item == "N": energy = energy - min(y, 3 * x) + min(x, 3 * y) elif item == "S": energy = energy - min(3 * y, x) + min(3 * x, y) ans = min(ans, energy) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for _ in range(int(input())): n = int(input()) a = input() x, y = list(map(int, input().split(" "))) r, l = [0], [0] for i in a: if i == "N": r.append(min(x, y * 3)) l.append(min(y, x * 3)) if i == "S": r.append(min(y, x * 3)) l.append(min(x, y * 3)) if i == "E": r.append(0) l.append(min(x * 2, y * 2)) if i == "W": r.append(min(2 * x, 2 * y)) l.append(0) for i in range(1, n + 1): l[i] = l[i] + l[i - 1] r[i] = r[i] + r[i - 1] zzans = 1e18 + 7 for i in range(n + 1): lans = l[n] - l[i] rans = r[i] zzans = min(zzans, lans + rans) print(zzans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR LIST NUMBER LIST NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for _ in range(int(input())): n = int(input()) s = input() y, x = map(int, input().split()) left, reverse, right = min(x, 3 * y), min(2 * x, 2 * y), min(3 * x, y) dic = {"N": 0, "S": 0, "E": 0, "W": 0} for v in s: dic[v] += 1 cost = dic["N"] * left + dic["E"] * reverse + dic["S"] * right minim = cost for v in s: if v == "N": cost = cost - left + right elif v == "E": cost -= reverse elif v == "S": cost = cost - right + left else: cost += reverse minim = min(minim, cost) print(minim)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR STRING VAR BIN_OP VAR STRING VAR BIN_OP VAR STRING VAR ASSIGN VAR VAR FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR STRING VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for t in range(int(input())): l = int(input()) p = input() x, y = map(int, input().split()) n = [0] * l s = [0] * l e = [0] * l w = [0] * l a = 0 b = 0 c = 0 d = 0 for i in range(l): if p[i] == "N": a += 1 elif p[i] == "S": b += 1 elif p[i] == "E": c += 1 elif p[i] == "W": d += 1 n[i] = a s[i] = b e[i] = c w[i] = d m = min( n[-1] * min(x, 3 * y) + w[-1] * min(2 * x, 2 * y) + s[-1] * min(3 * x, y), n[-1] * min(3 * x, y) + s[-1] * min(x, 3 * y) + e[-1] * min(2 * x, 2 * y), ) for i in range(l): u = n[i] * min(x, 3 * y) + w[i] * min(2 * x, 2 * y) + s[i] * min(3 * x, y) v = ( (n[-1] - n[i]) * min(3 * x, y) + (s[-1] - s[i]) * min(x, 3 * y) + (e[-1] - e[i]) * min(2 * x, 2 * y) ) m = min(m, u + v) print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
from itertools import combinations for _ in range(int(input())): n = int(input()) soldger = list(input()) clock, anitClock = list(map(int, input().split())) east = [] west = [] for i in soldger: if i == "N": west.append(min(anitClock, 3 * clock)) east.append(min(clock, 3 * anitClock)) elif i == "S": east.append(min(anitClock, 3 * clock)) west.append(min(clock, 3 * anitClock)) elif i == "E": east.append(0) west.append(2 * min(clock, anitClock)) elif i == "W": west.append(0) east.append(2 * min(clock, anitClock)) x = 0 y = 0 LtoR_east = [0] RtoL_west = [] for idx in range(n): x += east[idx] y += west[n - idx - 1] LtoR_east.append(x) RtoL_west.append(y) RtoL_west = RtoL_west[::-1] RtoL_west.append(0) ans = float("inf") for i in range(n + 1): ans = min(ans, LtoR_east[i] + RtoL_west[i]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
for _ in range(int(input())): n = int(input()) s = input() x, y = map(int, input().split()) li = [] E = [min(x, 3 * y), 0, min(3 * x, y), min(2 * x, 2 * y)] W = [min(3 * x, y), min(2 * x, 2 * y), min(x, 3 * y), 0] data = {"N": 0, "E": 1, "S": 2, "W": 3} for i in s: li.append([E[data[i]], W[data[i]]]) res = sum([i[1] for i in li]) mini = res for i in li: res = res - i[1] + i[0] mini = min(res, mini) print(mini)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
def find_east(x, clock, anti): if x == "E": return 0 elif x == "S": if anti < clock * 3: return anti else: return clock * 3 elif x == "N": if anti * 3 < clock: return anti * 3 else: return clock elif anti < clock: return anti * 2 else: return clock * 2 def find_west(x, clock, anti): if x == "W": return 0 elif x == "S": if anti * 3 < clock: return anti * 3 else: return clock elif x == "N": if anti < clock * 3: return anti else: return clock * 3 elif anti < clock: return anti * 2 else: return clock * 2 def solve(string, clock, anti): east = [] west = [] for x in string: a = find_east(x, clock, anti) east.append(a) c = find_west(x, clock, anti) west.append(c) e = 0 w = sum(west) best = w for x in range(len(string)): e += east[x] w -= west[x] if e + w < best: best = e + w print(best) test = int(input()) for _ in range(test): n = int(input()) string = input() clock, anti = [int(x) for x in input().split()] solve(string, clock, anti)
FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR STRING IF VAR BIN_OP VAR NUMBER RETURN VAR RETURN BIN_OP VAR NUMBER IF VAR STRING IF BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER RETURN VAR IF VAR VAR RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR STRING IF BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER RETURN VAR IF VAR STRING IF VAR BIN_OP VAR NUMBER RETURN VAR RETURN BIN_OP VAR NUMBER IF VAR VAR RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
def calcmin(n, e, w, s): ans = 0 ans += min(n * x, n * y * 3) ans += min(w * 2 * x, w * y * 2) ans += min(s * x * 3, s * y) return ans def calcmin1(n, e, w, s): ans = 0 ans += min(n * x * 3, n * y) ans += min(e * 2 * x, e * y * 2) ans += min(s * x, s * y * 3) return ans t = int(input()) for jj in range(t): nn = int(input()) a = list(input()) x, y = map(int, input().split()) e, w, n, s = 0, 0, 0, 0 e1, w1, n1, s1 = a.count("E"), a.count("W"), a.count("N"), a.count("S") ans = float("inf") for i in range(nn): ans = min(calcmin(n, e, w, s) + calcmin1(n1, e1, w1, s1), ans) if a[i] == "S": s += 1 s1 -= 1 elif a[i] == "W": w += 1 w1 -= 1 elif a[i] == "E": e += 1 e1 -= 1 elif a[i] == "N": n += 1 n1 -= 1 ans = min(calcmin(n, e, w, s) + calcmin1(n1, e1, w1, s1), ans) print(ans)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
T = int(input()) for t in range(T): n = int(input()) directions = input() cost_turn_cw, cost_turn_ccw = [int(x) for x in input().split()] cost_to_face_left = [] cost_to_face_right = [] for direction in directions: if direction == "E": cost_to_face_right += [0] cost_to_face_left += [2 * min(cost_turn_ccw, cost_turn_cw)] elif direction == "N": cost_to_face_right += [min(cost_turn_cw, 3 * cost_turn_ccw)] cost_to_face_left += [min(cost_turn_ccw, 3 * cost_turn_cw)] elif direction == "W": cost_to_face_right += [2 * min(cost_turn_cw, cost_turn_ccw)] cost_to_face_left += [0] else: cost_to_face_left += [min(cost_turn_cw, 3 * cost_turn_ccw)] cost_to_face_right += [min(cost_turn_ccw, 3 * cost_turn_cw)] cost = sum(cost_to_face_left) ans = cost for i in range(n): cost += cost_to_face_right[i] - cost_to_face_left[i] ans = min(ans, cost) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR STRING VAR LIST NUMBER VAR LIST BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF VAR STRING VAR LIST FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR LIST FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR STRING VAR LIST BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR LIST NUMBER VAR LIST FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR LIST FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
def enToFaceEast(d, X, Y): if d == "E": return 0 elif d == "N": return min(X * 1, Y * 3) elif d == "W": return min(X * 2, Y * 2) else: return min(X * 3, Y * 1) def enToFaceWest(d, X, Y): if d == "E": return min(X * 2, Y * 2) elif d == "N": return min(X * 3, Y * 1) elif d == "W": return 0 else: return min(X * 1, Y * 3) T = int(input()) for _ in range(T): N = int(input()) S = input() X, Y = map(int, input().split()) E = 0 for d in S: E += enToFaceWest(d, X, Y) minE, altE = E, E for d in S: altE = E - enToFaceWest(d, X, Y) + enToFaceEast(d, X, Y) if altE < minE: minE = altE E = altE print(minE)
FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The Rumbling is a hypothetical cataclysmic event involving Karl Fritz's Wall Titans marching across the Earth, destroying all life upon it. Eren Yeager who possesses the Founding Titan has decided to begin The Rumbling to show the true power of the Eldians to the rest of the world. There are N Titans standing in a line from west to east. Each of the Titans is facing one of the four directions i.e., North, South, East, or West, represented by uppercase English letters 'N', 'S', 'E', and 'W' respectively. A Titan can rotate on the spot where it is standing. It requires X units of energy to rotate 90 degrees clockwise, and Y units of energy to rotate 90 degrees anti-clockwise. For the activation of The Rumbling, all the Titans must face Eren Yeager. So, Eren wants to position himself on the West-East line (on which other Titans are standing). He can either stand to the left of all Titans, in between two consecutive Titans, or to the right of all Titans. He wants to choose a position that minimizes the units of energy required by all the Titans to rotate and face him. Find the minimum units of energy required by all the Titans for the Activation of the Rumbling in the optimal configuration. ------ Input Format ------ - The first line consists of T, the number of test cases. Description of the test cases follow. - The first line of each test case will contain N, the number of Titans. - The second line of each test case will contain a string S of length N, where the i^{th} character represents the directions faced by the i^{th} Titan. - The third line of each test case will contain two space-separated integers, the values of X and Y. ------ Output Format ------ For each test case, output a single line containing one integer, the answer to that test case. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 2 * 10^{5}$ $1 ≀ X, Y ≀ 10^{9}$ $S$ contains characters from {'N', 'S', 'E', 'W'} only. - Sum of $N$ over all test cases doesn't exceed $10^{6}$ ----- Sample Input 1 ------ 1 5 SENWS 2 1 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - One valid configuration would be as follows: $EE \cdot WWW$, where $\cdot$ represents the position of Eren Yeager. - The first and the second Titans will face Eren at East from the left while the third, fourth, and the fifth Titans will face Eren at West from the right. - It takes $1$ unit of energy for the first Titan to move towards East by rotating $90$ degrees anticlockwise. - The second Titan is already facing East, so it costs him $0$ units of energy to stay that way. - It takes $1$ unit of energy for the third Titan to move towards West by rotating $90$ degrees anticlockwise. - The fourth Titan is already facing West, so it costs him $0$ units of energy to stay that way. - It takes $2$ units of energy for the fifth Titan to move towards West by rotating $90$ degrees clockwise. - So, it takes the Titans $4$ units of energy overall to face Eren Yeager, and activate The Rumbling!
test = int(input()) for i in range(0, test): n = int(input()) s = input() x, y = map(int, input().split()) west = [] east = [] for i in s: if i == "E": west.append(2 * min(x, y)) east.append(0) elif i == "N": west.append(min(y, 3 * x)) east.append(min(x, 3 * y)) elif i == "S": west.append(min(x, 3 * y)) east.append(min(y, 3 * x)) else: west.append(0) east.append(2 * min(x, y)) find = [] g = sum(west) mini = g k = [g] for i in range(0, n): g = g - west[i] + east[i] k.append(g) if g < mini: mini = g print(mini)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR