description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
def solve(s): ans = 0 if s.count(1) in (len(s), 0): s[0] = 1 - s[0] ans += 1 start = 0 groups = [] for i in range(len(s) + 1): if i == len(s) or s[i] != s[start]: groups.append(i - start) start = i if len(s) > 1 and s[0] == s[-1]: groups[0] += groups[-1] groups.pop() for group in groups: ans += group // 3 return ans for _ in range(int(input())): _ = int(input()) s = input() s = [(1 if ch == "R" else 0) for ch in s] print(solve(s))
FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR 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 VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
def find_cost_of_reordering(directions): assert len(directions) >= 3 if len(set(directions)) == 1: return 1 + (len(directions) - 1) // 3 chains_lengths = _extract_lengths_of_chains_of_same_directions(directions) cost = sum( chain_length // 3 for chain_length in chains_lengths if chain_length >= 3 ) return cost _DIRECTION_SAME = "S" _DIRECTION_CHANGED = "C" def _extract_lengths_of_chains_of_same_directions(directions): if len(set(directions)) == 1: return [len(directions)] directions_shifted_left = directions[1:] + directions[0] directions_diffs = "".join( _DIRECTION_SAME if curr_dir == next_dir else _DIRECTION_CHANGED for curr_dir, next_dir in zip(directions, directions_shifted_left) ) first_changed_i = directions_diffs.index(_DIRECTION_CHANGED) directions_diffs = ( directions_diffs[first_changed_i + 1 :] + directions_diffs[: first_changed_i + 1] ) assert directions_diffs[-1] == _DIRECTION_CHANGED directions_diffs = directions_diffs[:-1] stability_chains = directions_diffs.split(_DIRECTION_CHANGED) chains_lengths = [ (len(stability_chain) + 1) for stability_chain in stability_chains ] return chains_lengths def main(): t = int(input()) for i in range(t): n = int(input()) directions = input() result = find_cost_of_reordering(directions) print(result) main()
FUNC_DEF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN LIST FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF 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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
for _ in range(int(input())): n = int(input()) s = input() arr = [] if s[0] == s[1] and s[1] == s[-1]: arr.append(1) else: arr.append(0) for i in range(1, n - 1): if s[i - 1] == s[i] and s[i] == s[i + 1]: arr.append(1) else: arr.append(0) if s[n - 2] == s[n - 1] and s[n - 1] == s[0]: arr.append(1) else: arr.append(0) special = False if arr[0] == 1 and arr[-1] == 1: special = True new = [] sum = 0 for i in arr: if i == 1: sum += 1 elif sum != 0: new.append(sum) sum = 0 if sum != 0: new.append(sum) sum = 0 if special == True and len(new) != 1: new[0] += new.pop() ans = 0 for i in new: if i % 3 == 0: ans += i // 3 else: ans += i // 3 + 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 LIST IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
for t in range(int(input())): n = int(input()) f = list(input()) ans = 0 fir = 0 sem = 1 for i in range(1, n): if f[i] == f[i - 1]: sem += 1 elif fir == 0: fir = sem sem = 1 else: ans += sem // 3 sem = 1 if sem == n and n % 3 != 0: ans += 1 if f[0] == f[-1]: ans += (sem + fir) // 3 else: ans += sem // 3 ans += fir // 3 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
t = int(input()) for i in range(t): n = int(input()) a = input() ans = 0 f = False for j in range(n): if a[j - 1] != a[j]: f = True k = j if f: k = (k + 1) % n c = 1 for j in range(k, n): if a[j] == a[j - 1]: c += 1 else: ans += c // 3 c = 1 for j in range(k): if a[j] == a[j - 1]: c += 1 else: ans += c // 3 c = 1 else: ans += (n + 2) // 3 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
import sys input = sys.stdin.buffer.readline inin = lambda: int(input()) inar = lambda: list(map(int, input().split())) inst = lambda: input().decode().rstrip("\n\r") INF = float("inf") for _t_ in range(inin()): n = inin() s = list(inst()) if len(set(s)) == 1: print((n - 1) // 3 + 1) continue else: i = 0 while s[i] == s[-1]: s.append(s[i]) i += 1 s = s[i:] ans = 0 cnt = 0 prev = " " for i in s: if i == prev: cnt += 1 else: ans += cnt // 3 cnt = 1 prev = i ans += cnt // 3 print(ans)
IMPORT ASSIGN 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 FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
from sys import gettrace, stdin if gettrace(): inputi = input else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def solve(): n = int(input()) ss = list(input()) res = 0 if all(s == ss[0] for s in ss): print((n + 2) // 3) return st = 0 while ss[st] == ss[-1]: st += 1 cur = ss[-1] count = 0 for s in ss[st:] + ss[:st]: if s != cur: res += count // 3 count = 0 cur = s count += 1 res += count // 3 print(res) def main(): t = int(input()) for _ in range(t): solve() main()
IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
import sys input = sys.stdin.readline inp, ip = lambda: int(input()), lambda: [int(w) for w in input().split()] for _ in range(inp()): n = inp() s = input().strip() prev = -1 x = [] for i in range(n): if s[i] != prev: x.append(0) x[-1] += 1 prev = s[i] ans = 0 if len(x) == 1: ans += (x[0] - 1) // 3 + 1 else: if s[0] == s[-1] and len(x) > 1: t = x.pop() x[0] += t for i in x: ans += (i - 3) // 3 + 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
t = int(input()) for i in range(t): n = int(input()) l = input() p = 0 b = False for i in range(1, n): if l[i] != l[i - 1]: p = i b = True break if b: l = l[p:] + l[:p] + "." t = 0 r = 0 for i in range(1, n + 1): r += 1 if l[i] != l[i - 1]: t += r // 3 r = 0 print(t) else: print((n - 1) // 3 + 1)
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
def main(): (n,) = map(int, input().split(" ")) s = list(input()) ss = set(s) if len(ss) == 1: return (len(s) - 1) // 3 + 1 while s[0] == s[-1]: s = s[1:] + s[:1] z = 0 pred = s[0] count = 1 for i in range(1, n): if s[i] == pred: count += 1 else: z += count // 3 count = 1 pred = s[i] z += count // 3 return z for _ in range(int(input())): print(main())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
from sys import stdin, stdout _input, _print = stdin.readline, stdout.write _int, _range, _str, _enumerate = int, range, str, enumerate def solution(): for _ in _range(_int(_input())): n = _int(_input()) arr = _input().rstrip("\n") if arr[-1] == arr[0]: k = 0 while arr[0] == arr[-(k + 1)] and k + 1 < n: k += 1 arr = arr[-k:] + arr[:-k] current = 0 ans = 0 for i, v in _enumerate(arr): if v != arr[current]: ans += (i - current) // 3 current = i else: ans += (n - current) // 3 if current == 0 and (n - current) % 3 > 0: ans += 1 print(ans) solution()
ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF 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 STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
from sys import stdin, stdout def shift(c_a): c = c_a[0] for i in range(n - 1): c_a[i] = c_a[i + 1] c_a[n - 1] = c def omkar_and_bed_wars(n, s): c_a = list(s) r1 = cal_dp(n, c_a) shift(c_a) r2 = cal_dp(n, c_a) shift(c_a) r3 = cal_dp(n, c_a) shift(c_a) r4 = cal_dp(n, c_a) return min(r1, r2, r3, r4) def cal_dp(n, c_a): dp = [10**9] * n for i in range(1, n): rl = get_cnt(i - 1, c_a, "RL") if i > 1: rl += dp[i - 2] rrl = 10**9 rll = 10**9 if i > 1: rrl = get_cnt(i - 2, c_a, "RRL") rll = get_cnt(i - 2, c_a, "RLL") if i > 2: rrl += dp[i - 3] rll += dp[i - 3] rrll = 10**9 if i > 2: rrll = get_cnt(i - 3, c_a, "RRLL") if i > 3: rrll += dp[i - 4] dp[i] = min(rl, rrl, rll, rrll) return dp[n - 1] def get_cnt(idx, c_a, s): cnt = 0 for i in range(len(s)): if c_a[idx + i] != s[i]: cnt += 1 return cnt t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) s = stdin.readline().strip() stdout.write(str(omkar_and_bed_wars(n, s)) + "\n")
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR 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 FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
for _ in range(int(input())): n = int(input()) s = list(input()) ans = 0 m = 1 if s.count("R") == n or s.count("L") == n: ans = (n + 2) // 3 else: while s[-1] == s[0]: s.append(s.pop(0)) s.append("A") for i in range(len(s) - 1): if s[i] == s[i + 1]: m += 1 else: ans += m // 3 m = 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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
for _ in range(int(input())): n = int(input()) s = input() island = [(0) for i in range(n + 1)] act = 0 island[0] = 1 for i in range(1, n): if s[i] != s[i - 1]: act += 1 island[act] += 1 if act == 0: if n % 3 == 0: print(n // 3) else: print(n // 3 + 1) else: if act % 2 == 0: island[0] += island[act] act -= 1 ans = 0 for i in range(act + 1): ans += island[i] // 3 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 NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
for _ in range(int(input())): n = int(input()) s = input() l = [] i = 0 while i < n: ct = 0 while i < n and s[i] == "L": ct += 1 i += 1 if ct > 0: l.append(ct) ct = 0 while i < n and s[i] == "R": ct += 1 i += 1 if ct > 0: l.append(ct) if s[0] == s[-1]: l[0] += l[-1] l.pop() if len(l) < 2: print((n + 2) // 3) else: ct = 0 for i in l: ct += i // 3 print(ct)
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 LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
from sys import exit, stderr, stdin def rl(): return [int(w) for w in stdin.readline().split()] (T,) = rl() for _ in range(T): (n,) = rl() s = stdin.readline().rstrip() l = 1 while l < n and s[l] == s[0]: l += 1 if l == n: print((n + 2) // 3) continue r = n while s[r - 1] == s[0]: r -= 1 y = (n - r + l) // 3 k = l for i in range(l, r): if s[i] != s[k]: y += (i - k) // 3 k = i y += (r - k) // 3 print(y)
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
t = int(input()) def diff(s, t): return sum(1 for a, b in zip(s, t) if a != b) for _ in range(t): n = int(input()) s = input() best = n + 5 for _ in range(4): cc = [n + 2] * (n + 1) cc[0] = 0 for i in range(2, n + 1): cc[i] = min(cc[i], cc[i - 2] + diff("RL", s[i - 2 : i])) if i >= 3: cc[i] = min(cc[i], cc[i - 3] + diff("RRL", s[i - 3 : i])) cc[i] = min(cc[i], cc[i - 3] + diff("RLL", s[i - 3 : i])) if i >= 4: cc[i] = min(cc[i], cc[i - 4] + diff("RRLL", s[i - 4 : i])) best = min(cc[n], best) s = s[1:] + s[0] print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
from sys import stdin input = stdin.readline for _ in range(int(input())): n = int(input()) s = input()[:-1] x = 0 c = 0 ans = 0 if s[0] == "L": s = s.strip("L") if not s: print((n + 2) // 3) continue else: s = s.strip("R") if not s: print((n + 2) // 3) continue ans += (n - len(s)) // 3 for i in s: if c: if i == "L": x += 1 else: ans += x // 3 c ^= 1 x = 1 elif i == "R": x += 1 else: ans += x // 3 c ^= 1 x = 1 ans += x // 3 print(ans)
ASSIGN 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR IF VAR STRING VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
def main(): n = int(input()) s = input() ans = 0 if s == "L" * n: s = s[:-1] + "R" ans += 1 if s == "R" * n: s = s[:-1] + "L" ans += 1 i = s.find("LR") if i != -1: s = s[i + 1 :] + s[: i + 1] i = 0 while 0 <= i < n: j = s.find("L", i) k = s.find("R", j) if k == -1: k = n count1 = j - i count2 = k - j ans += count1 // 3 + count2 // 3 i = k print(ans) t = int(input()) for _ in range(t): main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
for _ in range(int(input())): n = int(input()) s = input() if len(set(s)) == 1: print((n + 2) // 3) continue cur = 1 arr = [] for i in range(n): if s[i] == s[(i + 1) % n]: cur += 1 else: arr.append(cur) cur = 1 if s[-1] == s[0]: arr[0] += cur - 1 res = 0 for e in arr: res += e // 3 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] for _ in range(II()): n = II() s = SI() cR = s.count("R") if cR == n or cR == 0: if n < 3: ans = 0 elif n == 3: ans = 1 else: ans = (n + 2) // 3 print(ans) continue s *= 2 si = -1 for i in range(1, n): if s[i - 1] != s[i]: si = i break cnt = 1 ans = 0 for i in range(si, si + n): if s[i] != s[i + 1]: ans += cnt // 3 cnt = 1 else: cnt += 1 print(ans)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
t = int(input()) for _ in range(t): n = int(input()) s = input() pos = -1 for i in range(1, n): if s[i] != s[i - 1]: pos = i if pos == -1: print(int((n + 2) / 3)) else: if s[0] == s[n - 1]: s = s[pos:] + s[:pos] cnt, res = 1, 0 for i in range(1, n): if s[i] == s[i - 1]: cnt += 1 else: res += int(cnt / 3) cnt = 1 res += int(cnt / 3) print(res)
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 NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
a = int(input()) for i in range(a): s = int(input()) t = input() z = [] for j in range(len(t)): z.append(t[j]) ans = [] count = 1 for i in range(1, len(z)): if z[i] == z[i - 1]: count += 1 else: ans.append(count) count = 1 ans.append(count) if len(ans) == 1 and ans[0] < 3: print(0) continue if len(ans) == 1: print((ans[0] + 2) // 3) continue if z[-1] == z[0]: ans[0] += ans[-1] ans.pop() total = 0 for i in range(len(ans)): total += ans[i] // 3 print(total) continue else: total = 0 for i in range(len(ans)): total += ans[i] // 3 print(total) continue
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 LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
test = int(input()) for _ in range(test): n = int(input()) arr = list(input()) ans = 0 i = 0 freq = [] while i < n: f = 1 i += 1 while i < n and arr[i] == arr[i - 1]: f += 1 i += 1 freq.append(f) if len(freq) == 1: print((n - 1) // 3 + 1) else: if len(freq) % 2: s = freq.pop() freq[0] += s res = 0 for i in range(len(freq)): v = freq[i] // 3 res += v print(res)
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
def find_cost_of_reordering(directions): assert len(directions) >= 3 directions_n = len(directions) get_direction = lambda index: directions[index % directions_n] if len(set(directions)) == 1: return 1 + (directions_n - 1) // 3 i = 0 while get_direction(i) == get_direction(i - 1): i += 1 directions = directions[i:] + directions[:i] cost = 0 chain_length = 1 for i in range(1, directions_n): if get_direction(i) == get_direction(i - 1): chain_length += 1 else: cost += chain_length // 3 chain_length = 1 cost += chain_length // 3 return cost def main(): t = int(input()) for i in range(t): n = int(input()) directions = input() result = find_cost_of_reordering(directions) print(result) main()
FUNC_DEF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF 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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
import sys readline = sys.stdin.readline T = int(readline()) Ans = [None] * T INF = 10**9 + 7 for qu in range(T): N = int(readline()) A = [(1 if s == "R" else 0) for s in readline().strip()] ans = INF for _ in range(4): dp = [0, INF, INF, INF, INF] for a in A: dp2 = [INF] * 5 if a == 0: dp2[0] = min(dp[1], dp[2], dp[3], dp[4]) dp2[1] = dp[0] + 1 dp2[2] = dp[1] dp2[3] = dp[1] + 1 dp2[4] = dp[3] else: dp2[0] = min(dp[1], dp[2], dp[3], dp[4]) + 1 dp2[1] = dp[0] dp2[2] = dp[1] + 1 dp2[3] = dp[1] dp2[4] = dp[3] + 1 dp = dp2[:] ans = min(ans, dp[0]) A = [A[-1]] + A[:-1] Ans[qu] = ans print("\n".join(map(str, Ans)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
for _ in range(int(input())): n = int(input()) a = list(str(input())) ans = 0 flag = 0 c = 1 for i in range(1, n): if a[i] != a[i - 1]: flag = 1 break if i == n - 1 and flag == 0: if a[0] == "R": a[0] = "L" else: a[0] = "R" ans = 1 else: a = a[i:n] + a[:i] for i in range(1, n): if a[i] == a[i - 1]: c += 1 else: ans += c // 3 c = 1 print(ans + c // 3)
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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
t = int(input()) for i in range(t): n = int(input()) s = input() c = 0 a = s[0] if s[0] != s[n - 1]: d = 1 for i in s[1:]: if i == a: d = d + 1 else: c = c + d // 3 d = 1 a = i c = c + d // 3 else: b = 0 e = d = 1 for i in s[1:]: if i == a: if b == 0: e = e + 1 else: d = d + 1 else: if b == 0: b = 1 c = c + d // 3 d = 1 a = i if b == 0: d = d + 1 e = e + d c = c + e // 3 print(c)
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 NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
for _ in range(int(input())): n = int(input()) a = list(input()) l = 0 r = 0 t = 0 j = 0 if a[0] == "L" and a[-1] == "L" and "R" in a: j = a.index("R") elif a[0] == "R" and a[-1] == "R" and "L" in a: j = a.index("L") for i in range(j, n): if a[i] == "L": t += r // 3 l += 1 r = 0 else: t += l // 3 r += 1 l = 0 if l == n or r == n: if n % 3 == 0: t += n // 3 else: t += n // 3 + 1 elif l > 0: t += (j + l) // 3 else: t += (j + r) // 3 print(t)
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING STRING VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR NUMBER STRING STRING VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
t = int(input()) def ceildiv(a, b): return (a + b - 1) // b for case in range(t): n = int(input()) s = input() if all(x == s[0] for x in s): print(ceildiv(n, 3)) else: if s[0] == s[-1]: q = s.index("L" if s[0] == "R" else "R") s = s[q:] + s[:q] c = 0 out = 0 prev = None for x in s: if x == prev: c += 1 else: out += c // 3 c = 1 prev = x if c: out += c // 3 print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING STRING STRING ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
t = int(input()) for i in range(t): n = int(input()) s = list(input()) ans = 0 if len(set(s)) != 1: ind = 0 for i in range(1, n): if s[i] != s[i - 1]: ind = i break S = s[ind:] + s[:ind] A = [1] for i in range(1, n): if S[i] != S[i - 1]: A.append(1) else: A[-1] += 1 for i in range(len(A)): ans += A[i] // 3 else: ans = 1 n -= 1 ans += n // 3 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 NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
for nt in range(int(input())): n = int(input()) s = input() if "L" not in s or "R" not in s: if n <= 2: print(0) else: print((n + 2) // 3) continue g = [] curr = s[0] count = 1 for i in range(1, n): if s[i] == curr: count += 1 else: curr = s[i] g.append(count) count = 1 g.append(count) if s[0] != s[-1]: ans = 0 for i in g: ans += i // 3 print(ans) else: g[0] += g[-1] g[-1] = 0 ans = 0 for i in g: ans += i // 3 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 IF STRING VAR STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
t = int(input()) for _ in range(t): n = int(input()) s = list(input()) if s[-1] == s[0]: i = n - 1 while i > 0 and s[i] == s[0]: i -= 1 if i != 0: s = s[i + 1 :] + s[: i + 1] ans = 0 for i in range(n): if s[i] == "R": if not (s[(i + 1) % n] == "L" or s[(i + 2) % n] == "L"): ans += 1 if s[(i + 3) % n] == "R": s[(i + 2) % n] = "L" elif not (s[(i - 1) % n] == "R" or s[(i - 2) % n] == "R"): ans += 1 s[i] = "R" 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 IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING IF VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
from sys import stdin input = stdin.readline t = int(input()) for _ in range(t): n = int(input()) s = input().strip() if s == "R" * n or s == "L" * n: print((n + 2) // 3) else: bckt = [] end = n - 1 ct = 0 while end >= 0 and s[end] == s[0]: ct += 1 end -= 1 start = 0 while start < n and s[start] == s[-1]: ct += 1 start += 1 bckt.append(ct) while start <= end: j = start + 1 while j <= end and s[j] == s[start]: j += 1 bckt.append(j - start) start = j ans = 0 for i in bckt: ans += i // 3 print(ans)
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 FUNC_CALL VAR IF VAR BIN_OP STRING VAR VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player Β β€” i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows. The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) Β β€” the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. -----Example----- Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Output 0 1 1 3 2 -----Note----- In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$. In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.
for s in [*open(0)][2::2]: f = [1] for b, a in zip(s, s[1:-1]): if a == b: f[-1] += 1 else: f.append(1) if s[0] == s[-2] and len(f) > 1: f[0] += f.pop() f[0] += 2 * (len(f) < 2) print(sum(i // 3 for i in f))
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin. We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. For both Adil and Bera the process looks as follows: 1. Choose to stop or to continue to collect bottles. 2. If the choice was to continue then choose some bottle and walk towards it. 3. Pick this bottle and walk to the recycling bin. 4. Go to step 1. Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles. They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin. Input First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≀ ax, ay, bx, by, tx, ty ≀ 109) β€” initial positions of Adil, Bera and recycling bin respectively. The second line contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of bottles on the ground. Then follow n lines, each of them contains two integers xi and yi (0 ≀ xi, yi ≀ 109) β€” position of the i-th bottle. It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct. Output Print one real number β€” the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if <image>. Examples Input 3 1 1 2 0 0 3 1 1 2 1 2 3 Output 11.084259940083 Input 5 0 4 2 2 0 5 5 2 3 0 5 5 3 5 3 3 Output 33.121375178000 Note Consider the first sample. Adil will use the following path: <image>. Bera will use the following path: <image>. Adil's path will be <image> units long, while Bera's path will be <image> units long.
read = lambda: map(int, input().split()) ax, ay, bx, by, tx, ty = read() n = int(input()) p = [tuple(read()) for i in range(n)] a, b = [], [] for i in range(n): x, y = p[i] pt = ((tx - x) ** 2 + (ty - y) ** 2) ** 0.5 pa = ((ax - x) ** 2 + (ay - y) ** 2) ** 0.5 pb = ((bx - x) ** 2 + (by - y) ** 2) ** 0.5 a.append((pa - pt, i)) b.append((pb - pt, i)) a.sort() b.sort() if a[0][1] == b[0][1] and n > 1: ans = min(a[0][0], b[0][0], a[0][0] + b[1][0], a[1][0] + b[0][0]) else: ans = min(a[0][0], b[0][0]) if a[0][1] != b[0][1]: ans = min(ans, a[0][0] + b[0][0]) ans += sum(2 * ((tx - x) ** 2 + (ty - y) ** 2) ** 0.5 for x, y in p) print(ans)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin. We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. For both Adil and Bera the process looks as follows: 1. Choose to stop or to continue to collect bottles. 2. If the choice was to continue then choose some bottle and walk towards it. 3. Pick this bottle and walk to the recycling bin. 4. Go to step 1. Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles. They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin. Input First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≀ ax, ay, bx, by, tx, ty ≀ 109) β€” initial positions of Adil, Bera and recycling bin respectively. The second line contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of bottles on the ground. Then follow n lines, each of them contains two integers xi and yi (0 ≀ xi, yi ≀ 109) β€” position of the i-th bottle. It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct. Output Print one real number β€” the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if <image>. Examples Input 3 1 1 2 0 0 3 1 1 2 1 2 3 Output 11.084259940083 Input 5 0 4 2 2 0 5 5 2 3 0 5 5 3 5 3 3 Output 33.121375178000 Note Consider the first sample. Adil will use the following path: <image>. Bera will use the following path: <image>. Adil's path will be <image> units long, while Bera's path will be <image> units long.
ax, ay, bx, by, tx, ty = map(int, input().split()) n = int(input()) l = [tuple(map(int, input().split())) for _ in range(n)] a, b = [], [] for i in range(n): x, y = l[i] lt = ((tx - x) * (tx - x) + (ty - y) * (ty - y)) ** 0.5 la = ((ax - x) * (ax - x) + (ay - y) * (ay - y)) ** 0.5 lb = ((bx - x) * (bx - x) + (by - y) * (by - y)) ** 0.5 a += [(la - lt, i)] b += [(lb - lt, i)] a.sort() b.sort() if a[0][1] == b[0][1] and n > 1: ans = min(a[0][0], b[0][0], a[0][0] + b[1][0], a[1][0] + b[0][0]) elif a[0][1] == b[0][1]: ans = min(a[0][0], b[0][0]) else: ans = min(a[0][0], b[0][0], a[0][0] + b[0][0]) for i in range(n): x, y = l[i] lt = ((tx - x) * (tx - x) + (ty - y) * (ty - y)) ** 0.5 ans += lt + lt print(ans)
ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN 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 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 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 VAR LIST BIN_OP VAR VAR VAR VAR LIST BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN 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 VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin. We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. For both Adil and Bera the process looks as follows: 1. Choose to stop or to continue to collect bottles. 2. If the choice was to continue then choose some bottle and walk towards it. 3. Pick this bottle and walk to the recycling bin. 4. Go to step 1. Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles. They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin. Input First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≀ ax, ay, bx, by, tx, ty ≀ 109) β€” initial positions of Adil, Bera and recycling bin respectively. The second line contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of bottles on the ground. Then follow n lines, each of them contains two integers xi and yi (0 ≀ xi, yi ≀ 109) β€” position of the i-th bottle. It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct. Output Print one real number β€” the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if <image>. Examples Input 3 1 1 2 0 0 3 1 1 2 1 2 3 Output 11.084259940083 Input 5 0 4 2 2 0 5 5 2 3 0 5 5 3 5 3 3 Output 33.121375178000 Note Consider the first sample. Adil will use the following path: <image>. Bera will use the following path: <image>. Adil's path will be <image> units long, while Bera's path will be <image> units long.
ax, ay, bx, by, tx, ty = map(int, input().split()) n = int(input()) a, b = [], [] res = 0 for i in range(n): x, y = map(int, input().split()) lt = ((tx - x) * (tx - x) + (ty - y) * (ty - y)) ** 0.5 la = ((ax - x) * (ax - x) + (ay - y) * (ay - y)) ** 0.5 lb = ((bx - x) * (bx - x) + (by - y) * (by - y)) ** 0.5 a += [(la - lt, i)] b += [(lb - lt, i)] res += lt a.sort() b.sort() res *= 2 if a[0][1] == b[0][1] and n > 1: res += min(a[0][0], b[0][0], a[0][0] + b[1][0], a[1][0] + b[0][0]) elif a[0][1] == b[0][1]: res += min(a[0][0], b[0][0]) else: res += min(a[0][0], b[0][0], a[0][0] + b[0][0]) print(res)
ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 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 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 VAR LIST BIN_OP VAR VAR VAR VAR LIST BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin. We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. For both Adil and Bera the process looks as follows: 1. Choose to stop or to continue to collect bottles. 2. If the choice was to continue then choose some bottle and walk towards it. 3. Pick this bottle and walk to the recycling bin. 4. Go to step 1. Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles. They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin. Input First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≀ ax, ay, bx, by, tx, ty ≀ 109) β€” initial positions of Adil, Bera and recycling bin respectively. The second line contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of bottles on the ground. Then follow n lines, each of them contains two integers xi and yi (0 ≀ xi, yi ≀ 109) β€” position of the i-th bottle. It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct. Output Print one real number β€” the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if <image>. Examples Input 3 1 1 2 0 0 3 1 1 2 1 2 3 Output 11.084259940083 Input 5 0 4 2 2 0 5 5 2 3 0 5 5 3 5 3 3 Output 33.121375178000 Note Consider the first sample. Adil will use the following path: <image>. Bera will use the following path: <image>. Adil's path will be <image> units long, while Bera's path will be <image> units long.
import sys input = sys.stdin.readline ax, ay, bx, by, tx, ty = map(int, input().strip().split()) def dd(x0, y0, x1, y1): return ((x0 - x1) ** 2 + (y0 - y1) ** 2) ** 0.5 n = int(input().strip()) arr = [list(map(int, input().strip().split())) for i in range(n)] dist = [dd(x, y, tx, ty) for x, y in arr] ans = sum(dist) * 2 a_dists = sorted((dd(*x, ax, ay) - dd(*x, tx, ty), i) for i, x in enumerate(arr)) b_dists = sorted((dd(*x, bx, by) - dd(*x, tx, ty), i) for i, x in enumerate(arr)) a_dists = a_dists[:2] b_dists = b_dists[:2] mi = 10**100 for dx, x in a_dists: for dy, y in b_dists: if x != y: mi = min(mi, ans + dx + dy) mi = min(mi, ans + min(a_dists[0][0], b_dists[0][0])) print(mi)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER 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 VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin. We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. For both Adil and Bera the process looks as follows: 1. Choose to stop or to continue to collect bottles. 2. If the choice was to continue then choose some bottle and walk towards it. 3. Pick this bottle and walk to the recycling bin. 4. Go to step 1. Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles. They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin. Input First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≀ ax, ay, bx, by, tx, ty ≀ 109) β€” initial positions of Adil, Bera and recycling bin respectively. The second line contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of bottles on the ground. Then follow n lines, each of them contains two integers xi and yi (0 ≀ xi, yi ≀ 109) β€” position of the i-th bottle. It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct. Output Print one real number β€” the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if <image>. Examples Input 3 1 1 2 0 0 3 1 1 2 1 2 3 Output 11.084259940083 Input 5 0 4 2 2 0 5 5 2 3 0 5 5 3 5 3 3 Output 33.121375178000 Note Consider the first sample. Adil will use the following path: <image>. Bera will use the following path: <image>. Adil's path will be <image> units long, while Bera's path will be <image> units long.
def glen(x1, y1, x2, y2): return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 xa, ya, xb, yb, xt, yt = map(int, input().split()) n = int(input()) x = [0] * n y = [0] * n for i in range(n): x[i], y[i] = map(int, input().split()) inf = 10**10 afmin = [0, 0] asmin = [0, 0] for i in range(n): a = glen(xa, ya, x[i], y[i]) - glen(xa, ya, xt, yt) - glen(xt, yt, x[i], y[i]) if asmin[0] > a: asmin[0] = a asmin[1] = i if asmin[0] < afmin[0]: asmin, afmin = afmin, asmin bfmin = [0, 0] bsmin = [0, 0] for i in range(n): b = glen(xb, yb, x[i], y[i]) - glen(xb, yb, xt, yt) - glen(xt, yt, x[i], y[i]) if bsmin[0] > b: bsmin[0] = b bsmin[1] = i if bsmin[0] < bfmin[0]: bsmin, bfmin = bfmin, bsmin res = 0 b = bfmin[1] a = afmin[1] b1 = bsmin[1] a1 = asmin[1] res1 = min( glen(x[a], y[a], xa, ya) - glen(x[a], y[a], xt, yt), glen(x[b], y[b], xb, yb) - glen(x[b], y[b], xt, yt), ) if b != a: res += glen(x[a], y[a], xa, ya) - glen(x[a], y[a], xt, yt) res += glen(x[b], y[b], xb, yb) - glen(x[b], y[b], xt, yt) else: res += min( glen(x[a], y[a], xa, ya) - glen(x[a], y[a], xt, yt) + glen(x[b1], y[b1], xb, yb) - glen(x[b1], y[b1], xt, yt), glen(x[a1], y[a1], xa, ya) - glen(x[a], y[a], xt, yt) + glen(x[b], y[b], xb, yb) - glen(x[b1], y[b1], xt, yt), ) for i in range(n): res += 2 * glen(xt, yt, x[i], y[i]) res1 += 2 * glen(xt, yt, x[i], y[i]) if n == 1: res = 10**11 print(min(res, res1))
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin. We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. For both Adil and Bera the process looks as follows: 1. Choose to stop or to continue to collect bottles. 2. If the choice was to continue then choose some bottle and walk towards it. 3. Pick this bottle and walk to the recycling bin. 4. Go to step 1. Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles. They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin. Input First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≀ ax, ay, bx, by, tx, ty ≀ 109) β€” initial positions of Adil, Bera and recycling bin respectively. The second line contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of bottles on the ground. Then follow n lines, each of them contains two integers xi and yi (0 ≀ xi, yi ≀ 109) β€” position of the i-th bottle. It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct. Output Print one real number β€” the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if <image>. Examples Input 3 1 1 2 0 0 3 1 1 2 1 2 3 Output 11.084259940083 Input 5 0 4 2 2 0 5 5 2 3 0 5 5 3 5 3 3 Output 33.121375178000 Note Consider the first sample. Adil will use the following path: <image>. Bera will use the following path: <image>. Adil's path will be <image> units long, while Bera's path will be <image> units long.
def D(x1, y1, x2, y2): return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 Ax, Ay, Bx, By, Rx, Ry = [float(_) for _ in input().split()] N = int(input()) PosX, PosY = [0.0] * N, [0.0] * N for i in range(N): PosX[i], PosY[i] = map(int, input().split()) CutA, CutB = [0.0] * N, [0.0] * N ans = 0 for i in range(N): DA = D(PosX[i], PosY[i], Ax, Ay) DB = D(PosX[i], PosY[i], Bx, By) DR = D(PosX[i], PosY[i], Rx, Ry) CutA[i] = [i, DR - DA] CutB[i] = [i, DR - DB] ans += 2 * D(PosX[i], PosY[i], Rx, Ry) CutA.sort(key=lambda x: x[1], reverse=True) CutB.sort(key=lambda x: x[1], reverse=True) CutCase = [] CutCase.append(CutA[0][1]) CutCase.append(CutB[0][1]) if N >= 2: if CutA[0][0] != CutB[0][0]: CutCase.append(CutA[0][1] + CutB[0][1]) else: CutCase.append(CutA[0][1] + CutB[1][1]) CutCase.append(CutA[1][1] + CutB[0][1]) ans -= max(CutCase) print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR BIN_OP VAR VAR ASSIGN VAR VAR LIST VAR BIN_OP VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin. We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. For both Adil and Bera the process looks as follows: 1. Choose to stop or to continue to collect bottles. 2. If the choice was to continue then choose some bottle and walk towards it. 3. Pick this bottle and walk to the recycling bin. 4. Go to step 1. Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles. They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin. Input First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≀ ax, ay, bx, by, tx, ty ≀ 109) β€” initial positions of Adil, Bera and recycling bin respectively. The second line contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of bottles on the ground. Then follow n lines, each of them contains two integers xi and yi (0 ≀ xi, yi ≀ 109) β€” position of the i-th bottle. It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct. Output Print one real number β€” the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if <image>. Examples Input 3 1 1 2 0 0 3 1 1 2 1 2 3 Output 11.084259940083 Input 5 0 4 2 2 0 5 5 2 3 0 5 5 3 5 3 3 Output 33.121375178000 Note Consider the first sample. Adil will use the following path: <image>. Bera will use the following path: <image>. Adil's path will be <image> units long, while Bera's path will be <image> units long.
def readlist(): return list(map(float, input().split())) def dist(P1, P2): aa = P1[0] - P2[0] bb = P1[1] - P2[1] return (aa * aa + bb * bb) ** 0.5 ax, ay, bx, by, tx, ty = readlist() A = [ax, ay] B = [bx, by] T = [tx, ty] n = int(input()) Bottles = [] for i in range(n): Bottles.append(readlist()) dists_to_T = [dist(T, btl) for btl in Bottles] sum_dists = 2 * sum(dists_to_T) A_possible_wins = [(dist(btl, T) - dist(A, btl)) for btl in Bottles] B_possible_wins = [(dist(btl, T) - dist(B, btl)) for btl in Bottles] a_best = max(A_possible_wins) b_best = max(B_possible_wins) if a_best < 0 or b_best < 0 or len(Bottles) == 1: print(sum_dists - max(a_best, b_best)) else: a_pos = A_possible_wins.index(a_best) b_pos = B_possible_wins.index(b_best) if a_pos != b_pos: print(sum_dists - a_best - b_best) else: a_2best = max(A_possible_wins[:a_pos] + A_possible_wins[a_pos + 1 :]) b_2best = max(B_possible_wins[:b_pos] + B_possible_wins[b_pos + 1 :]) print( min( sum_dists - a_best - max(0, b_2best), sum_dists - max(0, a_2best) - b_best, ) )
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR VAR
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin. We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. For both Adil and Bera the process looks as follows: 1. Choose to stop or to continue to collect bottles. 2. If the choice was to continue then choose some bottle and walk towards it. 3. Pick this bottle and walk to the recycling bin. 4. Go to step 1. Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles. They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin. Input First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≀ ax, ay, bx, by, tx, ty ≀ 109) β€” initial positions of Adil, Bera and recycling bin respectively. The second line contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of bottles on the ground. Then follow n lines, each of them contains two integers xi and yi (0 ≀ xi, yi ≀ 109) β€” position of the i-th bottle. It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct. Output Print one real number β€” the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if <image>. Examples Input 3 1 1 2 0 0 3 1 1 2 1 2 3 Output 11.084259940083 Input 5 0 4 2 2 0 5 5 2 3 0 5 5 3 5 3 3 Output 33.121375178000 Note Consider the first sample. Adil will use the following path: <image>. Bera will use the following path: <image>. Adil's path will be <image> units long, while Bera's path will be <image> units long.
read = lambda: map(int, input().split()) def dis(x1, y1, x2, y2): return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 ax, ay, bx, by, tx, ty = read() n = int(input()) a, b = [], [] sum = 0 for i in range(n): x, y = read() dist = dis(tx, ty, x, y) a.append((dis(ax, ay, x, y) - dist, i)) b.append((dis(bx, by, x, y) - dist, i)) sum += dist * 2 a.sort() b.sort() if n > 1 and a[0][1] == b[0][1]: ans = min(a[0][0], b[0][0], a[0][0] + b[1][0], a[1][0] + b[0][0]) else: ans = min(a[0][0], b[0][0]) if n > 1: ans = min(a[0][0] + b[0][0], ans) print(ans + sum)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin. We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. For both Adil and Bera the process looks as follows: 1. Choose to stop or to continue to collect bottles. 2. If the choice was to continue then choose some bottle and walk towards it. 3. Pick this bottle and walk to the recycling bin. 4. Go to step 1. Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles. They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin. Input First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≀ ax, ay, bx, by, tx, ty ≀ 109) β€” initial positions of Adil, Bera and recycling bin respectively. The second line contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of bottles on the ground. Then follow n lines, each of them contains two integers xi and yi (0 ≀ xi, yi ≀ 109) β€” position of the i-th bottle. It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct. Output Print one real number β€” the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if <image>. Examples Input 3 1 1 2 0 0 3 1 1 2 1 2 3 Output 11.084259940083 Input 5 0 4 2 2 0 5 5 2 3 0 5 5 3 5 3 3 Output 33.121375178000 Note Consider the first sample. Adil will use the following path: <image>. Bera will use the following path: <image>. Adil's path will be <image> units long, while Bera's path will be <image> units long.
def distance(x1, y1, x2, y2): return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 def second_smallest(arr, temp_x, temp_y, tx, ty): smallest_index = arr.index( max(arr, key=lambda T: distance(tx, ty, *T) - distance(ax, ay, *T)) ) d = 10**20 index = -1 for i, v in enumerate(arr): x, y = v dist = -distance(x, y, tx, ty) + distance(x, y, temp_x, temp_y) if dist < d and smallest_index != i: d = dist index = i return index ax, ay, bx, by, tx, ty = map(int, input().split()) n = int(input()) arr = list() for i in range(n): x, y = map(int, input().split()) arr.append((x, y)) if n == 1: print( min(distance(ax, ay, *arr[0]), distance(bx, by, *arr[0])) + distance(tx, ty, *arr[0]) ) exit() distances = [ (distance(ax, ay, x, y), distance(bx, by, x, y), distance(tx, ty, x, y)) for x, y in arr ] ans = 2 * sum(v[2] for v in distances) index1 = distances.index(max(distances, key=lambda T: T[2] - T[0])) index2 = distances.index(max(distances, key=lambda T: T[2] - T[1])) if index1 != index2: if distance(tx, ty, *arr[index1]) > distance(ax, ay, *arr[index1]): ans -= distance(tx, ty, *arr[index1]) ans += distance(ax, ay, *arr[index1]) if distance(tx, ty, *arr[index2]) > distance(bx, by, *arr[index2]): ans -= distance(tx, ty, *arr[index2]) ans += distance(bx, by, *arr[index2]) if distance(tx, ty, *arr[index1]) < distance(ax, ay, *arr[index1]) and distance( tx, ty, *arr[index2] ) < distance(bx, by, *arr[index2]): p, q = distance(tx, ty, *arr[index1]) - distance( ax, ay, *arr[index1] ), distance(tx, ty, *arr[index2]) - distance(bx, by, *arr[index2]) ans -= max(p, q) print(ans) else: p, q = ans, ans p -= distance(tx, ty, *arr[index1]) p += distance(ax, ay, *arr[index1]) temp_index = second_smallest(arr, bx, by, tx, ty) if distance(bx, by, *arr[temp_index]) < distance(tx, ty, *arr[temp_index]): p -= distance(tx, ty, *arr[temp_index]) p += distance(bx, by, *arr[temp_index]) q -= distance(tx, ty, *arr[index2]) q += distance(bx, by, *arr[index2]) temp_index = second_smallest(arr, ax, ay, tx, ty) if distance(ax, ay, *arr[temp_index]) < distance(tx, ty, *arr[temp_index]): q -= distance(tx, ty, *arr[temp_index]) q += distance(ax, ay, *arr[temp_index]) print(min(p, q))
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin. We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. For both Adil and Bera the process looks as follows: 1. Choose to stop or to continue to collect bottles. 2. If the choice was to continue then choose some bottle and walk towards it. 3. Pick this bottle and walk to the recycling bin. 4. Go to step 1. Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles. They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin. Input First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≀ ax, ay, bx, by, tx, ty ≀ 109) β€” initial positions of Adil, Bera and recycling bin respectively. The second line contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of bottles on the ground. Then follow n lines, each of them contains two integers xi and yi (0 ≀ xi, yi ≀ 109) β€” position of the i-th bottle. It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct. Output Print one real number β€” the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if <image>. Examples Input 3 1 1 2 0 0 3 1 1 2 1 2 3 Output 11.084259940083 Input 5 0 4 2 2 0 5 5 2 3 0 5 5 3 5 3 3 Output 33.121375178000 Note Consider the first sample. Adil will use the following path: <image>. Bera will use the following path: <image>. Adil's path will be <image> units long, while Bera's path will be <image> units long.
ax, ay, bx, by, tx, ty = map(int, input().split()) n = int(input()) X = [0] * n Y = [0] * n T = [0.0] * n res = 0.0 for i in range(n): X[i], Y[i] = map(int, input().split()) T[i] = ((X[i] - tx) ** 2 + (Y[i] - ty) ** 2) ** 0.5 res += T[i] * 2 L1 = [[0]] * (n + 2) L2 = [[0]] * (n + 2) for i in range(n + 2): L1[i] = [0.0] * 2 L2[i] = [0.0] * 2 L1[n][1] = n L2[n][1] = n L1[n + 1][1] = -1 L2[n + 1][1] = -2 L1[n + 1][0] = ((tx - ax) ** 2 + (ty - ay) ** 2) ** 0.5 L2[n + 1][0] = ((tx - bx) ** 2 + (ty - by) ** 2) ** 0.5 for i in range(n): L1[i][0] = ((X[i] - ax) ** 2 + (Y[i] - ay) ** 2) ** 0.5 - T[i] L2[i][0] = ((X[i] - bx) ** 2 + (Y[i] - by) ** 2) ** 0.5 - T[i] L1[i][1] = L2[i][1] = i L1.sort() L2.sort() d = 10**18 for i in range(min(n + 2, 10)): for j in range(min(n + 2, 10)): if L1[i][1] != L2[j][1]: d = min(d, res + L1[i][0] + L2[j][0]) print(d)
ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Two integer sequences existed initially, one of them was strictly increasing, and another one β€” strictly decreasing. Strictly increasing sequence is a sequence of integers $[x_1 < x_2 < \dots < x_k]$. And strictly decreasing sequence is a sequence of integers $[y_1 > y_2 > \dots > y_l]$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences $[1, 3, 4]$ and $[10, 4, 2]$ can produce the following resulting sequences: $[10, \textbf{1}, \textbf{3}, 4, 2, \textbf{4}]$, $[\textbf{1}, \textbf{3}, \textbf{4}, 10, 4, 2]$. The following sequence cannot be the result of these insertions: $[\textbf{1}, 10, \textbf{4}, 4, \textbf{3}, 2]$ because the order of elements in the increasing sequence was changed. Let the obtained sequence be $a$. This sequence $a$ is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β€” strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO". -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO" in the first line. Otherwise print "YES" in the first line. In the second line, print a sequence of $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ should be either $0$ or $1$ for each $i$ from $1$ to $n$. The $i$-th element of this sequence should be $0$ if the $i$-th element of $a$ belongs to the increasing sequence, and $1$ otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. -----Examples----- Input 9 5 1 3 6 8 2 9 0 10 Output YES 1 0 0 0 0 1 0 1 0 Input 5 1 2 4 0 2 Output NO
n, a = int(input()), [int(i) for i in input().split()] + [0] ans, inc, dec = [(0) for _ in range(n)], -1, float("inf") for i in range(n): if inc < a[i] < dec: if a[i] < a[i + 1]: inc = a[i] else: dec = a[i] ans[i] = 1 elif inc < a[i]: inc = a[i] elif dec > a[i]: dec = a[i] ans[i] = 1 else: print("NO") break else: print("YES") print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Two integer sequences existed initially, one of them was strictly increasing, and another one β€” strictly decreasing. Strictly increasing sequence is a sequence of integers $[x_1 < x_2 < \dots < x_k]$. And strictly decreasing sequence is a sequence of integers $[y_1 > y_2 > \dots > y_l]$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences $[1, 3, 4]$ and $[10, 4, 2]$ can produce the following resulting sequences: $[10, \textbf{1}, \textbf{3}, 4, 2, \textbf{4}]$, $[\textbf{1}, \textbf{3}, \textbf{4}, 10, 4, 2]$. The following sequence cannot be the result of these insertions: $[\textbf{1}, 10, \textbf{4}, 4, \textbf{3}, 2]$ because the order of elements in the increasing sequence was changed. Let the obtained sequence be $a$. This sequence $a$ is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β€” strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO". -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO" in the first line. Otherwise print "YES" in the first line. In the second line, print a sequence of $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ should be either $0$ or $1$ for each $i$ from $1$ to $n$. The $i$-th element of this sequence should be $0$ if the $i$-th element of $a$ belongs to the increasing sequence, and $1$ otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. -----Examples----- Input 9 5 1 3 6 8 2 9 0 10 Output YES 1 0 0 0 0 1 0 1 0 Input 5 1 2 4 0 2 Output NO
n = int(input()) a = list(map(int, input().split())) + [0] answer = [0] * n current_increase_max = -1 current_decrease_min = 1000000.0 for i in range(n): if current_increase_max < a[i] and a[i] < current_decrease_min: if a[i] < a[i + 1]: current_increase_max = a[i] answer[i] = 0 else: current_decrease_min = a[i] answer[i] = 1 elif current_increase_max < a[i]: current_increase_max = a[i] answer[i] = 0 elif current_decrease_min > a[i]: current_decrease_min = a[i] answer[i] = 1 else: print("NO") return print("YES") print(*answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Two integer sequences existed initially, one of them was strictly increasing, and another one β€” strictly decreasing. Strictly increasing sequence is a sequence of integers $[x_1 < x_2 < \dots < x_k]$. And strictly decreasing sequence is a sequence of integers $[y_1 > y_2 > \dots > y_l]$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences $[1, 3, 4]$ and $[10, 4, 2]$ can produce the following resulting sequences: $[10, \textbf{1}, \textbf{3}, 4, 2, \textbf{4}]$, $[\textbf{1}, \textbf{3}, \textbf{4}, 10, 4, 2]$. The following sequence cannot be the result of these insertions: $[\textbf{1}, 10, \textbf{4}, 4, \textbf{3}, 2]$ because the order of elements in the increasing sequence was changed. Let the obtained sequence be $a$. This sequence $a$ is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β€” strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO". -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO" in the first line. Otherwise print "YES" in the first line. In the second line, print a sequence of $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ should be either $0$ or $1$ for each $i$ from $1$ to $n$. The $i$-th element of this sequence should be $0$ if the $i$-th element of $a$ belongs to the increasing sequence, and $1$ otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. -----Examples----- Input 9 5 1 3 6 8 2 9 0 10 Output YES 1 0 0 0 0 1 0 1 0 Input 5 1 2 4 0 2 Output NO
def main(): n = int(input()) a = list(map(int, input().split())) result = [0] * n last_increasing = -1 last_decreasing = 200001 for i, x in enumerate(a): if i == n - 1: if x < last_decreasing: result[i] = 1 elif x <= last_increasing: print("NO") return else: if last_increasing >= x >= last_decreasing: print("NO") return if x > last_increasing and x >= last_decreasing: last_increasing = x if x <= last_increasing and x < last_decreasing: last_decreasing = x result[i] = 1 if last_increasing < x < last_decreasing: if a[i + 1] > x: last_increasing = x else: last_decreasing = x result[i] = 1 print("YES") print(" ".join(map(str, result))) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Two integer sequences existed initially, one of them was strictly increasing, and another one β€” strictly decreasing. Strictly increasing sequence is a sequence of integers $[x_1 < x_2 < \dots < x_k]$. And strictly decreasing sequence is a sequence of integers $[y_1 > y_2 > \dots > y_l]$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences $[1, 3, 4]$ and $[10, 4, 2]$ can produce the following resulting sequences: $[10, \textbf{1}, \textbf{3}, 4, 2, \textbf{4}]$, $[\textbf{1}, \textbf{3}, \textbf{4}, 10, 4, 2]$. The following sequence cannot be the result of these insertions: $[\textbf{1}, 10, \textbf{4}, 4, \textbf{3}, 2]$ because the order of elements in the increasing sequence was changed. Let the obtained sequence be $a$. This sequence $a$ is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β€” strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO". -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO" in the first line. Otherwise print "YES" in the first line. In the second line, print a sequence of $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ should be either $0$ or $1$ for each $i$ from $1$ to $n$. The $i$-th element of this sequence should be $0$ if the $i$-th element of $a$ belongs to the increasing sequence, and $1$ otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. -----Examples----- Input 9 5 1 3 6 8 2 9 0 10 Output YES 1 0 0 0 0 1 0 1 0 Input 5 1 2 4 0 2 Output NO
MAX = 1 + 2 * 10**5 MIN = -1 ASC = 0 DSC = 1 n = int(input()) a = list(map(int, input().split())) asc = MIN dsc = MAX res = [] for i in range(n): if a[i] > asc and a[i] < dsc: if i + 1 == n: res.append(DSC) elif a[i] > a[i + 1]: res.append(DSC) dsc = a[i] else: res.append(ASC) asc = a[i] elif a[i] > asc: res.append(ASC) asc = a[i] elif a[i] < dsc: res.append(DSC) dsc = a[i] else: print("NO") return print("YES") print(*res)
ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER 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 ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Two integer sequences existed initially, one of them was strictly increasing, and another one β€” strictly decreasing. Strictly increasing sequence is a sequence of integers $[x_1 < x_2 < \dots < x_k]$. And strictly decreasing sequence is a sequence of integers $[y_1 > y_2 > \dots > y_l]$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences $[1, 3, 4]$ and $[10, 4, 2]$ can produce the following resulting sequences: $[10, \textbf{1}, \textbf{3}, 4, 2, \textbf{4}]$, $[\textbf{1}, \textbf{3}, \textbf{4}, 10, 4, 2]$. The following sequence cannot be the result of these insertions: $[\textbf{1}, 10, \textbf{4}, 4, \textbf{3}, 2]$ because the order of elements in the increasing sequence was changed. Let the obtained sequence be $a$. This sequence $a$ is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β€” strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO". -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- If there is a contradiction in the input and it is impossible to split the given sequence $a$ into one increasing sequence and one decreasing sequence, print "NO" in the first line. Otherwise print "YES" in the first line. In the second line, print a sequence of $n$ integers $res_1, res_2, \dots, res_n$, where $res_i$ should be either $0$ or $1$ for each $i$ from $1$ to $n$. The $i$-th element of this sequence should be $0$ if the $i$-th element of $a$ belongs to the increasing sequence, and $1$ otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. -----Examples----- Input 9 5 1 3 6 8 2 9 0 10 Output YES 1 0 0 0 0 1 0 1 0 Input 5 1 2 4 0 2 Output NO
n = int(input()) a = list(map(int, input().split())) inf = 10**6 inc = [inf for i in range(0, n + 1)] dec = [(-inf) for i in range(0, n + 1)] trinc = [(-1) for i in range(0, n + 1)] trdec = [(-1) for i in range(0, n + 1)] inc[0] = -inf dec[0] = inf for i in range(0, n - 1): if a[i + 1] < a[i]: if inc[i + 1] > inc[i]: inc[i + 1] = inc[i] trinc[i + 1] = 1 if a[i + 1] > a[i]: if dec[i + 1] < dec[i]: dec[i + 1] = dec[i] trdec[i + 1] = 1 if a[i + 1] > inc[i]: if dec[i + 1] < a[i]: dec[i + 1] = a[i] trdec[i + 1] = 0 if a[i + 1] < dec[i]: if inc[i + 1] > a[i]: inc[i + 1] = a[i] trinc[i + 1] = 0 if inc[n - 1] == inf and dec[n - 1] == -inf: print("NO") return now_inc = False ans = [(0) for i in range(0, n)] if inc[n - 1] != inf: now_inc = True for i in range(n - 1, -1, -1): if now_inc: ans[i] = 1 if trinc[i] == 0: now_inc = False else: ans[i] = 0 if trdec[i] == 0: now_inc = True print("YES") print(" ".join(str(x) for x in ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
t = int(input()) for x in range(t): n, d = map(int, input().split()) l = list(map(int, input().split())) p1 = 0 p2 = 1 sums = 0 p = l[:] l.sort() i = n - 1 flag = 0 i = n - 1 j = i - 1 iprev = -20 while j >= 0 and len(l) > 1: if abs(l[i] - l[j]) < d: sums += l[i] + l[j] l.pop(-1) l.pop(-1) i = len(l) - 1 j = i - 1 else: l.pop(-1) i = len(l) - 1 j = i - 1 print(sums)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
t = int(input().strip()) for i in range(t): sum = 0 n, d = map(int, input().strip().split()) a = map(int, input().strip().split()) a = sorted(a, reverse=True) present = [1] * n for i in range(len(a)): if present[i]: for j in range(i + 1, len(a)): if present[j] and a[i] - a[j] >= d: break if present[j] and a[i] - a[j] < d: sum += a[i] + a[j] present[j] = 0 present[i] = 0 break print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
for t in range(int(input())): n, d = map(int, input().split()) a = list(map(int, input().split())) a = sorted(a, reverse=True) ans = 0 i = 0 while i < n - 1: if a[i] - a[i + 1] < d: ans += a[i] + a[i + 1] i += 2 else: i += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
t = int(input()) for _ in range(t): n, d = map(int, input().split()) l = list(map(int, input().split())) if n == 1: print(0) continue l.sort(reverse=True) ans = 0 i = 0 j = 1 while j < n: if l[i] - l[j] < d: ans += l[i] + l[j] j += 2 i += 2 else: i += 1 j += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
T = int(input()) for j in range(0, T): N, D = input().split() N = int(N) D = int(D) array = input().split() array = [int(x) for x in array] array.sort() sum = 0 for k in range(len(array) - 1, 0, -1): if array[k] - array[k - 1] < D: sum += array[k] + array[k - 1] array[k - 1] += 2 * D print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
for _ in range(int(input())): n, d = map(int, input().split()) nli = list(map(int, input().split())) nli.sort(reverse=True) lst = list() ind = 0 while ind < n - 1: if nli[ind] - nli[ind + 1] < d: lst.append(nli[ind + 1]) lst.append(nli[ind]) ind += 2 else: ind += 1 print(sum(lst))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
def sumpair(n, d, arr): arr.sort() i = n - 1 s = 0 while i > 0: if arr[i] - arr[i - 1] < d: s += arr[i - 1] + arr[i] i -= 2 else: i -= 1 return s t = int(input()) for _ in range(t): n, d = map(int, input().split()) arr = list(map(int, input().split())) print(sumpair(n, d, arr))
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
for i in range(int(input())): x, y = map(int, input().split()) a = list(map(int, input().split())) a.sort() k = 0 j = len(a) - 1 while j > 0: if a[j] - a[j - 1] < y: k = k + a[j] + a[j - 1] j = j - 2 else: j = j - 1 print(k)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
for t in range(int(input())): temp = list(map(int, input().split())) n = temp[0] d = temp[1] a = list(map(int, input().split())) a.sort(reverse=True) ans = 0 if n == 1: print(0) continue p1 = 0 p2 = 1 ans = 0 while p1 < n and p2 < n: if a[p1] - a[p2] < d: ans += a[p1] + a[p2] p1 = p2 + 1 p2 = p1 + 1 continue else: p1 = p2 + 1 if p1 > p2: tt = p1 p1 = p2 p2 = tt print(ans)
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 NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
for T in range(int(input())): N, D = map(int, input().split()) arr = list(map(int, input().split())) arr = sorted(arr) mark = [0] * N ans = 0 for i in range(N - 1, 0, -1): if mark[i] == 0: if arr[i] - arr[i - 1] < D: ans += arr[i] + arr[i - 1] mark[i], mark[i - 1] = 1, 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR 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 NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
t = int(input()) for T in range(t): n, d = [int(x) for x in input().split()] a = [int(x) for x in input().split()] a.sort(reverse=True) ind, ind1 = 0, 1 ans = [] while ind < n - 1: if a[ind] - a[ind + 1] < d: ans.append(a[ind]) ans.append(a[ind + 1]) ind += 2 continue ind += 1 print(sum(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
t = int(input()) for i in range(t): n, d = map(int, input().split()) l = list(map(int, input().split())) l.sort() s1, s2 = 0, 0 j = len(l) - 1 while j > 0: if l[j] - l[j - 1] < d: s1 += l[j] + l[j - 1] j -= 2 else: j = j - 1 j = 0 while j < len(l) - 1: if l[j + 1] - l[j] < d: s2 += l[j + 1] + l[j] j += 2 else: j += 1 print(max(s1, s2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
a = int(input()) for i in range(0, a): b, c = map(int, input().split()) d = list(map(int, input().split())) d.sort(reverse=True) u = 0 z = [] for t in range(0, b): z.append(0) for t in range(0, b): if z[t] == 0: z[t] = 1 for y in range(t + 1, b): r = abs(d[t] - d[y]) if r < c and z[y] == 0: u = u + (d[t] + d[y]) z[y] = 1 break elif r >= c: break print(u)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
n = int(input()) list2 = [] for i in range(n): n, d = map(int, input().split()) b = list(map(int, input().split())) ans = 0 b.sort() j = len(b) - 1 while j > 0: if b[j] - b[j - 1] < d: ans += b[j] + b[j - 1] j -= 2 else: j -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
for _ in range(int(input())): N, D = list(map(int, input().split())) L = list(map(int, input().split())) L.sort(reverse=True) sum_ = 0 flag = 0 for i in range(N - 1): if flag == 1: flag = 0 elif L[i] - L[i + 1] < D: sum_ += L[i] + L[i + 1] flag = 1 print(sum_)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
for t in range(int(input())): n, d = map(int, input().split()) a = list(map(int, input().split())) a.sort() a.reverse() i = 1 ans = 0 while 1: if i > n - 1: break elif a[i - 1] - a[i] < d: ans = ans + a[i - 1] + a[i] i = i + 2 else: i = i + 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
tests = int(input()) for t in range(tests): n, d = [int(i) for i in input().split()] a = [int(i) for i in input().split()] a.sort(reverse=True) i = 0 while i < len(a) - 1: if a[i] - a[i + 1] < d: i += 2 else: a.pop(i) if i == len(a) - 1: a.pop() print(sum(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
def integer_list(): return list(map(int, input().split())) def string_list(): return list(map(str, input().split())) def hetro_list(): return list(input().split()) t = int(input()) for _ in range(t): n, d = integer_list() lst = integer_list() lst.sort() s = 0 while lst: ele = lst.pop() if lst: if ele - lst[-1] < d: s += ele + lst.pop() print(s)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
for _ in range(int(input())): n, d = map(int, input().split()) a = list(map(int, input().split())) a.sort() k = a.pop() ans = 0 while a: k1 = a.pop() if k - k1 < d: ans += k + k1 if a: k = a.pop() else: k = k1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
def solve(): ans = 0 i = input().split(" ") n = int(i[0]) d = int(i[1]) a = list(map(int, input().split(" "))) a.sort() while n > 1: if a[-1] - a[-2] < d: ans += a[-1] + a[-2] del a[-1] del a[-1] n -= 2 else: del a[-1] n -= 1 print(ans) return def main(): t = int(input()) while t > 0: solve() t -= 1 return main()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
t = int(input()) while t: t -= 1 n, d = [int(i) for i in input().split()] a = [int(i) for i in input().split()] a.sort() a.reverse() i = 1 ans = 0 while i < n: if a[i - 1] - a[i] < d: ans += a[i] + a[i - 1] i += 2 else: i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
t = int(input()) for i in range(t): l = input().split() n = int(l[0]) d = int(l[1]) l = list(map(int, input().split())) l = sorted(l) sum = 0 j = len(l) - 1 while j >= 1: if l[j] - l[j - 1] < d: sum += l[j] + l[j - 1] j -= 2 else: j -= 1 print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
t = int(input()) while t: ans = 0 n, d = map(int, input().split()) arr = list(map(int, input().split())) arr = sorted(arr) l = len(arr) i = l - 1 while i > 0: if arr[i] - arr[i - 1] < d: ans += arr[i] + arr[i - 1] i = i - 2 else: i = i - 1 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
def io(func=str, n=1): def nfp(): ff = str try: it = iter(func) def nf(val): nonlocal ff ff = next(it, ff) return ff(val) return type(func)(nf(va) for va in input().split()) except TypeError: return func(input()) if type(n) is list: return [_(func, x) for x in n] else: t = lambda: nfp() if n == 1: return t() elif n == 0: return [t() for i in range(int(input()))] else: return [t() for i in range(n)] def main(): T = io(int) for i in range(T): N, D = io((int, int)) data = sorted(io([int]))[::-1] prec = None s = 0 for elt in data: if prec is not None: if prec - elt < D: s += elt + prec prec = None else: prec = elt else: prec = elt yield s print("\n".join([str(res) for res in main()]))
FUNC_DEF VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR VAR IF VAR NONE IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR EXPR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
t = int(input()) for j in range(0, t): n, d = input().strip().split() n = int(n) d = int(d) a = list(map(int, input().strip().split())) a.sort(reverse=True) add = 0 i = 0 while i < n - 1: if a[i] - a[i + 1] < d: add = add + a[i] + a[i + 1] i = i + 2 else: i = i + 1 print(add)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
test = int(input()) while test != 0: test = test - 1 n, d = map(int, input().split()) x = list(input().split()) i = 0 while i < n: x[i] = int(x[i]) i = i + 1 x.sort() ans = 0 while n > 0: a = x[n - 1] b = x[n - 2] b = abs(a - b) if b < d: ans = ans + x[n - 1] + x[n - 2] n = n - 2 else: n = n - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
t = int(input()) for _ in range(t): n, p = map(int, input().split()) flag = 0 sum = 0 l = list(map(int, input().split())) l.sort() for i in range(n - 1, 0, -1): if flag == 1: flag = 0 continue elif abs(l[i] - l[i - 1]) < p: sum = sum + l[i] + l[i - 1] flag = 1 print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian as well. Given an array of N numbers, a pair of numbers is called good if difference between the two numbers is strictly less than D. Find out maximum possible sum of all good disjoint pairs that can be made from these numbers. Sum of X pairs is the sum of all 2*X numbers in the pairs. ------ Input ------ First line contains T, the number of test cases to follow. First line of each test case contains 2 space separated integers: N and D. Second line of each test case contains N space separated integers. ------ Output ------ For each test case, output the answer in a separate line. ------ Constraints ------ $1 ≀ T, N, D, Array Elements ≀ 10^{5}$ $1 ≀ Sum of N over all test cases ≀ 5*10^{5}$ ------ Note: ------ Pair (a,b) is disjoint with pair (c,d) if and only if indices of a, b, c and d in the array are distinct. ----- Sample Input 1 ------ 3 3 3 3 5 8 4 3 5 8 10 12 5 3 3 2 8 17 15 ----- Sample Output 1 ------ 8 22 37 ----- explanation 1 ------ Test Case 1: You can only take 1 pair out of 3 numbers. So pair(3,5) is only valid pair whose difference is 2. Test Case 3: You can take pairs(3,2) and (15,17) as the answer.
def sort(low, high, L): if low < high: mid = low + int((high - low) / 2) a = low b = mid + 1 sort(low, mid, L) sort(mid + 1, high, L) temp = [] while a <= mid or b <= high: if a <= mid and b <= high: if L[a] > L[b]: temp.append(L[a]) a += 1 else: temp.append(L[b]) b += 1 elif a <= mid and b > high: temp.append(L[a]) a += 1 elif a > mid and b <= high: temp.append(L[b]) b += 1 for i in range(len(temp)): L[low + i] = temp[i] def solution(L, D): k = {} sum = 0 sort(0, len(L) - 1, L) i = 0 while i < len(L) - 1: if L[i] - L[i + 1] < D: sum += L[i] + L[i + 1] i += 1 i += 1 return sum T = int(input()) for t in range(T): N, D = map(int, input().split()) L = list(map(int, input().split())) print(solution(L, D))
FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
def gcd(a, b): while a != b and a != 0 and b != 0: if a > b: a = a % b else: b = b % a if a == 0: return b if b == 0: return a return a n = int(input()) a = list(map(int, input().split())) b = sorted(a) d = b[0] for i in range(1, n): d = gcd(d, b[i]) if d == 1: break if d != 1: print("YES") print(0) exit() odd = 0 ans = 0 for i in range(0, n): if a[i] % 2 == 0: if odd > 0: ans += odd // 2 + (2 if odd % 2 == 1 else 0) odd = 0 else: odd += 1 if odd > 0: ans += odd // 2 + (2 if odd % 2 == 1 else 0) print("YES") print(ans)
FUNC_DEF WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
def gcd(a, b): if b == 0: return a return gcd(b, a % b) n = int(input()) a = [int(i) for i in input().split()] g = 0 count = 0 result = 0 for p in a: g = gcd(g, p) if p & 1: count += 1 else: result += count // 2 + 2 * (count & 1) count = 0 result += count // 2 + 2 * (count & 1) print("YES") if g == 1: print(result) else: print(0)
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
import sys input = sys.stdin.readline n = int(input()) bits = [] g = 0 def gcd(a, b): while b: a, b = b, a % b return a for i in map(int, input().split()): bits.append(i % 2) g = gcd(i, g) if g > 1: print("YES") print("0") else: cnt = 0 for i in range(n - 1): if bits[i]: if bits[i + 1]: bits[i] = 0 bits[i + 1] = 0 cnt += 1 else: bits[i] = 0 cnt += 2 if bits[n - 1]: cnt += 2 print("YES") print(cnt)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
def GCD(a, b): return a if b == 0 else GCD(b, a % b) n = int(input()) a = list(map(int, input().split())) a.append(0) g = 0 odds = 0 count = 0 for i in range(n + 1): g = GCD(a[i], g) if a[i] % 2 == 1: odds += 1 else: count += odds // 2 + 2 * (odds % 2) odds = 0 print("YES") print(0 if g > 1 else count)
FUNC_DEF RETURN VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
n = int(input()) k = 0 l = list(map(int, input().split())) a = l[0] c = 0 for i in range(1, n): b = l[i] while a % b != 0: ost = a % b a = b b = ost if b == 1: c = 1 break else: a = b if c == 0: print("YES") print(0) exit() k = 0 for i in range(n - 1): if l[i] % 2 != 0: if l[i + 1] % 2 != 0: k = k + 1 l[i + 1] = 0 else: k = k + 2 if l[n - 1] % 2 != 0: k = k + 2 print("YES") print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
from sys import exit def gcd(x, y): if y == 0: return x else: return gcd(y, x % y) print("YES") n = int(input()) s = [] mn = float("inf") for i in input().split(): i = int(i) if i < mn: mn = i s.append(i) g = s[0] for i in s[1:]: g = gcd(g, i) if g == 1: break else: print(0) exit() count = 0 last = s[0] % 2 for i in s[1:]: if i % 2: if last: count += 1 last = 0 else: last = 1 elif last: count += 2 last = 0 if last: count += 2 print(count)
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
from sys import stdin, stdout def gcd(a, b): if a == 0: return b elif b == 0: return a elif a == b: return a else: while b: a, b = b, a % b return a def seqGCD(a): currentGCD = gcd(a[0], a[1]) i = 2 n = len(a) while currentGCD != 1 and i < n: currentGCD = gcd(currentGCD, a[i]) i += 1 return currentGCD n = int(stdin.readline().rstrip()) a = stdin.readline().rstrip().split() a = [int(b) for b in a] if seqGCD(a) > 1: print("YES") print(0) else: moves = 0 i = 0 while i + 1 < n: if a[i] % 2 == 0: i += 1 elif a[i + 1] % 2 == 1: moves += 1 i += 2 else: moves += 2 i += 2 if i == n - 1 and a[i] % 2 == 1: moves += 2 print("YES") print(moves)
FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR IF VAR VAR RETURN VAR WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
import sys def gcd(a, b): while a != 0 and b != 0: if a > b: a = a % b else: b = b % a return max(a, b) def listGcd(l): g = gcd(l[0], l[1]) for i in l[2:]: if g == 1: return g g = gcd(g, i) return g n = int(sys.stdin.readline()) A = [int(i) for i in sys.stdin.readline().split()] if listGcd(A) != 1: print("YES") print(0) else: count = 0 canPair = False for i in range(n): if canPair and A[i] % 2 == 1: count += 1 canPair = False elif canPair and A[i] % 2 == 0: count += 2 canPair = False elif canPair == False and A[i] % 2 == 1: if i == n - 1: count += 2 canPair = True print("YES") print(count)
IMPORT FUNC_DEF WHILE VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
n = int(input()) al = list(map(int, input().split())) def pgcd(a, b): while b: a, b = b, a % b return a def pgcdl(l): if len(l) == 0: return -1 if len(l) == 1: return l[0] p = pgcd(l[0], l[1]) for i in l[2:]: if p == 1: break p = pgcd(p, i) return p ret = 0 if pgcdl(al) == 1: for i in range(n): if al[i] % 2 == 1: if i == n - 1: ret += 2 elif al[i + 1] % 2 == 1: ret += 1 al[i + 1] = 0 else: ret += 2 al[i + 1] = 0 print("YES") print(ret)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
s, i, n = 0, 0, int(input()) t = list(map(int, input().split())) + [0] while i < n: if t[i] % 2: i += 1 s += 2 - t[i] % 2 i += 1 b = t[0] for a in t: while a: a, b = b % a, a if b == 1: break print("YES") if b > 1: print(0) else: print(s)
ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR WHILE VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
def gcd(a, b): while b != 0: if a < b: a, b = b, a a, b = b, a % b return a def main(): n = int(input()) a = list(map(int, input().split())) b = a[:] a.sort() for i in range(n - 1): if gcd(a[i], a[i + 1]) == 1: break else: print("YES\n0") return result = 0 a = [(0 if item % 2 == 0 else 1) for item in b] for i in range(n - 1): if a[i] == 1 and a[i + 1] == 1: a[i], a[i + 1] = 0, 0 result += 1 for i in range(n - 1): if a[i] == 1: result += 2 if a[-1] == 1: result += 2 print("YES\n%d" % result) main()
FUNC_DEF WHILE VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF 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 EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
n = int(input()) a = input().split() for i in range(n): a[i] = int(a[i]) k = 0 g = 0 def gcd(c, d): if c % d == 0: return d else: return gcd(d, c % d) while True: for i in range(n): g = gcd(g, a[i]) if g > 1: k = 0 break else: for i in range(n - 1): if a[i] % 2 != 0 and a[i + 1] % 2 != 0: k += 1 a[i] = a[i + 1] = 2 else: pass for i in range(n): if a[i] % 2 != 0: k += 2 a[i] = 2 break print("YES") print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR WHILE NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
def gcd(a, b): if a < b: return gcd(b, a) if a % b == 0: return b return gcd(b, a % b) n = int(input()) a = list(map(int, input().split())) k = gcd(a[0], a[1]) if n > 2: for i in a[2:]: k = gcd(k, i) print("YES") if k > 1: print(0) else: s, t = 0, 0 for i in a: if i % 2 == 1: s += 1 else: if s % 2 == 0: t += s // 2 else: t += s // 2 + 2 s = 0 if s % 2 == 0: t += s // 2 else: t += s // 2 + 2 print(t)
FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP 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 NUMBER VAR NUMBER IF VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
def gcd(a, b): while b: a, b = b, a % b return a n = int(input()) a = list(map(int, input().split())) ans = 0 l = 0 g = a[0] for i in range(n): g = gcd(a[i], g) if g > 1: print("YES\n0") else: for i in range(n): if a[i] % 2: l += 1 else: if l % 2: ans += l // 2 + 2 else: ans += l // 2 l = 0 if l % 2: ans += l // 2 + 2 else: ans += l // 2 print("YES") print(ans)
FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
def gcd(a, b): while b > 0: a, b = b, a % b return a def gcd_list(l): n = len(l) if n == 1: return l[0] return gcd(gcd_list(l[0 : n // 2]), gcd_list(l[n // 2 :])) n = int(input().strip()) a = [int(x) for x in input().strip().split()] print("YES") if gcd_list(a) > 1: print(0) else: a.append(0) ops = 0 odds = 0 for x in a: if x % 2 == 1: odds += 1 elif odds > 0: ops += odds // 2 + (2 if odds % 2 == 1 else 0) odds = 0 print(ops)
FUNC_DEF WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
def gcd(a, b): if not a or not b: return max(a, b) if a % b == 0: return b return gcd(b, a % b) n = int(input()) a = list(map(int, input().split())) now = a[0] for i in range(1, n): now = gcd(now, a[i]) if now != 1: print("YES\n0") else: res = 0 i = 0 while i < n: if a[i] & 1: if i < n - 1 and a[i + 1] & 1: res += 1 i += 2 else: res += 2 i += 1 else: i += 1 print("YES\n" + str(res))
FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP 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 VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
def gcd(a, b): if a == 0: return b if b == 0: return a return gcd(b, a % b) def gcd_of_list(a): a = a[::-1] partial_gcd = 0 while a: partial_gcd = gcd(partial_gcd, a.pop()) return partial_gcd def get_odd_chunk_lengths(a): a = a[::-1] chunk_lengths_arr = [] curr_chunk_length = 0 while a: next_num = a.pop() if next_num % 2 == 1: curr_chunk_length += 1 if next_num % 2 == 0 and curr_chunk_length != 0: chunk_lengths_arr.append(curr_chunk_length) curr_chunk_length = 0 if curr_chunk_length != 0: chunk_lengths_arr.append(curr_chunk_length) return chunk_lengths_arr def num_ops_to_evenify_odd_chunk(chunk_length): if chunk_length % 2 == 0: return chunk_length // 2 else: return chunk_length // 2 + 2 input() a = [int(i) for i in input().split()] if gcd_of_list(a) > 1: print("YES") print(0) else: print("YES") print( sum( [ num_ops_to_evenify_odd_chunk(chunk_length) for chunk_length in get_odd_chunk_lengths(a) ] ) )
FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. <image>. Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≀ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. <image> is the biggest non-negative number d such that d divides bi for every i (1 ≀ i ≀ n). Input The first line contains a single integer n (2 ≀ n ≀ 100 000) β€” length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. Output Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful. Examples Input 2 1 1 Output YES 1 Input 3 6 2 4 Output YES 0 Input 2 1 3 Output YES 1 Note In the first example you can simply make one move to obtain sequence [0, 2] with <image>. In the second example the gcd of the sequence is already greater than 1.
from sys import stdin def gcd(a, b): if b: return gcd(b, a % b) else: return a n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) g = 0 for el in a: g = gcd(g, el) res = 0 if g == 1: for i in range(n - 1): if a[i] % 2 and a[i + 1] % 2: a[i], a[i + 1] = a[i] - a[i + 1], a[i] + a[i + 1] res += 1 for i in range(n): if a[i] % 2: res += 2 print("YES") print(res)
FUNC_DEF IF VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. <image> Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem. A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: * Empty string is a correct bracket sequence. * if s is a correct bracket sequence, then (s) is also a correct bracket sequence. * if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence. Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s. Joyce doesn't know anything about bracket sequences, so she asked for your help. Input The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000). Output Print the answer to Will's puzzle in the first and only line of output. Examples Input ((?)) Output 4 Input ??()?? Output 7 Note For the first sample testcase, the pretty substrings of s are: 1. "(?" which can be transformed to "()". 2. "?)" which can be transformed to "()". 3. "((?)" which can be transformed to "(())". 4. "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: 1. "??" which can be transformed to "()". 2. "()". 3. "??()" which can be transformed to "()()". 4. "?()?" which can be transformed to "(())". 5. "??" which can be transformed to "()". 6. "()??" which can be transformed to "()()". 7. "??()??" which can be transformed to "()()()".
s = input() l = len(s) ans = 0 for i in range(0, l): m = n = 0 for j in range(i, l): m += s[j] == "(" m -= s[j] == ")" n += s[j] == "?" if m + n < 0: break if m < n: m, n = n, m ans += m == n print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING VAR VAR VAR STRING VAR VAR VAR STRING IF BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. <image> Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem. A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: * Empty string is a correct bracket sequence. * if s is a correct bracket sequence, then (s) is also a correct bracket sequence. * if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence. Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s. Joyce doesn't know anything about bracket sequences, so she asked for your help. Input The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000). Output Print the answer to Will's puzzle in the first and only line of output. Examples Input ((?)) Output 4 Input ??()?? Output 7 Note For the first sample testcase, the pretty substrings of s are: 1. "(?" which can be transformed to "()". 2. "?)" which can be transformed to "()". 3. "((?)" which can be transformed to "(())". 4. "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: 1. "??" which can be transformed to "()". 2. "()". 3. "??()" which can be transformed to "()()". 4. "?()?" which can be transformed to "(())". 5. "??" which can be transformed to "()". 6. "()??" which can be transformed to "()()". 7. "??()??" which can be transformed to "()()()".
s = str(input()) n = len(s) ans = 0 for l in range(n - 1): cnt = 0 qu = 0 chk = 0 if s[l] == "(": cnt += 1 chk += 1 elif s[l] == "?": qu += 1 chk = max(chk - 1, 0) else: continue for r in range(l + 1, n): if s[r] == "(": cnt += 1 chk += 1 elif s[r] == "?": qu += 1 chk = max(chk - 1, 0) else: cnt -= 1 chk = max(chk - 1, 0) if cnt + qu < 0: break elif ( (qu - cnt) % 2 == 0 and qu - cnt >= 0 and (qu - cnt) // 2 <= qu and chk <= 0 ): ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. <image> Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem. A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: * Empty string is a correct bracket sequence. * if s is a correct bracket sequence, then (s) is also a correct bracket sequence. * if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence. Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s. Joyce doesn't know anything about bracket sequences, so she asked for your help. Input The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000). Output Print the answer to Will's puzzle in the first and only line of output. Examples Input ((?)) Output 4 Input ??()?? Output 7 Note For the first sample testcase, the pretty substrings of s are: 1. "(?" which can be transformed to "()". 2. "?)" which can be transformed to "()". 3. "((?)" which can be transformed to "(())". 4. "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: 1. "??" which can be transformed to "()". 2. "()". 3. "??()" which can be transformed to "()()". 4. "?()?" which can be transformed to "(())". 5. "??" which can be transformed to "()". 6. "()??" which can be transformed to "()()". 7. "??()??" which can be transformed to "()()()".
S = input() n = len(S) Ans = 0 for l in range(0, n): bs = 0 det = 0 for r in range(l, n): if S[r] == "?": if bs > 0: det = det + 1 bs = bs - 1 else: bs = bs + 1 elif S[r] == "(": bs = bs + 1 else: if bs == 0 and det > 0: bs = bs + 2 det = det - 1 bs = bs - 1 if bs < 0: break if bs > 0: continue if bs % 2 == 0: Ans = Ans + 1 print(Ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. <image> Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem. A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: * Empty string is a correct bracket sequence. * if s is a correct bracket sequence, then (s) is also a correct bracket sequence. * if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence. Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s. Joyce doesn't know anything about bracket sequences, so she asked for your help. Input The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000). Output Print the answer to Will's puzzle in the first and only line of output. Examples Input ((?)) Output 4 Input ??()?? Output 7 Note For the first sample testcase, the pretty substrings of s are: 1. "(?" which can be transformed to "()". 2. "?)" which can be transformed to "()". 3. "((?)" which can be transformed to "(())". 4. "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: 1. "??" which can be transformed to "()". 2. "()". 3. "??()" which can be transformed to "()()". 4. "?()?" which can be transformed to "(())". 5. "??" which can be transformed to "()". 6. "()??" which can be transformed to "()()". 7. "??()??" which can be transformed to "()()()".
s = input() a = 0 n = len(s) for i in range(n): l = 0 k = 0 for j in range(i, n): l += s[j] == "(" l -= s[j] == ")" k += s[j] == "?" if l + k < 0: break if k > l: l, k = k, l if l == k: a = a + 1 print(a)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING VAR VAR VAR STRING VAR VAR VAR STRING IF BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR