description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = list(map(int, input().split())) for i in range(n): p[i] -= 1 res = [] f = [1] * n for v in p: if f[v] == 1: f[v] = 0 sum = 1 v = p[v] while 1: if f[v] == 0: break sum += 1 f[v] = 0 v = p[v] res.append(sum) res = sorted(res) sum = 0 for v in res: sum += v * v sum += res[len(res) - 1] * res[len(res) - 2] * 2 if len(res) == 1: print(res[0] * res[0]) else: print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) l = list(map(int, input().split())) c = [-1] * n w = -1 mx = -1 tm = -1 ans = 0 for i in range(n): sz = 0 ptr = i if c[ptr] == -1: w += 1 while c[ptr] == -1: c[ptr] = w ptr = l[ptr] - 1 sz += 1 if sz > mx: tm, mx = mx, sz else: tm = max(sz, tm) if sz > 0: ans += sz**2 if tm == -1: print(ans) else: print(ans - tm**2 - mx**2 + (mx + tm) ** 2)
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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) a = list(map(int, input().split())) ans = [] for i, x in enumerate(a): cycle = 0 while a[i] > 0: cycle += 1 tmp = a[i] - 1 a[i] -= n + 1 i = tmp ans.append(cycle) n = max(ans) ans.remove(n) m = max(ans) if ans else 0 if m: ans.remove(m) o = 0 for x in ans: o += x**2 print((n + m) ** 2 + o)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = [0] + list(map(int, input().split())) def visit(v): res = 0 while p[v] != 0: res += 1 x = p[v] p[v], v = 0, x return res val = sorted([visit(v) for v in range(n + 1)]) print(sum(list([(x * x) for x in val])) + 2 * val[-1] * val[-2])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
import sys sys.setrecursionlimit(10000) def dfs(root, t): time[root] = t vis[root] = 1 stack = [root] while len(stack) != 0: element = stack.pop() time[element] = t vis[element] = 1 for i in graph[element]: if vis[i] == 0: stack.append(i) t += 1 else: c.append(t - time[i] + 1) n = int(input()) l = list(map(int, input().split())) graph = [[] for i in range(n)] for i in range(n): graph[i].append(l[i] - 1) vis = [0] * n c = [] time = [0] * n for i in range(n): if vis[i] == 0: dfs(i, 1) c.sort() ans = 0 for i in c: ans += i**2 if len(c) >= 2: print(ans + c[-1] * c[-2] * 2) else: print(ans)
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = [(int(x) - 1) for x in input().split()] encontrados = [False] * n ciclos = [] for i in range(n): if encontrados[i]: continue ciclos.append([i]) encontrados[i] = True j = p[i] while not encontrados[j]: ciclos[-1].append(j) encontrados[j] = True j = p[j] primero = max([len(x) for x in ciclos]) for i in range(n): if len(ciclos[i]) == primero: ciclos.pop(i) break if ciclos: segundo = max([len(x) for x in ciclos]) else: segundo = 0 print((primero + segundo) ** 2 + sum([(len(x) ** 2) for x in ciclos]) - segundo**2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) st = list(map(int, input().split())) if len(st) != n: exit() checked = [] for i in range(n + 5): checked.append(False) cycles = [] def dfs(ver): cur = ver curcycle = 0 while not checked[cur]: checked[cur] = True curcycle += 1 cur = st[cur] - 1 cycles.append(curcycle) for i in range(n): dfs(i) cycles = sorted(cycles, reverse=True) if len(cycles) == 0: print(0) elif len(cycles) == 1: print(cycles[0] ** 2) else: s = (cycles[0] + cycles[1]) ** 2 for i in range(2, len(cycles)): s += cycles[i] ** 2 print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
input() l = [[int(x) - 1, False] for x in input().split()] loop = [] for begin in l: if begin[1]: continue count = 0 nextI = begin[0] while not l[nextI][1]: l[nextI][1] = True nextI = l[nextI][0] count += 1 loop.append(count) s = sorted(loop, reverse=True) total = sum(map(lambda x: x * x, s)) + (2 * s[0] * s[1] if len(s) >= 2 else 0) print(total)
EXPR FUNC_CALL VAR ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) P = [(int(p) - 1) for p in input().split()] seen = [False] * n res = m0 = m1 = 0 for i in range(n): if not seen[i]: seen[i] = True j = P[i] c = 1 while j != i: seen[j] = True j = P[j] c += 1 res += c * c if c > m0: m0, m1 = c, m0 elif c > m1: m1 = c res += (m0 + m1) * (m0 + m1) - m0 * m0 - m1 * m1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
def dfs(u): c = 0 while p[u]: c += 1 p[u], u = 0, p[u] return c a = lambda: list(map(int, input().split())) n = a() p = [0] + a() val = sorted([dfs(u) for u in p]) print(sum(list(map(lambda x: x * x, val))) + 2 * val[-1] * val[-2])
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) edges = [(int(x) - 1) for x in input().split()] visited = [(False) for _ in range(n)] sizes = [] for i in range(n): if visited[i]: continue visited[i] = True component_size = 1 j = edges[i] while not visited[j]: visited[j] = True component_size += 1 j = edges[j] sizes.append(component_size) sizes = sorted(sizes) if len(sizes) > 1: sizes[-2] += sizes[-1] sizes.pop() print(sum(map(lambda x: x**2, sizes)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) destination = list(map(lambda x: int(x) - 1, input().split())) visited = [(False) for __ in range(n)] maxs = [0, 0] convenience = 0 for i in range(n): if not visited[i]: visited[i] = True cycle_len = 1 current_node = i while destination[current_node] != i: current_node = destination[current_node] visited[current_node] = True cycle_len += 1 convenience += cycle_len**2 if cycle_len > min(maxs): maxs = [cycle_len, max(maxs)] convenience += 2 * maxs[0] * maxs[1] print(convenience)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
r = lambda: map(int, input().split()) def main(): (n,) = r() p = [(x - 1) for x in r()] vis = [False] * n lens = [] for i in range(n): if vis[i]: continue vis[i], cc = True, 1 j = p[i] while not vis[j]: vis[j] = True cc += 1 j = p[j] lens.append(cc) lens.sort() if len(lens) > 1: lens[-2] += lens[-1] lens.pop() print(sum(x * x for x in lens)) main()
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
def dfs(i, Colour, P): Stack = [i] size = 0 while Stack: v = Stack[-1] if Colour[v] == 0: Colour[v] = 1 size += 1 if Colour[P[v] - 1] == 0: Stack.append(P[v] - 1) else: Colour[v] = 2 Stack.pop() else: Colour[v] = 2 Stack.pop() return size n = int(input()) P = list(map(int, input().split())) Colour = [0] * n Len = [] for i in range(n): if Colour[i] == 0: Len.append(dfs(i, Colour, P)) c1 = 0 c2 = 0 count = 0 for i in Len: if c1 <= i: c2 = c1 c1 = i elif i > c2: c2 = i for i in Len: count += i * i count += c1 * c2 * 2 print(count)
FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL 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 BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
bz = [(0) for i in range(1000000)] to = [(0) for i in range(1000000)] n = 0 n = int(input()) i = 1 for x in input().split(): to[i] = int(x) i += 1 all_num = 0 mx1 = 0 mx2 = 0 for i in range(1, n + 1): if bz[i] == 0: x = i num = 0 while bz[x] == 0: num += 1 bz[x] = 1 x = to[x] all_num += num**2 if mx1 <= num: mx1, mx2 = num, mx1 elif mx2 < num: mx2 = num print(all_num - mx1**2 - mx2**2 + (mx1 + mx2) ** 2)
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
COLOR_WHITE, COLOR_GREY, COLOR_BLACK = 0, 1, 2 def dfs(targets, colors, node): stack = [node] size = 0 while stack: node = stack[-1] if colors[node] == COLOR_WHITE: colors[node] = COLOR_GREY size += 1 neighbor = targets[node] if colors[neighbor] == COLOR_WHITE: stack.append(neighbor) else: colors[node] = COLOR_BLACK stack.pop() else: colors[node] = COLOR_BLACK stack.pop() return size station_count = int(input()) targets = list(map(lambda node: int(node) - 1, input().split())) colors = [COLOR_WHITE] * station_count component_sizes = [] for node in range(station_count): if colors[node] == COLOR_WHITE: size = dfs(targets, colors, node) component_sizes.append(size) value = sum(size**2 for size in component_sizes) max1, max2 = -1, -1 for size in component_sizes: if max1 < size: max2 = max1 max1 = size elif max2 < size: max2 = size if max2 > -1: value += max1 * max2 * 2 print(value)
ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = list(map(int, input().split())) r = 0 v = [False] * n c = [] for i in range(n): p[i] = p[i] - 1 for i in range(n): r = p[i] if v[r] == False: v[r] = True k = 1 while r != i: r = p[r] v[r] = True k += 1 c.append(k) c.sort c1 = c[len(c) - 1] if len(c) > 1: for i in range(len(c)): if c[i] == max(c): c1 = c[i] break c.pop(i) for i in range(len(c)): if c[i] == max(c): c2 = c[i] break c.pop(i) c.append(c1 + c2) ans = 0 for i in range(len(c)): ans += c[i] ** 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP 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
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
from sys import stdin def main(): n = int(stdin.readline()) ar = list(map(int, stdin.readline().split())) check = [False] * n ans_list = [] for i in range(n): if not check[i]: count = 0 curr = i while not check[curr]: check[curr] = True count += 1 curr = ar[curr] - 1 ans_list.append(count) ans_list.sort(reverse=True) ans = 0 if len(ans_list) >= 2: ans += (ans_list[0] + ans_list[1]) ** 2 for i in range(2, len(ans_list)): ans += ans_list[i] ** 2 else: ans = ans_list[0] ** 2 print(ans) 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 LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
import sys input = sys.stdin.readline class Unionfind: def __init__(self, n): self.par = [-1] * n self.rank = [1] * n def root(self, x): p = x while not self.par[p] < 0: p = self.par[p] while x != p: tmp = x x = self.par[x] self.par[tmp] = p return p def unite(self, x, y): rx, ry = self.root(x), self.root(y) if rx == ry: return False if self.rank[rx] < self.rank[ry]: rx, ry = ry, rx self.par[rx] += self.par[ry] self.par[ry] = rx if self.rank[rx] == self.rank[ry]: self.rank[rx] += 1 def is_same(self, x, y): return self.root(x) == self.root(y) def count(self, x): return -self.par[self.root(x)] n = int(input()) p = list(map(int, input().split())) uf = Unionfind(n) for i in range(n): uf.unite(i, p[i] - 1) rs = set(uf.root(i) for i in range(n)) l = [] for r in rs: l.append(uf.count(r)) l.sort(reverse=True) if len(l) == 1: print(n * n) exit() ans = (l[0] + l[1]) ** 2 for i in range(2, len(l)): ans += l[i] ** 2 print(ans)
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) visited = [0] * n a = list(map(int, input().split())) sizes = [] for i in range(n): if visited[i]: continue dest = a[i] - 1 visited[i] = 1 count = 1 while dest != i: visited[dest] = 1 dest = a[dest] - 1 count += 1 sizes.append(count) sizes.sort() if len(sizes) == 1: print(n * n) exit() q = sizes[-1] r = sizes[-2] ans = (q + r) * (q + r) for i in range(len(sizes) - 2): ans += sizes[i] * sizes[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = list(map(int, input().split())) results = list() stoplist = list() for i in p: if i == 0: continue j = p[i - 1] p[i - 1] = 0 result = 1 while j != i: t = p[j - 1] p[j - 1] = 0 j = t result += 1 results.append(result) results.sort(reverse=True) if len(results) < 2: print(sum([(x**2) for x in results])) elif len(results) == 2: print((results[0] + results[1]) ** 2) else: print(sum([(results[0] + results[1]) ** 2] + [(x**2) for x in results[2:]]))
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 ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = list(map(int, input().split())) c = [] visited = [0] * (n + 1) for i in range(n): if visited[p[i]] == 0: ct = 1 t = p[p[i] - 1] visited[p[i]] = 1 while t != p[i]: visited[t] = 1 t = p[t - 1] ct += 1 c.append(ct) c.sort() if len(c) == 1: print(c[0] ** 2) else: aux = c[-1] + c[-2] c = c[:-2] + [aux] print(sum(i**2 for i in c))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) a = list(map(int, input().split())) b = [] chains = [] for i in range(n): b.append(True) for i in range(n): if b[i]: chains.append([i]) j = i b[j] = False while a[j] - 1 != chains[-1][0]: chains[-1].append(a[j] - 1) j = a[j] - 1 b[j] = False chainlen = list(map(len, chains)) if len(chains) > 1: max1 = max(chainlen) max1 = chainlen.index(max1) chainlen[max1] *= -1 max2 = max(chainlen) max2 = chainlen.index(max2) chainlen[max2] = chainlen[max2] + -1 * chainlen[max1] del chainlen[max1] con = 0 for i in chainlen: con += i * i print(con)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = [0] * (n + 1) p[1 : n + 1] = list(map(int, input().split())) c = [] used = [False] * (n + 1) for i in range(1, n + 1): if used[i]: continue s = list() s.append(i) used[i] = True j = p[i] while not used[j]: s.append(j) used[j] = True j = p[j] c.append(len(s)) if len(c) == 1: print(n * n) else: c.sort() c.reverse() m = c[0] + c[1] print(m * m + sum([(d * d) for d in c[2:]]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = [(int(i) - 1) for i in input().split()] visited = [(0) for _ in range(n)] cycle = [0, 0] for i in range(n): j = i count = 0 while not visited[j]: visited[j] = 1 count += 1 j = p[j] if count > 0: cycle.append(count) cycle.sort() print(sum([(i * i) for i in cycle]) + 2 * cycle[-1] * cycle[-2])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
def inp(): n = int(input()) return [[int(x), False] for x in input().split()] def main(): struct = inp() cycles = [] for s in struct: if not s[1]: s[1] = True cycleLen = 1 rec = s[0] - 1 while not struct[rec][1]: struct[rec][1] = True rec = struct[rec][0] - 1 cycleLen += 1 cycles.append(cycleLen) fM, sM, s = 0, 0, 0 for c in cycles: if c > fM: sM = fM fM = c elif c > sM: sM = c s += c**2 s = s - fM**2 - sM**2 + (fM + sM) ** 2 return s answer = main() print(answer)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN LIST FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = list(map(int, input().split())) for i in range(n): p[i] -= 1 color = [0] * n c = 1 s = [] for i in range(n): if color[i] == 0: j, k = i, 1 color[i] = c while p[j] != i: j = p[j] color[j] = c k += 1 c += 1 s.append(k) s.sort() if len(s) == 1: x = s[0] ** 2 else: x = (s[-1] + s[-2]) ** 2 for i in range(len(s) - 2): x += s[i] ** 2 print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) a = list(map(int, input().split())) b = [0] * n k1 = 0 s = [] for i in range(n): if b[i] == 0: j = a[i] - 1 k = 1 b[i] = 1 sp = [i] while b[j] == 0: sp.append(j) k += 1 j = a[j] - 1 s.append(min(k1, k)) k1 = max(k1, k) for u in sp: b[u] = k k2 = max(s) print(sum(list(map(lambda x: x * x, s))) + k1 * k1 + 2 * k1 * k2)
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 LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) line = list(map(int, input().split())) max1, max2 = -1, -1 ar = [] colors = [(False) for i in range(n)] for i in range(n): if colors[i] == True: continue x = line[i] - 1 colors[i] = True cnt = 1 while colors[x] == False: colors[x] = True x = line[x] - 1 cnt += 1 if cnt >= max1: ar.append(max2) max2 = max1 max1 = cnt elif cnt > max2: ar.append(max2) max2 = cnt else: ar.append(cnt) if max2 == -1: max2 = 0 ans = (max1 + max2) ** 2 for elem in ar: if elem != -1: ans += elem**2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = list(map(int, input().split())) p = [(i - 1) for i in p] vis = [(False) for _ in range(n)] sz = [0] for i in range(n): j = i cnt = 0 while not vis[j]: vis[j] = True cnt += 1 j = p[j] if 0 < cnt: sz.append(cnt) sz.sort() print(sum(list(map(lambda x: x * x, sz))) + 2 * sz[-1] * sz[-2])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) a = [i for i in map(lambda x: int(x) - 1, input().split())] visited, comp = [False] * n, 0 nmbs = [] for i, v in enumerate(a): if not visited[i]: t = i while not visited[t]: visited[t] = True comp += 1 t = a[t] nmbs.append(comp) comp = 0 nmbs.sort() nmbs.reverse() if len(nmbs) == 1: print(nmbs[0] * nmbs[0]) exit(0) nmbs[0], nmbs[1] = nmbs[0] + nmbs[1], 0 print(sum(x * x for x in nmbs))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = list(map(int, input().split())) vis = set() comps = [] for i in range(n): if i in vis: continue sz = 0 j = i while j not in vis: vis.add(j) sz += 1 j = p[j] - 1 comps.append(sz) comps = sorted(comps) if len(comps) == 1: print(n * n) else: a = comps[-1] + comps[-2] ans = a * a + sum([(c * c) for c in comps[:-2]]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
from itertools import product def solve(): stations = int(input()) arr = [(int(x) - 1) for x in input().split(" ")] cycles = [] discovered = {} for i in arr: if i in discovered: continue count = 1 dest = arr[i] path = [dest] discovered[dest] = 1 while dest != i: count += 1 dest = arr[dest] path.append(dest) discovered[dest] = 1 cycles.append(path) if len(cycles) == 1: print(stations * stations) return longest = sorted(cycles, key=len)[len(cycles) - 2 :] joined = longest[0] + longest[1] cycles = sorted(cycles, key=len)[: len(cycles) - 2] cycles.append(joined) total = 0 for cycle in cycles: total += len(cycle) ** 2 print(total) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) st = [[int(x), False] for x in input().split()] groups = [] ptr = 0 while True: if ptr >= n: break if st[ptr][1]: ptr += 1 continue cnt = 0 cur = ptr while True: st[cur][1] = True cnt += 1 cur = st[cur][0] - 1 if cur == ptr: break groups.append(cnt) ptr += 1 groups.sort(reverse=True) if len(groups) == 1: print(n**2) else: ans = (groups[0] + groups[1]) ** 2 for i, cnt in enumerate(groups): if i < 2: continue ans += cnt**2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) a = [(int(x) - 1) for x in input().split()] vis = [False] * n cyc = [] ans = 0 for i in range(n): if not vis[i]: here = a[i] vis[i] = True ct = 1 while here != i: vis[here] = True here = a[here] ct += 1 cyc.append(ct) cyc.sort() if len(cyc) > 1: cyc.append(cyc.pop() + cyc.pop()) for i in cyc: ans += i * i print(ans if ans > 0 else 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) tabp = list(map(int, input().split(" "))) nMax = 0 nSecondMax = 0 counter = 1 Visited = set([]) i = 0 convenience = 0 while i < n: Visited.add(i) j = tabp[i] - 1 counter = 1 while j != i: counter += 1 Visited.add(j) j = tabp[j] - 1 if counter >= nMax: nSecondMax = nMax nMax = counter elif counter >= nSecondMax: nSecondMax = counter convenience += counter * counter for k in range(i + 1, n + 1): if k not in Visited: i = k break if nSecondMax > 0: convenience = ( convenience - nSecondMax * nSecondMax - nMax * nMax + (nSecondMax + nMax) * (nSecondMax + nMax) ) print(convenience)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) ps = list(map(lambda x: int(x) - 1, input().split())) sum = 0 maxest_pipe = 0 max_pipe = 0 free_points = set(range(n)) free_points.remove(0) start_point = 0 current_point = 0 steps = 0 while True: steps += 1 next_point = ps[current_point] if next_point in free_points: free_points.remove(next_point) if next_point == start_point: sum += steps * steps if steps > maxest_pipe: max_pipe = maxest_pipe maxest_pipe = steps elif steps > max_pipe: max_pipe = steps steps = 0 if len(free_points) > 0: current_point = free_points.pop() start_point = current_point else: break else: current_point = next_point print( sum - max_pipe * max_pipe - maxest_pipe * maxest_pipe + (max_pipe + maxest_pipe) * (max_pipe + maxest_pipe) )
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = [int(x) for x in input().split()] p.insert(0, -3) max1, max2 = 0, 0 used = [0] * (n + 1) count = 0 for i in range(1, n + 1): m = 0 v = i while used[p[v]] == 0: used[p[v]] = 1 v = p[v] m += 1 if m > max2: max2 = m if max2 > max1: max2, max1 = max1, max2 count += m**2 count = count - max1**2 - max2**2 + (max1 + max2) ** 2 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) (*a,) = map(int, input().split()) vis = set(a) ans = [] while vis: v = vis.pop() cur = 1 while a[v - 1] in vis: v = a[v - 1] vis.remove(v) cur += 1 ans.append(cur) s = sum(i**2 for i in ans) if len(ans) == 1: print(s) exit() ans.sort() print(s + 2 * ans[-1] * ans[-2])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
class node: def __init__(self, index): self.index = index self.nextN = None self.visited = 0 n = int(input()) nodes = [node(i + 1) for i in range(n)] c = [int(i) for i in input().split()] i = 0 while i < n: nodes[i].nextN = nodes[c[i] - 1] i += 1 sequence = [] i = 0 news = [] while i < n: x = i while nodes[x].visited != 1: news.append(nodes[x]) nodes[x].visited = 1 x = nodes[x].nextN.index - 1 sequence.append(news) news = [] i += 1 sequence.sort(key=len, reverse=True) if n == 1: print(1) elif n == 2: print(4) else: x = len(sequence[0]) + len(sequence[1]) x *= x som = 0 for i in range(2, n): value = len(sequence[i]) if value == 0: break som += value * value print(x + som)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
import sys n = int(input()) p = [(int(x) - 1) for x in input().split()] visited = [False] * n cycles = [] if n == 1: print(1) sys.exit(0) for i in range(n): if not visited[i]: v = p[i] visited[v] = True k = 1 while v != i: v = p[v] visited[v] = True k += 1 cycles.append(k) cycles.sort() print(sum(map(lambda x: x**2, cycles[:-2])) + sum(cycles[-2:]) ** 2)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) inp = input() inp = inp.split() for i in range(n): inp[i] = int(inp[i]) vis = [(0) for i in range(n)] siz = [] for i in range(n): if vis[i] == 1: continue vis[i] = 1 j = inp[i] - 1 count = 1 while vis[j] != 1: count += 1 vis[j] = 1 j = inp[j] - 1 siz.append(count) siz.sort(reverse=True) if len(siz) == 1: print(siz[0] ** 2) else: ans = (siz[0] + siz[1]) ** 2 for i in range(2, len(siz)): ans += siz[i] * siz[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = list(map(int, input().split())) used = [False] * n nums = list() def count(i): c = 0 while True: if used[i]: return c else: used[i] = True i = p[i] - 1 c += 1 for i in range(n): if not used[i]: nums.append(count(i)) if n == 1: print(1) elif n == 2: print(4) else: nums.sort(reverse=True) if len(nums) > 1: c = (nums[0] + nums[1]) ** 2 for i in nums[2:]: c += i**2 print(c) else: print(nums[0] ** 2)
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 FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = list(map(int, input().split())) vis = [False] * n nv = 0 m = 0 cyc = [] while nv < n: while vis[m]: m += 1 temp = [] c = m while not vis[c]: temp.append(c) nv += 1 vis[c] = True c = p[c] - 1 cyc.append(temp) size = [] for i in range(len(cyc)): size.append(len(cyc[i])) size = sorted(size) if len(size) == 1: print(size[0] * size[0]) else: out = (size[len(size) - 1] + size[len(size) - 2]) * ( size[len(size) - 1] + size[len(size) - 2] ) for i in range(len(size) - 2): out += size[i] * size[i] print(out)
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 ASSIGN VAR LIST WHILE VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) graph = {} a = input().split() for i in range(n): graph[i] = int(a[i]) - 1 vis = [False] * n cycles = [] for i in graph.keys(): if not vis[i]: cv = graph[i] vis[i] = True size = 1 while cv != i: vis[cv] = True cv = graph[cv] size += 1 cycles.append(size) cycles.sort(reverse=True) v = 0 if len(cycles) >= 2: v += (cycles[0] + cycles[1]) ** 2 for i in cycles[2:]: v += i**2 else: v = cycles[0] ** 2 print(v)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) P = [(int(x) - 1) for x in input().split()] visited = [-1] * n cc = [] for i in range(n): if visited[i] == -1: visited[i] = 0 amount = 1 x = P[i] while visited[x] == -1: visited[x] = 0 amount += 1 x = P[x] cc += [amount] cc.sort() result = 0 merged_size = cc[-1] if len(cc) > 1: merged_size += cc[-2] result = merged_size * merged_size for i in range(len(cc) - 2): result += cc[i] * cc[i] print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) data = list(map(int, input().split())) gp = {i: i for i in range(n)} sets = {i: [i] for i in range(n)} for i in range(n): u, v = i, data[i] - 1 if gp[u] != gp[v]: temp = gp[v] for i in sets[gp[v]]: gp[i] = gp[u] sets[gp[u]].append(i) del sets[temp] l = [] for i in sets: l.append(len(sets[i])) l.sort(reverse=True) n = len(l) if n == 1: print(l[0] * l[0]) else: ans = (l[0] + l[1]) ** 2 for i in range(2, n): ans += l[i] ** 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: For each station i there exists exactly one train that goes from this station. Its destination station is p_{i}, possibly p_{i} = i; For each station i there exists exactly one station j such that p_{j} = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of p_{i} for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! -----Input----- The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the current structure of the subway. All these numbers are distinct. -----Output----- Print one number — the maximum possible value of convenience. -----Examples----- Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 -----Note----- In the first example the mayor can change p_2 to 3 and p_3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p_2 to 4 and p_3 to 5.
n = int(input()) p = [0] + list(map(int, input().split())) vis = [0] * (n + 1) part = [] for i in range(1, n + 1): if not vis[i]: tot = 0 x = i while not vis[x]: tot += 1 vis[x] = 1 x = p[x] part.append(tot) part.sort(reverse=True) if len(part) == 1: print(n * n) else: ans = (part[0] + part[1]) ** 2 for x in part[2:]: ans += x * x print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() b = 1 up = len(s) - 1 out = [0] * len(s) while up > -1: if b == 1: if s[up] == "b": out[up] = 0 else: out[up] = 1 b = (b + 1) % 2 elif s[up] == "a": out[up] = 0 else: out[up] = 1 b = (b + 1) % 2 up -= 1 ans = "" for i in range(len(s)): ans += str(out[i]) + " " print(ans[:-1])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = list(input()) ones = 0 for i in s: if i == "a": ones += 1 else: break res = [0] * len(s) for prefix in range(1, len(s) + 1): string = s[:prefix] one = 0 for h in range(prefix - 1, -1, -1): if string[h] == "a": one += 1 else: break if ones < one: if prefix < len(s) and s[prefix] == "a": continue ones = one s[:prefix] = s[:prefix][::-1] res[prefix - 1] = 1 elif prefix < len(s): if s[0] == "a" and s[prefix] == "a": s[:prefix] = s[:prefix][::-1] res[prefix - 1] = 1 print(*res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = list(input()) n = len(s) R = [0] * n for i in range(n - 1): if s[i] == "b": if s[i + 1] == "a": R[i] = 1 else: pass elif s[i + 1] == "a": pass else: R[i] = 1 if s[n - 1] == "a": R[n - 1] = 1 print(*R)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
n = input() x = [0] * len(n) for i in range(len(n) - 1): if n[i] is n[i + 1]: continue else: x[i] = 1 if n[-1] is "a": x[-1] = 1 for i in range(len(n)): print(x[i], end=" ")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
from itertools import groupby s = "#" + input() l = len(s) ans = [0] * l for i in range(1, l): if s[i] == "a": ans[i - 1] ^= 1 ans[i] = 1 print(*ans[1:])
ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
gi = lambda: list(map(int, input().split())) s = input() n = len(s) ans = [(0) for e in range(n)] k = 1 while k < n: if s[k] == "a": temp = k while k < n and s[k] == "a": k += 1 ans[temp - 1] = ans[k - 1] = 1 k += 1 print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() l = [] for i in range(1, len(s) + 1): if s[0] == "b": if i == len(s) or s[i - 1] == "a" and s[i] == "b": l.append(1) s = s[:i][::-1] + s[i:] else: l.append(0) elif i < len(s) and s[i - 1] == "b" and s[i] == "a": l.append(1) s = s[:i][::-1] + s[i:] else: l.append(0) for x in l: print(x, end=" ") print()
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = list(input()) for i in range(len(s) - 1): print(int(s[i] != s[i + 1])) if s[i] != s[i + 1]: s[: i + 1] = s[i::-1] print(int(s[-1] == "a"))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() + "b" for i in range(len(s) - 1): if s[i] == "a" and s[i + 1] == "b" or s[i] == "b" and s[i + 1] == "a": print(1, end=" ") else: print(0, end=" ")
ASSIGN VAR BIN_OP FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() n = len(s) a = s.count("a") s1 = "" count = [] i = 0 while i < n: count.append(0) s1 = s1 + s[i] while i + count[-1] < n and s[i + count[-1]] == s[i]: count[-1] += 1 i += count[-1] nc = len(count) if s1[nc - 1] == "a": for i in range(nc): for j in range(1, count[i]): print(0, end=" ") print(1, end=" ") else: bs = 0 while bs + 1 <= n and s[-bs - 1] == "b": bs += 1 for i in range(nc - 1): for j in range(1, count[i]): print(0, end=" ") print(1, end=" ") for i in range(bs): print(0, end=" ")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() for i in range(len(s) - 1): if s[i + 1] == s[0]: print(1, end=" ") s = s[i::-1] + s[i + 1 :: 1] else: print(0, end=" ") if s[0] == "a": print(0) else: print(1)
ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER STRING IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
from itertools import groupby s = "#" + input() l = len(s) ss = s[1:] ans1 = [0] * (l - 1) gb = [list(g) for k, g in groupby(ss)] ll, first_a, turn_a = 0, False, False for i in gb: if i[0] == "a": if not first_a: ll += len(i) ans1[ll - 1] = 1 first_a = True turn_a = True else: ll += len(i) ans1[ll - 1] = 1 turn_a = True else: ll += len(i) ans1[ll - 1] = 1 turn_a = False if not turn_a: ans1[ll - 1] = 0 print(*ans1)
ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR NUMBER STRING IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() test = [] tot = [] for item in range(1, len(s)): pod = list(s[0:item]) if pod[0] == s[item]: tot.append(1) pod.reverse() pod_1 = "" for elem in pod: pod_1 += elem s = pod_1 + s[item:] else: tot.append(0) tot_1 = "" if s[-1] == "a": tot.append(1) else: tot.append(0) for item in tot: tot_1 += str(item) + " " print(tot_1[:-1])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input().strip() n = len(s) ret = [0] * n for i in range(1, n): if s[i] == "a": ret[i] = 1 ret[i - 1] ^= 1 print(*ret)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
def f(mag): if len(mag) == 1: return [] elif mag[-1] == "b": return f(mag[:-1]) else: boi = len(mag) old = f(mag[:-1]) if boi - 1 in old: old.pop() old.append(boi) return old else: old.append(boi - 1) old.append(boi) return old string = input() k = f(string) out = "" for i in range(len(string)): if i + 1 in k: out += "1 " else: out += "0 " print(out[:-1])
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST IF VAR NUMBER STRING RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
def main(): s = input() aFront = 1 ans = [] for i in range(1, len(s) + 1): needAFront = 1 if i < len(s) and s[i] == "a": needAFront = 0 if aFront != needAFront: ans.append(1) else: ans.append(0) aFront = needAFront print(*ans) return main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = [(0 if x == "a" else 1) for x in input().strip()] ls = len(s) sw = [0] * ls fe = le = s[0] for i in range(1, ls, 1): if s[i] != le: sw[i - 1] = 1 fe = le le = s[i] if fe > s[ls - 1]: sw[ls - 1] = 1 print(*sw)
ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() if s[0] == "a": swp = ["a", "b"] else: swp = ["b", "a"] ans = [0] * len(s) for i in range(1, len(s)): if s[i] == swp[1] and s[i - 1] == swp[0]: ans[i - 1] = 1 x = swp[1] swp[1] = swp[0] swp[0] = x if s[-1] == "a": ans[-1] = 1 print(*ans)
ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR LIST STRING STRING ASSIGN VAR LIST STRING STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() ls = [] leftmost = s[0] for i in range(0, len(s) - 1): rightmost = s[i] after = s[i + 1] if after != rightmost: ls.append(1) leftmost, rightmost = rightmost, after else: ls.append(0) rightmost = after if leftmost == "b": ls.append(1) else: ls.append(0) print(" ".join(map(str, ls)))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = list(input()) n = len(s) arr = [0] * n i = 1 while i < n: if s[i] == "b": arr[i - 1] = 1 while s[i] == "b": i += 1 if i == n: break if i != n and s[i - 1] == "b": arr[i - 1] = 1 i += 1 if s[n - 1] != "b": arr[-1] = 1 print(" ".join(map(str, arr)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR STRING VAR NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
a = input() i = 1 n = len(a) s = [] while i < n: if a[0] == "a" and a[i] == "a": search = 0 a = a[:i][::-1] + a[i:] search = 0 s.append(i) elif a[0] == "b" and a[i] == "b": search = 1 a = a[:i][::-1] + a[i:] search = 1 s.append(i) i += 1 b = [0] * n for i in range(0, len(s)): b[s[i] - 1] = 1 cb = a.count("b") ca = a.count("a") if "a" * ca + "b" * cb != a: b[n - 1] = 1 print(*b)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR IF VAR NUMBER STRING VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
a = list(input()) a.append("b") kek = [] for i in range(len(a) - 1): if a[i] == "a" and a[i + 1] == "b" or a[i] == "b" and a[i + 1] == "a": kek.append(1) else: kek.append(0) print(*kek)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() l = [[(0) for i in range(len(s))] for j in range(2)] for i in range(len(s)): if s[i] == "b": l[0][i] = 0 l[1][i] = 1 else: l[0][i] = 1 l[1][i] = 0 ans = [] w = 0 for i in range(len(s) - 1, -1, -1): ans.append(l[w][i]) w = abs(w - l[w][i]) ans = ans[::-1] print(*ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() c = [0] n = len(s) for i in range(1, n - 1): if s[i + 1] == s[0]: c.append(1) s = s[: i + 1][::-1] + s[i + 1 :] else: c.append(0) if n == 1: print(0) elif n == 2: if s == "aa" or s == "bb" or s == "ab": print(0, 0) elif s == "ba": print(0, 1) else: if s[-1] == "a": c.append(1) else: c.append(0) print(*c)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = [(0 if x == "a" else 1) for x in input().strip()] ls = len(s) sw = [0] * ls for i in range(ls - 1): if s[i] != s[i + 1]: sw[i] = 1 for j in range((i + 1) // 2): s[j], s[i - j] = s[i - j], s[j] if s[0] > s[ls - 1]: sw[ls - 1] = 1 print(*sw)
ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
def solve(s): res = [(0) for _ in range(len(s))] for i in range(1, len(s)): if s[i] == "a": res[i] = 1 res[i - 1] = 1 - res[i - 1] return res lst = list(input()) print(" ".join(list(map(str, solve(lst)))))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() idx = [] n = len(s) if n == 1: print(0) else: for i in range(1, n): if s[i] != s[i - 1]: idx.append(i - 1) idx.append(n - 1) res = [0] * n if s[-1] == "b": for i in range(1, len(idx) - 1): res[idx[i]] = 1 else: for i in range(1, len(idx)): res[idx[i]] = 1 print(" ".join(map(str, res)))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input().strip() n = len(s) b = s[:1], [0] w = s[:1], [0] for i in range(1, n): b0 = b[0] + s[i] b1 = s[i] + w[0][::-1] w0 = w[0] + s[i] w1 = s[i] + b[0][::-1] bcopy = b if b0 < b1: b = b0, b[1] + [0] else: b = b1, w[1] + [1] if w0 > w1: w = w0, w[1] + [0] else: w = w1, bcopy[1] + [1] print(*b[1])
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER LIST NUMBER ASSIGN VAR VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER LIST NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() result = [(0) for i in range(len(s))] pWa = 0 for i in reversed(range(len(s))): if s[i] == "a": if not pWa: result[i] = 1 if i - 1 >= 0 and s[i - 1] == "b": result[i - 1] = 1 pWa = 1 else: pWa = 0 print(*result)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() ans = [0] for i in range(1, len(s)): if s[i] == "a": ans[-1] = 1 - ans[-1] ans.append(1) else: ans.append(0) print(" ".join([str(i) for i in ans]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
string = input() sortedS = list(string) sortedS.sort() n = len(string) start = 0 end = n - 1 order = False i = n - 1 swap = ["0"] * n while i >= 0: if order: if sortedS[start] != string[i]: swap[i] = "1" order = not order start += 1 else: if sortedS[end] != string[i]: swap[i] = "1" order = not order end -= 1 i -= 1 print(" ".join(swap))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR WHILE VAR NUMBER IF VAR IF VAR VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
a = list(input()) b = [] c = 0 for i in range(len(a) - 1): if a[i] != a[i + 1]: b.append(i) d = [0] * len(a) for i in range(1, len(b)): d[b[i]] = 1 if a[-1] == "a": d[-1] = 1 print(*d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
def min_word(s): n, flag = len(s), 0 result = [0] * n for i in range(n - 1, -1, -1): if s[i] == "a": result[i] = 1 - flag flag = 1 else: result[i] = flag flag = 0 return result print(*min_word(input()))
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
def solve(s): i = s.rfind("a") if i == -1: return s s = input() n = len(s) ans = ["0" for i in range(n)] for i in range(n - 1): if s[0] == s[i + 1]: s = s[i::-1] + s[i + 1 :] ans[i] = "1" if s[-1] == "a": ans[-1] = "1" print(" ".join(ans))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
a = list(input()) flag = a[0] == "a" for i in range(len(a) - 1): if a[i] != a[i + 1]: print(1) a[: i + 1] = a[i::-1] flag = a[0] == "a" else: print(0) if a[0] == "a": print(0) else: print(1) a[:] = a[::-1]
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() n = len(s) dp = [(0) for i in range(n)] i = 1 while i < n: if s[i] == "a": dp[i - 1] = 1 while i < n and s[i] == "a": i += 1 i -= 1 dp[i] = 1 i += 1 print(*dp)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() s = s + "b" k = "" i = 1 while i < len(s): if s[i - 1 : i + 1] == "ba" or s[i - 1 : i + 1] == "ab": k += " 1" else: k += " 0" i += 1 print(k[1:])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR STRING VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() a = [0] if len(s) == 1: print(0) exit(0) for i in range(1, len(s) - 1): if s[i] != s[i + 1]: a.append(1) else: a.append(0) if s[-1] == "b": a.append(0) else: a.append(1) print(*a)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() arr_ans = [0] * len(s) for i in range(1, len(s)): if s[i] == "a": arr_ans[i - 1] = (arr_ans[i - 1] + 1) % 2 arr_ans[i] += 1 print(*arr_ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() n = len(s) ans = [] for i in range(n - 1): if s[i] != s[i + 1]: ans.append(1) else: ans.append(0) if s[-1] == "a": ans.append(1) else: ans.append(0) for t in ans: print(t, end=" ")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
a = input() res, string, n = [], "", len(a) for i, x in enumerate(a): string += x s = string[::-1] if i < n - 1: if a[i + 1] == string[0]: string = s res.append(1) continue elif s < string: string = s res.append(1) continue res.append(0) print(*res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST STRING FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
def main(): s = list(input()) n = len(s) res = [0] * n for i in range(1, n): if s[i] == "a": res[i - 1] ^= 1 res[i] = 1 print(" ".join([str(c) for c in res])) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = list(input()) n = len(s) ans = [(0) for i in range(n + 1)] for i in range(1, n + 1): if s[i - 1] == "a": ans[i - 1] ^= 1 ans[i] = 1 for i in range(1, n + 1): print(ans[i], end=" ") print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
a = input() ans = [0] for i in range(1, len(a)): if a[i] == "a": ans[-1] ^= 1 ans.append(1) else: ans.append(0) print(*ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
base = input() pseq = [] beg = 0 c = base[0] rots = [] for i in range(1, len(base)): if base[i - 1] == "b" and base[i] == "a": base = base[i - 1 :: -1] + base[i:] rots.append(i - 1) elif base[i - 1] == "a" and base[i] == "b": base = base[i - 1 :: -1] + base[i:] rots.append(i - 1) if base[-1] == "a": rots.append(len(base) - 1) base = base[::-1] i = 0 out = [] for k in range(len(base)): if i < len(rots) and k == rots[i]: i += 1 out.append("1") else: out.append("0") print(" ".join(out))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
import sys input = lambda: sys.stdin.readline().strip("\r\n") a = list(input()) n = len(a) ans = [0] * n for i in range(1, n): if a[i] == "a": ans[i - 1] ^= 1 ans[i] = 1 print(*ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
a = list(map(str, input())) l = len(a) res = [0] * l for i in range(1, l): if a[i] == a[0]: res[i - 1] = 1 a[:i] = list(reversed(a[:i])) if a[l - 1] == "a" and a[0] != a[l - 1]: res[l - 1] = 1 for p in res: print(p, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
u = list(input()) n = len(u) ans = [0] * n for i in range(1, n): if u[i] != u[i - 1]: ans[i - 1] = 1 if u[-1] == "a": ans[-1] = 1 print(" ".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
import sys input = lambda: sys.stdin.readline().rstrip() s = [*input()] ans = [0] * len(s) for i in range(1, len(s)): if s[i] == "a": ans[i - 1] ^= 1 ans[i] = 1 print(*ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
def scand(): return int(input()) def scana(): return [int(x) for x in input().split()] t = input() ans = ["" for char in t] n = len(t) flag = "a" for i in range(n): if t[n - 1 - i] == flag: ans[n - 1 - i] = "1" flag = "b" if flag == "a" else "a" else: ans[n - 1 - i] = "0" print(" ".join(ans))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR STRING STRING STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
s = input() n = len(s) ans = [False] * n for i in range(1, n): if s[i] == "a": ans[i - 1] = not ans[i - 1] ans[i] = True print(*list(map(int, ans)))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'.
def read_int(): return int(input().strip()) def read_ints(): return list(map(int, input().strip().split(" "))) def solve(): S = input().strip() N = len(S) C = [False] * N for i in range(1, N): if S[i] == "a": C[i - 1] = not C[i - 1] C[i] = True else: C[i] = False return " ".join(str(int(c)) for c in C) print(solve())
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR