description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) k = input().split() ans = 0 for i in range(0, len(k)): if k[i] == "0": ans += 1 elif i != 0: if k[i] == "3" and k[i - 1] == "2": k[i] = "1" elif k[i] == "3" and k[i - 1] == "1": k[i] = "2" elif k[i] == "2" and k[i - 1] == "2": ans += 1 k[i] = "0" elif k[i] == "1" and k[i - 1] == "1": ans += 1 k[i] = "0" print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
from sys import stdin REST = 0 ITC = 1 GYM = 2 def solve(n, arr): days = [([0] * 3) for i in range(n + 1)] for i in range(1, n + 1): days[i][0] = max(days[i - 1]) if arr[i - 1] in [1, 3]: days[i][ITC] = max(days[i - 1][REST] + 1, days[i - 1][GYM] + 1) if arr[i - 1] in [2, 3]: days[i][GYM] = max(days[i - 1][REST] + 1, days[i - 1][ITC] + 1) return n - max(days[n]) n = int(stdin.readline()) arr = list(map(int, stdin.readline().split(" "))) print(solve(n, arr))
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = list(map(int, input().split())) mem = [[(0) for x in range(3)] for x in range(len(a))] first = a[0] if first == 1: mem[0] = [0, 1, 0] elif first == 2: mem[0] = [0, 0, 1] elif first == 3: mem[0] = [0, 1, 1] for i, x in enumerate(a[1:], 1): mem[i][0] = max(mem[i - 1]) if x == 1 or x == 3: mem[i][1] = max(mem[i - 1][0] + 1, mem[i - 1][2] + 1) else: mem[i][1] = mem[i - 1][1] if x == 2 or x == 3: mem[i][2] = max(mem[i - 1][0] + 1, mem[i - 1][1] + 1) else: mem[i][2] = mem[i - 1][2] print(n - max(mem[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) alist = list(map(int, input().split())) dp = [[(101) for _ in range(3)] for _ in range(n + 1)] for i in range(3): dp[0][i] = 0 for i in range(1, n + 1): dp[i][0] = min([dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]]) + 1 if alist[i - 1] == 1: dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) elif alist[i - 1] == 2: dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) elif alist[i - 1] == 3: dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) print(min([dp[n][0], dp[n][1], dp[n][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 NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
def vacations(n, a): s = None r = 0 for i in range(n): if a[i] == 0: r += 1 s = 0 elif a[i] == 1: if s == 1: r += 1 s = 0 continue else: s = 1 elif a[i] == 2: if s == 2: r += 1 s = 0 continue else: s = 2 elif s == 1: s = 2 elif s == 2: s = 1 elif i != n - 1 and a[i + 1] == 1: s = 2 elif i != n - 1 and a[i + 1] == 2: s = 1 print(r) return n = int(input()) a = list(map(int, input().split(" "))) vacations(n, a)
FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = list(map(int, input().split())) b = [[0, 0, 0]] for i in range(n): max_0 = max(b[-1]) max_1 = max(b[-1][0], b[-1][2]) + (a[i] % 2 > 0) max_2 = max(b[-1][0], b[-1][1]) + (a[i] // 2 > 0) b.append([max_0, max_1, max_2]) print(n - max(b[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) ls = list(map(int, input().split())) ans = 0 prev = 0 for x in ls: if x == prev or not x: prev = 0 ans += 1 elif x < 3: prev = x elif prev: prev = 3 - prev 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 NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = list(map(int, input().split())) INF = 10**9 def get_t(n, t): return n & t res = [[0, 0, 0] for _ in range(n)] res[-1] = [1, 0 if get_t(a[-1], 1) else 1, 0 if get_t(a[-1], 2) else 1] for i in range(n - 2, -1, -1): res[i] = [ 1 + min(res[i + 1]), min(res[i + 1][0], res[i + 1][2]) if get_t(a[i], 1) else INF, min(res[i + 1][0], res[i + 1][1]) if get_t(a[i], 2) else INF, ] print(min(res[0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF RETURN BIN_OP VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER LIST NUMBER FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) s = list(map(int, input().split())) r = [[0, 0, 0] for i in range(n)] if s[0] == 0: r[0] = [1, 1, 1] if s[0] == 1: r[0] = [1, 0, 1] if s[0] == 2: r[0] = [1, 1, 0] if s[0] == 3: r[0] = [1, 0, 0] for i in range(1, n): if s[i] == 0: r[i] = [(min(r[i - 1]) + 1) for j in range(3)] if s[i] == 1: r[i][0] = min(r[i - 1]) + 1 r[i][1] = min(r[i - 1][0], r[i - 1][2], r[i - 1][1] + 1) r[i][2] = min(r[i - 1]) + 1 if s[i] == 2: r[i][0] = min(r[i - 1]) + 1 r[i][2] = min(r[i - 1][0], r[i - 1][1], r[i - 1][2] + 1) r[i][1] = min(r[i - 1]) + 1 if s[i] == 3: r[i][0] = min(r[i - 1]) + 1 r[i][1] = min(r[i - 1][0], r[i - 1][2], r[i - 1][1] + 1) r[i][2] = min(r[i - 1][0], r[i - 1][1], r[i - 1][2] + 1) print(min(r[n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
def vacations(n, d, a): prev = 3 res = 0 for i in a: if i == prev and prev != 3: i = 0 elif i == 3 and prev != 3: i -= prev if i == 0: res += 1 prev = i return res n = input() d = input() a = list(map(int, d.split(" "))) print(vacations(n, d, a))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) days = list(map(int, input().split())) arr = [] for i in days: if i == 0: arr.append("rest") elif i == 1: arr.append("contest") elif i == 2: arr.append("gym") else: arr.append("don't know") count = 0 for i in range(n): if i > 0: if arr[i] == arr[i - 1] or arr[i] == "rest": arr[i] = "rest" count += 1 elif arr[i] in ["gym", "contest"]: pass elif arr[i] == "don't know" and arr[i - 1] != "rest": if arr[i - 1] == "gym": arr[i] = "contest" elif arr[i - 1] == "contest": arr[i] = "gym" else: arr[i] = "rest" elif arr[i - 1] == "rest": arr[i] = "rest" else: if arr[0] == "rest": arr[0] = "rest" count += 1 if arr[0] == "don't know": arr[0] = "rest" print(count)
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 IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR LIST STRING STRING IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = list(map(int, input().split())) b = [([0] * 2) for i in range(n + 1)] b for i in range(n): if a[i] == 0: b[i + 1][0] = min(b[i][0], b[i][1]) + 1 b[i + 1][1] = min(b[i][0], b[i][1]) + 1 if a[i] == 1: b[i + 1][0] = b[i][1] b[i + 1][1] = min(b[i][0], b[i][1]) + 1 if a[i] == 2: b[i + 1][0] = min(b[i][0], b[i][1]) + 1 b[i + 1][1] = b[i][0] if a[i] == 3: b[i + 1][0] = b[i][1] b[i + 1][1] = b[i][0] print(min(b[n][0], b[n][1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) lst = list(map(int, input().split())) ans = 0 cur = 0 last = 0 i = 0 while i < n: if lst[i] == 0: ans += 1 i += 1 last = 0 continue if last == 0: if lst[i] == 3: j = i while j < n and lst[j] == 3: j += 1 i = j if j == n: i = n break elif lst[j] == 1: last = 2 elif lst[j] == 2: last = 1 else: last = lst[i] i += 1 elif last == lst[i]: ans += 1 last = 0 i += 1 else: if last == 1: last = 2 else: last = 1 i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) arr = [int(i) for i in input().split()] dp = [0] * n dp[0] = arr[0] for i in range(n): if arr[i] == 1 and dp[i - 1] != 1: dp[i] = 1 elif arr[i] == 2 and dp[i - 1] != 2: dp[i] = 2 elif arr[i] == 3: if dp[i - 1] == 1: dp[i] = 2 elif dp[i - 1] == 2: dp[i] = 1 else: dp[i] = 3 print(dp.count(0))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = list(map(int, input().split())) dp = [-1] * n act = [0] * n if a[0] == 1: act[0] = 1 dp[0] = 0 elif a[0] == 2: act[0] = 2 dp[0] = 0 elif a[0] == 0: act[0] = 0 dp[0] = 1 elif a[0] == 3: if len(a) == 1: act[0] = 1 elif a[1] == 1: act[0] = 2 elif a[1] == 2: act[0] = 1 dp[0] = 0 for i in range(1, len(a)): if a[i] == 0: act[i] = 0 dp[i] = dp[i - 1] + 1 elif a[i] == 1: if act[i - 1] == 1: act[i] = 0 dp[i] = dp[i - 1] + 1 else: act[i] = 1 dp[i] = dp[i - 1] elif a[i] == 2: if act[i - 1] == 2: act[i] = 0 dp[i] = dp[i - 1] + 1 else: act[i] = 2 dp[i] = dp[i - 1] elif a[i] == 3: if act[i - 1] == 1: act[i] = 2 elif act[i - 1] == 2: act[i] = 1 elif act[i - 1] == 0: if i == len(a) - 1: act[i] = 1 elif a[i + 1] == 1: act[i] = 2 elif a[i + 1] == 2: act[i] = 1 dp[i] = dp[i - 1] print(dp[-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = input().split() rests = 0 odd = True if a[0] == "0": rests += 1 for i in range(1, n): if a[i] == "0": odd = True rests += 1 elif a[i] == "3": if a[i - 1] == "2" and odd: a[i] = "1" elif a[i - 1] == "1" and odd: a[i] = "2" odd = True continue elif a[i] == a[i - 1]: odd = not odd if not odd: rests += 1 else: odd = True print(rests)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR ASSIGN VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
a = int(input()) days = [int(x) for x in input().split()] dp = [[666, 666, 666] for i in range(a)] if days[0] == 3: dp[0][0] = 0 dp[0][1] = 0 dp[0][2] = 0 else: dp[0][0], dp[0][days[0]] = 0, 0 dp[0][0] += 1 for x in range(1, a): dp[x][0] = min(dp[x - 1][:]) + 1 if days[x] == 3: dp[x][1] = min(dp[x - 1][0], dp[x - 1][2]) dp[x][2] = min(dp[x - 1][0], dp[x - 1][1]) elif days[x] == 2: dp[x][2] = min(dp[x - 1][0], dp[x - 1][1]) elif days[x] == 1: dp[x][1] = min(dp[x - 1][0], dp[x - 1][2]) print(min(dp[a - 1][:]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = list(map(int, input().split())) d = [] i = 0 while i < n: d.append([0, 0, 0]) if i == 0: if a[i] in [1, 3]: d[i][1] = 1 if a[i] in [2, 3]: d[i][2] = 1 elif i > 0: d[i][0] = max(d[i - 1]) if a[i] in [1, 3]: d[i][1] = max(d[i - 1][2] + 1, d[i - 1][0] + 1) if a[i] in [2, 3]: d[i][2] = max(d[i - 1][0] + 1, d[i - 1][1] + 1) i += 1 print(n - max(d[i - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n, vr, vg, vc = int(input()), 0, 0, 0 for x in map(int, input().split()): cr = min(vr, vg, vc) + 1 cg = min(vr, vc) if x & 2 else n cc = min(vr, vg) if x & 1 else n vr, vg, vc = cr, cg, cc print(min(vr, vg, vc))
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
INF = 10**9 n = int(input()) d = [0] + [int(i) for i in input().split()] dp = [[0, INF, INF]] + [[INF, INF, INF] for i in range(n)] for i in range(1, n + 1): if d[i] & 1: dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) if d[i] & 2: dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) if d[i] == 0 or dp[i][2] == INF and dp[i][1] == INF: dp[i][0] = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + 1 print(min(dp[n]))
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST LIST NUMBER VAR VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
from sys import exit n = int(input()) mas = list(map(int, input().split())) i = 1 e = mas[0] if e == 0: res = 1 else: res = 0 if e == 3: for i, x in enumerate(mas): if x != 3: break if mas[i] == 3: print(0) from sys import exit exit() if mas[i] == 2: e = 1 else: e = 2 for k in range(i, n): r = mas[k] if r == 0: res += 1 e = 0 continue if e != 3: if r == 3: if e == 1: e = 2 elif e == 2: e = 1 else: e = 3 elif e == r: e = 0 res += 1 else: e = r else: e = r print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n_day = eval(input("")) day = input("") days = day.split(" ") count_rest = 0 temp = "" for i in range(0, len(days)): if days[i] == "0": count_rest += 1 temp = "" elif days[i] == "1": if temp != "code": temp = "code" else: count_rest += 1 temp = "" elif days[i] == "2": if temp != "gym": temp = "gym" else: count_rest += 1 temp = "" elif days[i] == "3": if temp == "code": temp = "gym" elif temp == "gym": temp = "code" print(count_rest)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR STRING IF VAR VAR STRING IF VAR STRING ASSIGN VAR STRING VAR NUMBER ASSIGN VAR STRING IF VAR VAR STRING IF VAR STRING ASSIGN VAR STRING VAR NUMBER ASSIGN VAR STRING IF VAR VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
size = int(input()) days = [int(a) for a in input().split()] daysoff = 0 yesterday = 0 if days[0] == 0: daysoff += 1 yesterday = 0 elif days[0] == 3: yesterday = 0 else: yesterday = days[0] for i in range(1, size): if days[i] == 0: yesterday = 0 daysoff += 1 elif days[i] == yesterday: yesterday = 0 daysoff += 1 elif days[i] == 3 and yesterday == 0: yesterday = 0 elif days[i] == 3 and yesterday == 1: yesterday = 2 elif days[i] == 3 and yesterday == 2: yesterday = 1 else: yesterday = days[i] print(daysoff)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
def main(): input() a1 = a2 = x1 = x2 = z = 0 for b in map(int, input()[::2]): if 0 < b < 3: if a1 == b: a1 = 0 x1 += 1 else: a1 = b if a2 == b: a2 = 0 x2 += 1 else: a2 = b if x1 != x2: if x1 > x2: x1, a1 = x2, a2 else: x2, a2 = x1, a1 elif b: a1 = 1 if a1 != 1 else 2 a2 = 2 if a2 != 2 else 1 else: a1 = a2 = 0 z += 1 print(x1 + z) main()
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER IF NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
import sys sys.setrecursionlimit(2000) inf = float("inf") mod = 1000000007 def get_array(): return list(map(int, sys.stdin.readline().split())) def get_ints(): return map(int, sys.stdin.readline().split()) def input(): return sys.stdin.readline() def recurse(arr, i, flag): if i == len(arr): return 1 if dp[i][flag] != inf: return dp[i][flag] if arr[i] == 0: dp[i][flag] = min(dp[i][flag], 1 + recurse(arr, i + 1, 2)) elif arr[i] == 1: if flag != 1: dp[i][flag] = min( dp[i][flag], recurse(arr, i + 1, 1), 1 + recurse(arr, i + 1, 2) ) else: dp[i][flag] = min(dp[i][flag], 1 + recurse(arr, i + 1, 2)) elif arr[i] == 2: if flag != 0: dp[i][flag] = min( dp[i][flag], recurse(arr, i + 1, 0), 1 + recurse(arr, i + 1, 2) ) else: dp[i][flag] = min(dp[i][flag], 1 + recurse(arr, i + 1, 2)) elif arr[i] == 3: if flag == 0: dp[i][flag] = min( dp[i][flag], 1 + recurse(arr, i + 1, 2), recurse(arr, i + 1, 1) ) elif flag == 1: dp[i][flag] = min( dp[i][flag], 1 + recurse(arr, i + 1, 2), recurse(arr, i + 1, 0) ) elif flag == 2: dp[i][flag] = min( dp[i][flag], 1 + recurse(arr, i + 1, 2), recurse(arr, i + 1, 0), recurse(arr, i + 1, 1), ) return dp[i][flag] def main(): n = int(input()) arr = get_array() print(recurse(arr, 0, 2) - 1) dp = [[inf for _ in range(3)] for _ in range(101)] main()
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) days = input().split() last = "0" counter = 0 for i in range(n): if last == days[i] or days[i] == "0": counter += 1 last = "0" elif days[i] != "3": last = days[i] elif last == "1": last = "2" elif last == "2": last = "1" elif i < n - 1: if days[i + 1] == "1": last = "2" elif days[i + 1] == "2": last = "1" print(counter)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR STRING IF VAR VAR STRING ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) l = [int(i) for i in input().split()] l2 = [0, 0, 0] for i in range(n): a = l2[:] l2[0] = min(a) + 1 if l[i] == 1 or l[i] == 3: l2[1] = min(a[0], a[2]) else: l2[1] = 10**6 if l[i] == 2 or l[i] == 3: l2[2] = min(a[0], a[1]) else: l2[2] = 10**6 print(min(l2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) arr = list(map(int, input().split())) dp = [] for i in range(n): dp.append([0, 0]) if arr[0] == 1 or arr[0] == 3: dp[0][0] = 1 if arr[0] == 2 or arr[0] == 3: dp[0][1] = 1 for i in range(1, n): if arr[i] == 3: dp[i][0] = dp[i - 1][1] + 1 dp[i][1] = dp[i - 1][0] + 1 elif arr[i] == 1: dp[i][0] = dp[i - 1][1] + 1 dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) elif arr[i] == 2: dp[i][1] = dp[i - 1][0] + 1 dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) else: dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) print(n - max(dp[-1][0], dp[-1][1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) last = -1 a = list(map(int, input().split())) ans = 0 for x in a: if x == 0: last = -1 ans += 1 elif x < 3: if last == x: last = -1 ans += 1 continue last = x elif last == 1: last = 2 elif last == 2: last = 1 else: last = -1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) y = list(map(int, input().split())) y.append(0) i = 0 current = count = 0 while i < n: if y[i] == 0: current = 0 count += 1 i += 1 elif y[i] == 1: if current == "R": current = 0 count += 1 else: current = "R" i += 1 elif y[i] == 2: if current == "G": current = 0 count += 1 else: current = "G" i += 1 elif y[i + 1] == 1 or y[i + 1] == 0: if current == "R" or current == 0: current = "G" else: current = "R" i += 1 elif y[i + 1] == 2: if current == "G" or current == 0: current = "R" else: current = "G" i += 1 elif y[i + 1] == 3 and current == 0: j = i while j < n: if y[j] == 3: j += 1 else: break if j != n: if y[j] == 1: if (j - i) % 2 == 0: current = "R" else: current = "G" elif y[j] == 2: if (j - i) % 2 == 0: current = "G" else: current = "R" else: current = "R" i += 1 else: if current == "R": current = "G" else: current = "R" i += 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR STRING VAR NUMBER IF VAR VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING VAR NUMBER IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
def f(arr): prev_contest = float("-inf") prev_gym = float("-inf") prev_none = 0 for a in arr: contest = prev_contest gym = prev_gym none = prev_none if a & 1: contest = max(prev_gym + 1, prev_none + 1) if a & 2: gym = max(prev_contest + 1, prev_none + 1) none = max(prev_contest, prev_gym, prev_none) prev_none = none prev_contest = contest prev_gym = gym res = len(arr) - max(prev_none, prev_contest, prev_gym) return res assert f([1]) == 0 assert f([2]) == 0 assert f([3]) == 0 assert f([1, 2]) == 0 assert f([1, 3]) == 0 assert f([1, 1]) == 1 assert f([1, 3, 2]) == 1 assert f([2, 3, 1]) == 1 assert f([1, 3, 2, 0]) == 2 assert f([1, 3, 3, 2, 1, 2, 3]) == 0 assert f([2, 2]) == 1 X100 = [ 3, 2, 3, 3, 3, 2, 3, 1, 3, 2, 2, 3, 2, 3, 3, 3, 3, 3, 3, 1, 2, 2, 3, 1, 3, 3, 2, 2, 2, 3, 1, 0, 3, 3, 3, 2, 3, 3, 1, 1, 3, 1, 3, 3, 3, 1, 3, 1, 3, 0, 1, 3, 2, 3, 2, 1, 1, 3, 2, 3, 3, 3, 2, 3, 1, 3, 3, 3, 3, 2, 2, 2, 1, 3, 1, 3, 3, 3, 3, 1, 3, 2, 3, 3, 0, 3, 3, 3, 3, 3, 1, 0, 2, 1, 3, 3, 0, 2, 3, 3, ] assert len(X100) == 100 assert f(X100) == 16 n = int(input()) A = map(int, input().split()) ans = f(list(A)) print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR LIST NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = list(map(int, input().split())) arr = [[0, 0, 0]] for i in range(n): f = a[i] r = max(arr[i][0], arr[i][1], arr[i][2]) c = arr[i][1] g = arr[i][2] if f == 1 or f == 3: c = max(arr[i][0], arr[i][2]) + 1 if f == 2 or f == 3: g = max(arr[i][0], arr[i][1]) + 1 arr.append([r, c, g]) print(n - max(arr[-1][0], arr[-1][1], arr[-1][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 LIST LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
_ = input() activity = list(map(int, input().split())) dp = [[float("inf") for _ in range(3)] for _ in range(len(activity))] for i in range(3): dp[0][i] = 1 if activity[0] == 1: dp[0][1] = 0 elif activity[0] == 2: dp[0][2] = 0 elif activity[0] == 3: dp[0][1] = 0 dp[0][2] = 0 for i in range(1, len(activity)): dp[i][0] = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + 1 if activity[i] == 1 or activity[i] == 3: dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) if activity[i] == 2 or activity[i] == 3: dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) length = len(activity) - 1 print(min(dp[length][0], dp[length][1], dp[length][2]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = list(map(int, input().split())) memo = [([-1] * 4) for i in range(n)] def dp(i, prev): if i == n: return 0 if memo[i][prev] == -1: memo[i][prev] = 1 + dp(i + 1, 0) if a[i] == 1 or a[i] == 2: if a[i] != prev: memo[i][prev] = min(memo[i][prev], dp(i + 1, a[i])) elif a[i] == 3: for x in range(1, 3): if x != prev: memo[i][prev] = min(memo[i][prev], dp(i + 1, x)) return memo[i][prev] print(dp(0, 0))
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 NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) p = pp = res = 0 a = [int(i) for i in input().split()] for i in a: if i == p or i == 0: p = 0 res += 1 elif i == 1 or i == 2: p = i elif p != 0: p = 3 - p else: print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = list(map(int, input().split(" "))) code, sport = [0] * n, [0] * n for i in range(n): code[i] = max(code[i], a[i] % 2) sport[i] = max(sport[i], a[i] // 2) for j in range(i + 1, n): if a[j] % 2: code[j] = max(code[j], sport[i] + 1) if a[j] // 2: sport[j] = max(sport[j], code[i] + 1) for j in range(i + 2, n): if a[j] % 2: code[j] = max(code[j], code[i] + 1) if a[j] // 2: sport[j] = max(sport[j], sport[i] + 1) result = n - max(max(code), max(sport)) print(result)
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 VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
from sys import stdin, stdout num_days = int(stdin.readline()) days = list(map(int, stdin.readline().strip().split())) dp = [list(float("inf") for i in range(num_days)) for j in range(3)] dp[0][0] = 1 if days[0] == 1 or days[0] == 3: dp[1][0] = 0 if days[0] == 2 or days[0] == 3: dp[2][0] = 0 for i in range(1, num_days): dp[0][i] = min(dp[0][i - 1], dp[1][i - 1], dp[2][i - 1]) + 1 if days[i] == 1 or days[i] == 3: dp[1][i] = min(dp[0][i - 1], dp[2][i - 1]) if days[i] == 2 or days[i] == 3: dp[2][i] = min(dp[0][i - 1], dp[1][i - 1]) print(min(dp[0][num_days - 1], dp[1][num_days - 1], dp[2][num_days - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
def create_table(situations_list): the_table = [] if situations_list[0] == 0: the_table.append([1, 0]) elif situations_list[0] == 1: the_table.append([0, 1]) elif situations_list[0] == 2: the_table.append([0, 2]) else: the_table.append([0, 0]) for k in range(1, len(situations_list)): if situations_list[k] == 0: the_table.append([the_table[k - 1][0] + 1, 0]) elif situations_list[k] == 1: if the_table[k - 1][1] == 1: the_table.append([the_table[k - 1][0] + 1, 0]) else: the_table.append([the_table[k - 1][0], 1]) elif situations_list[k] == 2: if the_table[k - 1][1] == 2: the_table.append([the_table[k - 1][0] + 1, 0]) else: the_table.append([the_table[k - 1][0], 2]) elif the_table[k - 1][1] == 0: the_table.append([the_table[k - 1][0], 0]) elif the_table[k - 1][1] == 1: the_table.append([the_table[k - 1][0], 2]) else: the_table.append([the_table[k - 1][0], 1]) return the_table def create_days(string): the_list = [] string_split = string.split(" ") for item in string_split: the_list.append(int(item)) return the_list length = int(input()) day_string = input() day_list = create_days(day_string) table = create_table(day_list) print(table[-1][0])
FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
def get_ints(string): return list(map(int, string.split())) def get_input(): n = int(input()) xs = get_ints(input()) return n, xs def foo(xs, res, last): if len(xs) is []: return res for i, x in enumerate(xs): if x == 0: res += 1 last = 0 elif x == 1 and last == 1: res += 1 last = 0 elif x == 1: last = 1 elif x == 2 and last == 2: res += 1 last = 0 elif x == 2: last = 2 elif last == 1: last = 2 elif last == 2: last = 1 elif i < len(xs) - 1 and xs[i + 1] == 0: last = 1 else: return min(foo(xs[i + 1 :], res, 1), foo(xs[i + 1 :], res, 2)) return res def main(): n, xs = get_input() res = foo(xs, 0, 0) print(res) return res main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR LIST RETURN VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = [int(x) for x in input().split()] dp = [[(1000) for i in range(0, 3)] for j in range(0, 101)] dp[0][0] = dp[0][1] = dp[0][2] = 0 for i in range(1, n + 1): dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) dp[i][0] = min(dp[i][0], dp[i - 1][2]) dp[i][0] += 1 if a[i - 1] == 1: dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) elif a[i - 1] == 2: dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) elif a[i - 1] == 3: dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) o = min(dp[n][0], dp[n][1]) o = min(o, dp[n][2]) print(o)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
nb_days = int(input()) previous, restday = 0, 0 jobs = [int(x) for x in input().split()] for i in jobs: if i == 0: restday += 1 previous = 0 elif i == 3: previous = 3 - previous elif previous == i: restday += 1 previous = 0 else: previous = i print(restday)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = list(map(int, input().split())) dp = [([False] * 2) for i in range(n)] ans = 0 for i in range(n): if a[i] == 1: if dp[i - 1][1] == True and dp[i - 1][0] == False: ans += 1 else: dp[i][1] = True elif a[i] == 2: if dp[i - 1][0] == True and dp[i - 1][1] == False: ans += 1 else: dp[i][0] = True elif a[i] == 3: if dp[i - 1][0]: dp[i][1] = True if dp[i - 1][1]: dp[i][0] = True if not dp[i - 1][0] and not dp[i - 1][1]: dp[i][1], dp[i][0] = True, True else: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = [int(x) for x in input().split()] dp = [0] * (n + 1) if a[0] > 0: dp[0] = 1 sport = a[0] for x in range(1, n): dp[x] = dp[x - 1] if a[x] == 1 and sport != 1: dp[x] += 1 sport = 1 elif a[x] == 2 and sport != 2: dp[x] += 1 sport = 2 elif a[x] == 3: dp[x] += 1 if sport == 1: sport = 2 elif sport == 2: sport = 1 else: sport = 3 else: sport = 0 print(n - dp[n - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
import sys input = sys.stdin.readline read_tuple = lambda _type: map(_type, input().split(" ")) def solve(): n = int(input()) a = list(read_tuple(int)) dp = [[(0) for _ in range(n)] for i in range(3)] if a[0] == 1: dp[1][0] = 1 elif a[0] == 2: dp[2][0] = 1 elif a[0] == 3: dp[1][0] = 1 dp[2][0] = 1 for i in range(1, n): dp[0][i] = max((dp[0][i - 1], dp[1][i - 1], dp[2][i - 1])) if a[i] in (1, 3): dp[1][i] = max((dp[0][i - 1] + 1, dp[2][i - 1] + 1)) if a[i] in (2, 3): dp[2][i] = max((dp[0][i - 1] + 1, dp[1][i - 1] + 1)) print(n - max((dp[0][-1], dp[1][-1], dp[2][-1]))) solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = [int(x) for x in input().split()] d0, d1, d2 = [0], [0], [0] for i in range(n): d0.append(max((d0[i], d1[i], d2[i]))) d1.append(max(d0[i], d2[i]) + (1 if a[i] & 1 else 0)) d2.append(max(d0[i], d1[i]) + (1 if a[i] & 2 else 0)) print(n - max((d0[-1], d1[-1], d2[-1])))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST NUMBER LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
from sys import stdin input = stdin.readline n = int(input()) a = list(map(int, input().split())) dp = [0] * n dp[0] = a[0] for i in range(1, n): if a[i] == 0: dp[i] = 0 if a[i] == 1: if dp[i - 1] == 1: dp[i] = 0 if dp[i - 1] != 1: dp[i] = 1 if a[i] == 2: if dp[i - 1] == 2: dp[i] = 0 if dp[i - 1] != 2: dp[i] = 2 if a[i] == 3: if dp[i - 1] == 1: dp[i] = 2 if dp[i - 1] == 2: dp[i] = 1 if dp[i - 1] == 3 or dp[i - 1] == 0: dp[i] = 3 print(dp.count(0))
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) l = list(map(int, input().split())) dp = [[0, 0, 0] for i in range(n + 1)] for i in range(1, n + 1): dp[i][0] = max(dp[i - 1]) if l[i - 1] == 3: dp[i][1] = max(dp[i - 1][0] + 1, dp[i - 1][2] + 1) dp[i][2] = max(dp[i - 1][0] + 1, dp[i - 1][1] + 1) elif l[i - 1] == 2: dp[i][2] = max(dp[i - 1][0] + 1, dp[i - 1][1] + 1) elif l[i - 1] == 1: dp[i][1] = max(dp[i - 1][0] + 1, dp[i - 1][2] + 1) print(n - max(dp[n]))
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 NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) arr = list(map(int, input().split())) d = [(0) for i in range(n)] for i in range(n): if arr[i] == 0: d[i] += 1 if i != 0: if arr[i] == arr[i - 1] and arr[i] != 3 and arr[i] != 0: d[i] += 1 arr[i] = 0 elif arr[i] == 3 and arr[i - 1] == 1: arr[i] = 2 elif arr[i] == 3 and arr[i - 1] == 2: arr[i] = 1 d[i] += d[i - 1] print(d[n - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
def vacation(day_sequence): count = 0 previous = 0 for day in day_sequence: if day == 0: count += 1 previous = 0 elif day == 3: if previous == 1: previous = 2 elif previous == 2: previous = 1 elif day == 1: if previous == 1: count += 1 previous = 0 else: previous = 1 elif day == 2: if previous == 2: count += 1 previous = 0 else: previous = 2 return count n = int(input()) day_string = input() string_list = day_string.split() day_sequence = [] for day in string_list: day_sequence.append(int(day)) print(vacation(day_sequence))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
import sys sys.setrecursionlimit(100000) n = int(input()) a = list(map(lambda x: int(x), input().split())) def calculate(last_action, a, i, dp): if (last_action, i) in dp: return dp[last_action, i] if i == len(a) - 1: if last_action == 1 and a[i] == 2: return 1 if last_action == 1 and a[i] == 3: return 0 if last_action == 1 and a[i] == 0: return 1 if last_action == 1 and a[i] == 1: return 0 if last_action == 0 and a[i] == 0: return 1 if last_action == 0 and a[i] in [1, 2, 3]: return 0 if last_action == 2 and a[i] == 2: return 0 if last_action == 2 and a[i] == 3: return 0 if last_action == 2 and a[i] == 0: return 1 if last_action == 2 and a[i] == 1: return 1 available_actions = [] if last_action == 0 and a[i] in [1, 2, 3]: if a[i] == 1: available_actions = [0, 2] if a[i] == 2: available_actions = [0, 1] if a[i] == 3: available_actions = [0, 1, 2] elif last_action == 0 and a[i] not in [1, 2, 3]: available_actions = [0] elif last_action == 1 and a[i] in [1, 3]: available_actions = [0, 2] elif last_action == 1 and a[i] in [0, 2]: available_actions = [0] elif last_action == 2 and a[i] in [1, 0]: available_actions = [0] elif last_action == 2 and a[i] in [3, 2]: available_actions = [0, 1] minimum_days = 1000 for action in available_actions: if action == 0: if (0, i + 1) in dp: answer = 1 + dp[0, i + 1] else: dp[0, i + 1] = calculate(0, a, i + 1, dp) answer = 1 + dp[0, i + 1] if answer < minimum_days: minimum_days = answer else: if (action, i + 1) in dp: answer = 0 + dp[action, i + 1] else: dp[action, i + 1] = calculate(action, a, i + 1, dp) answer = 0 + dp[action, i + 1] if answer < minimum_days: minimum_days = answer dp[last_action, i] = minimum_days return minimum_days print(calculate(0, a, 0, {}))
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR LIST NUMBER NUMBER NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST IF VAR NUMBER VAR VAR LIST NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER IF VAR NUMBER VAR VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER VAR VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER IF VAR NUMBER VAR VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER IF VAR NUMBER VAR VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER DICT
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = [int(x) for x in input().split()] v = {} GYM = "GYM" CONT = "CONT" OFF = "OFF" options = [GYM, CONT, OFF] first = a[0] if first == 0: v[0, OFF] = 1 elif first == 1: v[0, CONT] = 0 v[0, OFF] = 1 elif first == 2: v[0, GYM] = 0 v[0, OFF] = 1 else: v[0, CONT] = 0 v[0, GYM] = 0 v[0, OFF] = 1 valid = {OFF: [GYM, CONT, OFF], GYM: [CONT, OFF], CONT: [GYM, OFF]} ok = {(0): [OFF], (1): [OFF, CONT], (2): [OFF, GYM], (3): [OFF, CONT, GYM]} for idx in range(n): cur = a[idx] for c in options: debug = idx == 1 and c == CONT if c not in ok[cur]: continue cmin = None for prev in valid[c]: prev_item = idx - 1, prev if prev_item not in v: continue val = v[prev_item] if c == OFF: cand = val + 1 else: cand = val if cmin is None or cand < cmin: cmin = cand if cmin is not None: v[idx, c] = cmin fmin = None for c in options: if (n - 1, c) not in v: continue val = v[n - 1, c] if fmin is None or val < fmin: fmin = val print(fmin)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR DICT VAR VAR VAR LIST VAR VAR VAR LIST VAR VAR LIST VAR VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER LIST VAR LIST VAR VAR LIST VAR VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR NONE FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR NONE FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NONE VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) l = [int(x) for x in input().split()] a = [[(0) for i in range(3)] for j in range(n)] inf = 10**9 a[0][0] = 1 if l[0] == 1 or l[0] == 3: a[0][1] = 0 else: a[0][1] = inf if l[0] == 2 or l[0] == 3: a[0][2] = 0 else: a[0][2] = inf for i in range(1, n): a[i][0] = min(a[i - 1]) + 1 if l[i] == 1 or l[i] == 3: a[i][1] = min(a[i - 1][0], a[i - 1][2]) else: a[i][1] = inf if l[i] == 2 or l[i] == 3: a[i][2] = min(a[i - 1][:-1]) else: a[i][2] = inf print(min(a[n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) arr = list(map(int, input().split())) ans = [[(0) for i in range(3)] for j in range(n + 1)] for i in range(1, n + 1): ans[i][0] = max(ans[i - 1]) if arr[i - 1] == 1 or arr[i - 1] == 3: ans[i][1] = max(ans[i - 1][0] + 1, ans[i - 1][2] + 1) if arr[i - 1] == 2 or arr[i - 1] == 3: ans[i][2] = max(ans[i - 1][0] + 1, ans[i - 1][1] + 1) maximum = 0 for i in ans: x = max(i) if x > maximum: maximum = x print(n - maximum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input().strip()) a = list(map(int, input().split())) d = [[(0) for _ in range(3)] for _ in range(n)] d[0][0] = 1 d[0][1] = 0 if a[0] in [1, 3] else 1 d[0][2] = 0 if a[0] in [2, 3] else 1 for i in range(1, n): d[i][0] = min(d[i - 1]) + 1 if a[i] in [1, 3]: d[i][1] = min(d[i - 1][0], d[i - 1][2]) else: d[i][1] = 1000 if a[i] in [2, 3]: d[i][2] = min(d[i - 1][0], d[i - 1][1]) else: d[i][2] = 1000 print(min(d[n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) acts = list(map(int, input().split())) memo = [[(-1) for j in range(n + 1)] for i in range(3)] def solve(prev, day): if day == n: return 0 if memo[prev][day] != -1: return memo[prev][day] ans = n if prev != 1 and (acts[day] == 1 or acts[day] == 3): ans = min(ans, solve(1, day + 1)) if prev != 2 and (acts[day] == 2 or acts[day] == 3): ans = min(ans, solve(2, day + 1)) ans = min(ans, 1 + solve(0, day + 1)) memo[prev][day] = ans return ans print(solve(0, 0))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
from sys import stdin, stdout n = int(stdin.readline()) a = list( map( lambda x: ( (False, False) if x == "0" else ( (False, True) if x == "1" else (True, False) if x == "2" else (True, True) ) ), stdin.readline().rstrip().split(" "), ) ) dp = [[100, 100, 100] for _ in range(n)] dp[0][0] = 1 if a[0][0]: dp[0][1] = 0 if a[0][1]: dp[0][2] = 0 for i in range(1, n): if a[i][0]: dp[i][1] = min(dp[i - 1][2], dp[i - 1][0]) if a[i][1]: dp[i][2] = min(dp[i - 1][1], dp[i - 1][0]) dp[i][0] = min(dp[i - 1][1], min(dp[i - 1][2], dp[i - 1][0])) + 1 stdout.write("{}".format(min(dp[n - 1][1], min(dp[n - 1][2], dp[n - 1][0]))))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING NUMBER NUMBER VAR STRING NUMBER NUMBER VAR STRING NUMBER NUMBER NUMBER NUMBER FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) ans = [0] + list(map(int, input().split())) lis = ["@"] * (n + 1) i = 1 while i < n + 1: if ans[i] == 0: lis[i] = "r" elif ans[i] == 1: if lis[i - 1] == "c": lis[i] = "r" else: lis[i] = "c" elif ans[i] == 2: if lis[i - 1] == "g": lis[i] = "r" else: lis[i] = "g" elif lis[i - 1] == "g": lis[i] = "c" elif lis[i - 1] == "c": lis[i] = "g" else: while i + 1 <= n and ans[i + 1] == 3: i += 1 if i <= n and ans[i] == 2: lis[i] = "g" elif i <= n and ans[i] == 1: lis[i] = "c" elif i <= n and ans[i] == 0: lis[i] = "r" i += 1 aa = 0 for i in lis[1:]: if i == "r": aa += 1 print(aa)
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 STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
def prov(i): if a[i + 1] == 3: if a[i + 2] == 1: return 1 elif a[i + 2] == 2: return 2 else: return 1 if a[i + 1] == 1: return 2 elif a[i + 1] == 2: return 1 n = int(input()) a = [int(i) for i in input().split()] if a[0] == 3 and len(a) > 2: if a[1] == 3: if a[2] == 1: a[0] = 1 elif a[2] == 2: a[0] = 2 for i in range(1, len(a)): if a[i] == 3: if a[i - 1] == 1: a[i] = 2 elif a[i - 1] == 2: a[i] = 1 if a[i] == a[i - 1] and a[i] != 3: a[i] = 0 print(a.count(0))
FUNC_DEF IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) num = list(map(int, input().split())) ans = 100 t = 0 dp = [0] * n dp[0] = num[0] if dp[0] == 0: t = 1 for i in range(1, n): if num[i] == 0: dp[i] = 0 t += 1 if num[i] == 1: if dp[i - 1] == 1: dp[i] = 0 t += 1 else: dp[i] = 1 if num[i] == 2: if dp[i - 1] == 2: dp[i] = 0 t += 1 else: dp[i] = 2 if num[i] == 3: if dp[i - 1] == 2: dp[i] = 1 elif dp[i - 1] == 1: dp[i] = 2 else: dp[i] = 0 print(t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) As = [int(x) for x in input().split()] cost_con, cost_gym, cost_rest = 0, 0, 0 def my_min(*args): l = [] for c in args: if c is not None: l += [c] return min(l) for x in As: if x == 0: cost_con, cost_gym, cost_rest = ( None, None, my_min(cost_rest, cost_con, cost_gym) + 1, ) elif x == 1: cost_con, cost_gym, cost_rest = ( my_min(cost_rest, cost_gym), None, my_min(cost_rest, cost_con, cost_gym) + 1, ) elif x == 2: cost_con, cost_gym, cost_rest = ( None, my_min(cost_rest, cost_con), my_min(cost_rest, cost_con, cost_gym) + 1, ) elif x == 3: cost_con, cost_gym, cost_rest = ( my_min(cost_gym, cost_rest), my_min(cost_con, cost_rest), my_min(cost_rest, cost_con, cost_gym) + 1, ) print(my_min(cost_rest, cost_con, cost_gym))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR NONE VAR LIST VAR RETURN FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NONE NONE BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NONE BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NONE FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
length = int(input()) days = list(map(int, input().split())) contest = False gym = False rest = 0 for i in range(length): if days[i] == 0: rest += 1 contest = False gym = False elif days[i] == 1: if contest: rest += 1 contest = False else: contest = True gym = False elif days[i] == 2: if gym: rest += 1 gym = False else: gym = True contest = False elif days[i] == 3: if contest: if gym: rest += 1 gym = False else: gym = True contest = False elif gym: if contest: rest += 1 contest = False else: contest = True gym = False print(rest)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = list(map(int, input().split())) minr = ming = minc = 0 for i in a: if i == 0: minr = min(minr, ming, minc) + 1 ming = n minc = n elif i == 1: minc, minr, ming = min(minr, ming), min(minr, ming, minc) + 1, n elif i == 2: ming, minr, minc = min(minr, minc), min(minr, ming, minc) + 1, n else: minc, ming, minr = min(minr, ming), min(minr, minc), min(minr, ming, minc) + 1 print(min(minr, ming, minc))
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 NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) x = [int(x) for x in input().split()] x.append(-1) rest = 0 if x[0] == 0: rest = rest + 1 for i in range(1, n): if x[i] == 0: rest = rest + 1 elif x[i] == 1 and x[i - 1] == 1: rest = rest + 1 x[i] = 0 elif x[i] == 2 and x[i - 1] == 2: rest = rest + 1 x[i] = 0 elif x[i] == 3: if x[i - 1] == 1: x[i] = 2 elif x[i - 1] == 2: x[i] = 1 elif x[i - 1] == 0 and x[i + 1] == 2: x[i] == 1 elif x[i - 1] == 0 and x[i + 1] == 1: x[i] == 2 print(rest)
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 ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = [int(x) for x in input().split()] dp = [[(-1) for _ in range(n)] for _ in range(3)] def solve(prev, i): if i == n: return 0 if dp[prev][i] != -1: return dp[prev][i] if a[i] == 0: dp[prev][i] = 1 + solve(0, i + 1) elif a[i] == 1: if prev == 1: dp[prev][i] = 1 + solve(0, i + 1) else: dp[prev][i] = solve(1, i + 1) elif a[i] == 2: if prev == 2: dp[prev][i] = 1 + solve(0, i + 1) else: dp[prev][i] = solve(2, i + 1) elif prev == 1: dp[prev][i] = solve(2, i + 1) elif prev == 2: dp[prev][i] = solve(1, i + 1) else: dp[prev][i] = min(solve(2, i + 1), solve(1, i + 1)) return dp[prev][i] print(solve(0, 0))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = [int(x) for x in input().split()] i = 0 j = 1 ans = 0 while i < n and j < n: c = 0 if a[i] == 1 and a[j] == 1 or a[i] == 2 and a[j] == 2: ans += 1 i += 2 j += 2 elif a[i] == 1 and a[j] == 3: while j < n and a[j] == 3: c += 1 j += 1 if j < n: if c % 2 == 0 and a[j] == 1: ans += 1 i = j + 1 j = i + 1 elif c % 2 == 1 and a[j] == 2: ans += 1 i = j + 1 j = i + 1 else: i = j j = i + 1 elif a[i] == 2 and a[j] == 3: while j < n and a[j] == 3: c += 1 j += 1 if j < n: if c % 2 == 0 and a[j] == 2: ans += 1 i = j + 1 j = i + 1 elif c % 2 == 1 and a[j] == 1: ans += 1 i = j + 1 j = i + 1 else: i = j j = i + 1 else: i += 1 j += 1 print(ans + a.count(0))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) list1 = [int(i) for i in input().split()] dp = [] for x in range(n): dp.append([10**9, 10**9, 10**9]) dp[0][0] = 1 if list1[0] == 0: dp[0][1] = 1 dp[0][2] = 1 elif list1[0] == 1: dp[0][1] = 0 dp[0][2] = 1 elif list1[0] == 2: dp[0][1] = 1 dp[0][2] = 0 else: dp[0][1] = 0 dp[0][2] = 0 for x in range(1, n): dp[x][0] = 1 + min([dp[x - 1][0], dp[x - 1][1], dp[x - 1][2]]) if list1[x] == 0: dp[x][1] = 1 + min([dp[x - 1][0], dp[x - 1][1], dp[x - 1][2]]) dp[x][2] = 1 + min([dp[x - 1][0], dp[x - 1][1], dp[x - 1][2]]) elif list1[x] == 1: dp[x][1] = min([dp[x - 1][0], dp[x - 1][2]]) dp[x][2] = 1 + min([dp[x - 1][0], dp[x - 1][1], dp[x - 1][2]]) elif list1[x] == 2: dp[x][1] = 1 + min([dp[x - 1][0], dp[x - 1][1], dp[x - 1][2]]) dp[x][2] = min([dp[x - 1][0], dp[x - 1][1]]) else: dp[x][1] = min([dp[x - 1][0], dp[x - 1][2]]) dp[x][2] = min([dp[x - 1][0], dp[x - 1][1]]) print(min([dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: 1. on this day the gym is closed and the contest is not carried out; 2. on this day the gym is closed and the contest is carried out; 3. on this day the gym is open and the contest is not carried out; 4. on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day). Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days. Input The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations. The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where: * ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out; * ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out; * ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out; * ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out. Output Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses: * to do sport on any two consecutive days, * to write the contest on any two consecutive days. Examples Input 4 1 3 2 0 Output 2 Input 7 1 3 3 2 1 2 3 Output 0 Input 2 2 2 Output 1 Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day. In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
n = int(input()) a = [int(i) for i in input().split()] dp = [[0] * (n + 1), [0] * (n + 1), [0] * (n + 1)] INF = 1000000000.0 for i in range(1, n + 1): dp[0][i] = min(dp[0][i - 1], dp[1][i - 1], dp[2][i - 1]) + 1 dp[1][i] = INF dp[2][i] = INF if a[i - 1] in [1, 3]: dp[1][i] = min(dp[0][i - 1], dp[2][i - 1]) if a[i - 1] in [2, 3]: dp[2][i] = min(dp[0][i - 1], dp[1][i - 1]) print(min(dp[0][n], dp[1][n], dp[2][n]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR
Chef's loves his dog so much! Once his dog created two strings a and b each of length n consisting of digits 1 and 2, and even a problem about them! Chef's Dog will tell by barking if a string x (also containing only digits 1 and 2 and with length N) is good or not by performing the following actions. - It starts at first digit of the string, i.e. at i = 1. - It can move from digit i to either i - 1 or i + 1 if xi equals 1 and the corresponding digits exist. - It can move from digit i to either i - 2 or i + 2 if xi equals 2 and the corresponding digits exist. - It must visit each digit exactly once. - It must finish at the last digit (XN). Chef's dog wants to make both the strings a and b good by choosing some subset S (possibly empty) of indices of set {1, 2, ..., n} and swapping each index i ϵ S between string a and b, i.e. swapping ai and bi. Can you find how many such subsets S exist out there? As the answer could be large, output it modulo 109 + 7. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line contains string a. The second line contains string b. -----Output----- For each test case, output a single line containing answer of the problem. -----Constraints----- - 1 ≤ T ≤ 20 - 1 ≤ |a| = |b| ≤ 105 - '1' ≤ ai, bi ≤ '2' -----Subtasks----- - Subtask #1 (30 points) |a|, |b| ≤ 10 - Subtask #2 (70 points) original constraints -----Example----- Input: 2 1111 2211 222 111 Output: 8 0 -----Explanation----- Test case 1. Possible subsets are: {}, {1, 2}, {1, 2, 3}, {1, 2, 4}, {1, 2, 3, 4}, {3}, {4}, {3, 4}. Test case 2. There are no possible sets S which can make both the strings good.
MOD = 1000000007 for i in range(int(input())): a = input() b = input() n = len(a) if n == 1: print(2) elif a[n - 2] == "2" or b[n - 2] == "2": print(0) else: fr, sc = 0, 0 ans = 1 for j in range(n - 1): c = int(a[j]) + int(b[j]) if c == 2: if fr % 2 == 0 and sc % 2 == 0: ans = ans * 2 % MOD fr, sc = 0, 0 else: ans = 0 elif c == 3: if fr == 0 and sc == 0: fr = 1 ans = ans * 2 % MOD elif fr == 1 and sc == 0: fr = 2 elif fr == 2 and sc == 0: sc = 1 fr = 0 elif fr == 0 and sc == 1: sc = 2 elif fr == 0 and sc == 2: fr = 1 sc = 0 elif fr == 1 and sc == 1: ans = 0 elif fr == 2 and sc == 1: fr = 0 sc = 2 elif fr == 1 and sc == 2: fr = 2 sc = 0 elif fr == 2 and sc == 2: ans = 0 elif c == 4: if fr == 0 and sc == 0: fr = 1 sc = 1 ans = ans * 2 % MOD elif fr == 1 and sc == 0: fr = 2 sc = 1 ans = ans * 2 % MOD elif fr == 2 and sc == 0: ans = 0 elif fr == 0 and sc == 1: sc = 2 fr = 1 ans = ans * 2 % MOD elif fr == 0 and sc == 2: ans = 0 elif fr == 1 and sc == 1: fr = 2 sc = 2 ans = ans * 2 % MOD elif fr == 2 and sc == 1: ans = 0 elif fr == 1 and sc == 2: ans = 0 elif fr == 2 and sc == 2: ans = 0 if ans == 0: break ans = ans * 2 % MOD print(ans)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Chef's loves his dog so much! Once his dog created two strings a and b each of length n consisting of digits 1 and 2, and even a problem about them! Chef's Dog will tell by barking if a string x (also containing only digits 1 and 2 and with length N) is good or not by performing the following actions. - It starts at first digit of the string, i.e. at i = 1. - It can move from digit i to either i - 1 or i + 1 if xi equals 1 and the corresponding digits exist. - It can move from digit i to either i - 2 or i + 2 if xi equals 2 and the corresponding digits exist. - It must visit each digit exactly once. - It must finish at the last digit (XN). Chef's dog wants to make both the strings a and b good by choosing some subset S (possibly empty) of indices of set {1, 2, ..., n} and swapping each index i ϵ S between string a and b, i.e. swapping ai and bi. Can you find how many such subsets S exist out there? As the answer could be large, output it modulo 109 + 7. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line contains string a. The second line contains string b. -----Output----- For each test case, output a single line containing answer of the problem. -----Constraints----- - 1 ≤ T ≤ 20 - 1 ≤ |a| = |b| ≤ 105 - '1' ≤ ai, bi ≤ '2' -----Subtasks----- - Subtask #1 (30 points) |a|, |b| ≤ 10 - Subtask #2 (70 points) original constraints -----Example----- Input: 2 1111 2211 222 111 Output: 8 0 -----Explanation----- Test case 1. Possible subsets are: {}, {1, 2}, {1, 2, 3}, {1, 2, 4}, {1, 2, 3, 4}, {3}, {4}, {3, 4}. Test case 2. There are no possible sets S which can make both the strings good.
modulo = 1000000007 def pow(a, n): res = 1 while n > 0: if n & 1: res *= a res %= modulo a *= a a %= modulo n //= 2 return res def test2(s): if "4" in s: return True if len(s) & 1 > 0: return True return False def test(s): sss = s.split("343") return any(map(test2, sss)) T = int(input()) for t in range(T): s1 = input() s2 = input() ss = "".join(map(lambda x, y: str(int(x) + int(y)), s1, s2)) if len(ss) > 0: ss = ss[:-1] + "2" if "444" in ss or "344" in ss or "443" in ss or len(ss) > 1 and ss[-2] != "2": print(0) else: twos = list(filter(lambda x: x and x != "44", ss.split("2"))) if any(map(test, twos)): print(0) else: ones = sum(map(lambda x: x != "3", ss)) print(pow(2, ones + len(twos)))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF STRING VAR RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING IF STRING VAR STRING VAR STRING VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR STRING IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR
Chef's loves his dog so much! Once his dog created two strings a and b each of length n consisting of digits 1 and 2, and even a problem about them! Chef's Dog will tell by barking if a string x (also containing only digits 1 and 2 and with length N) is good or not by performing the following actions. - It starts at first digit of the string, i.e. at i = 1. - It can move from digit i to either i - 1 or i + 1 if xi equals 1 and the corresponding digits exist. - It can move from digit i to either i - 2 or i + 2 if xi equals 2 and the corresponding digits exist. - It must visit each digit exactly once. - It must finish at the last digit (XN). Chef's dog wants to make both the strings a and b good by choosing some subset S (possibly empty) of indices of set {1, 2, ..., n} and swapping each index i ϵ S between string a and b, i.e. swapping ai and bi. Can you find how many such subsets S exist out there? As the answer could be large, output it modulo 109 + 7. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line contains string a. The second line contains string b. -----Output----- For each test case, output a single line containing answer of the problem. -----Constraints----- - 1 ≤ T ≤ 20 - 1 ≤ |a| = |b| ≤ 105 - '1' ≤ ai, bi ≤ '2' -----Subtasks----- - Subtask #1 (30 points) |a|, |b| ≤ 10 - Subtask #2 (70 points) original constraints -----Example----- Input: 2 1111 2211 222 111 Output: 8 0 -----Explanation----- Test case 1. Possible subsets are: {}, {1, 2}, {1, 2, 3}, {1, 2, 4}, {1, 2, 3, 4}, {3}, {4}, {3, 4}. Test case 2. There are no possible sets S which can make both the strings good.
T = int(input()) for t in range(T): S1 = list(input()) S2 = list(input()) N = len(S1) MOD = 1000000007 count = 1 for n in range(N - 1): if S2[n] == "2": S1[n], S2[n] = S2[n], S1[n] if S1[n] == S2[n]: count = count * 2 % MOD count = count * 2 % MOD l22 = list("22") l11 = list("11") l221 = list("221") l222 = list("222") l121 = list("121") n = 0 expected = [1, 2, 3, 4] got = 0 while n < N - 2: inc = 1 if 1 in expected and S1[n : n + 2] == l22 and S2[n : n + 2] == l11: expected = [1, 2, 4] inc = 2 elif 2 in expected and S1[n] == "1" and S2[n] == "1": if len(expected) < 4: count = count * 2 % MOD expected = [1, 2, 3, 4] inc = 1 elif 3 in expected and S1[n : n + 3] == l221 and S2[n : n + 3] == l221: expected = [1, 2, 3, 4] inc = 3 elif 4 in expected and S1[n : n + 3] == l222 and S2[n : n + 3] == l121: expected = [1, 2, 4] inc = 3 else: count = 0 break n += inc if N > 1 and not (S1[N - 2] == "1" and S2[N - 2] == "1"): count = 0 elif len(expected) < 4: count = count * 2 % MOD print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF NUMBER VAR VAR VAR STRING VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Chef's loves his dog so much! Once his dog created two strings a and b each of length n consisting of digits 1 and 2, and even a problem about them! Chef's Dog will tell by barking if a string x (also containing only digits 1 and 2 and with length N) is good or not by performing the following actions. - It starts at first digit of the string, i.e. at i = 1. - It can move from digit i to either i - 1 or i + 1 if xi equals 1 and the corresponding digits exist. - It can move from digit i to either i - 2 or i + 2 if xi equals 2 and the corresponding digits exist. - It must visit each digit exactly once. - It must finish at the last digit (XN). Chef's dog wants to make both the strings a and b good by choosing some subset S (possibly empty) of indices of set {1, 2, ..., n} and swapping each index i ϵ S between string a and b, i.e. swapping ai and bi. Can you find how many such subsets S exist out there? As the answer could be large, output it modulo 109 + 7. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line contains string a. The second line contains string b. -----Output----- For each test case, output a single line containing answer of the problem. -----Constraints----- - 1 ≤ T ≤ 20 - 1 ≤ |a| = |b| ≤ 105 - '1' ≤ ai, bi ≤ '2' -----Subtasks----- - Subtask #1 (30 points) |a|, |b| ≤ 10 - Subtask #2 (70 points) original constraints -----Example----- Input: 2 1111 2211 222 111 Output: 8 0 -----Explanation----- Test case 1. Possible subsets are: {}, {1, 2}, {1, 2, 3}, {1, 2, 4}, {1, 2, 3, 4}, {3}, {4}, {3, 4}. Test case 2. There are no possible sets S which can make both the strings good.
size = 100000 modval = 1000000007 def main(): t = int(input()) for _ in range(t): a = input() b = input() l = len(a) count = 0 if l == 1: count = 2 elif l < 4: if checkGood(a, b, l) == 1: if l == 2: count = 4 else: count = 8 elif a[l - 2] == "2" or b[l - 2] == "2": count = 0 elif parityCheck(a, b, l) == 0 and columnCheck(a, b, l) == 1: i = 0 check = 1 count = 1 while i < l - 2: while i < l - 2 and a[i] == "1" and b[i] == "1": i += 1 check = 1 if a[i] != b[i]: if i < 2 or countTwo(a, b, i - 1) == 0: count *= 2 if count >= modval: count = count % modval t1 = countTwo(a, b, i + 1) t2 = countTwo(a, b, i + 2) if t1 == 0: check = 0 elif t1 == 1: i += 2 elif t2 == 1: i += 3 else: check = 0 elif i < l - 2 and a[i] == "2" and b[i] == "2": if ( i < l - 3 and countTwo(a, b, i + 1) == 2 and countTwo(a, b, i + 2) == 0 ): i += 3 else: check = 0 if check == 0: count = 0 break if check == 1: count *= 2 if count >= modval: count = count % modval for i in range(l - 1): if a[i] == b[i]: count *= 2 if count >= modval: count = count % modval print(count) def checkGood(a, b, l): for i in range(l - 1): if a[i] == "2" or b[i] == "2": return 0 return 1 def parityCheck(a, b, l): count = 0 for i in range(l - 2): if a[i] != b[i]: count += 1 return count % 2 def columnCheck(a, b, l): if countTwo(a, b, 0) > countTwo(a, b, 1): return 0 for i in range(l - 2): if countTwo(a, b, i) > countTwo(a, b, i - 1) + countTwo(a, b, i + 1): return 0 return 1 def countTwo(a, b, i): count = 0 if a[i] == "2": count += 1 if b[i] == "2": count += 1 return count main()
ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR STRING VAR VAR STRING IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR
Chef's loves his dog so much! Once his dog created two strings a and b each of length n consisting of digits 1 and 2, and even a problem about them! Chef's Dog will tell by barking if a string x (also containing only digits 1 and 2 and with length N) is good or not by performing the following actions. - It starts at first digit of the string, i.e. at i = 1. - It can move from digit i to either i - 1 or i + 1 if xi equals 1 and the corresponding digits exist. - It can move from digit i to either i - 2 or i + 2 if xi equals 2 and the corresponding digits exist. - It must visit each digit exactly once. - It must finish at the last digit (XN). Chef's dog wants to make both the strings a and b good by choosing some subset S (possibly empty) of indices of set {1, 2, ..., n} and swapping each index i ϵ S between string a and b, i.e. swapping ai and bi. Can you find how many such subsets S exist out there? As the answer could be large, output it modulo 109 + 7. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line contains string a. The second line contains string b. -----Output----- For each test case, output a single line containing answer of the problem. -----Constraints----- - 1 ≤ T ≤ 20 - 1 ≤ |a| = |b| ≤ 105 - '1' ≤ ai, bi ≤ '2' -----Subtasks----- - Subtask #1 (30 points) |a|, |b| ≤ 10 - Subtask #2 (70 points) original constraints -----Example----- Input: 2 1111 2211 222 111 Output: 8 0 -----Explanation----- Test case 1. Possible subsets are: {}, {1, 2}, {1, 2, 3}, {1, 2, 4}, {1, 2, 3, 4}, {3}, {4}, {3, 4}. Test case 2. There are no possible sets S which can make both the strings good.
def fun(a): n = len(a) i = 0 b = [] while i < n: c = 0 while i < n - 1 and a[i] == a[i + 1]: c += 1 i += 1 b.append([a[i], c + 1]) i += 1 return b def tatti(h): n = len(h) i = 0 b = [] while i < n: c = 0 while i < n - 1 and h[i][0] == h[i + 1][0]: c += 1 i += 1 b.append([h[i][0], h[i][1], c + 1]) i += 1 return b def mod(a, b, m): x = 1 y = a while b > 0: if b % 2: x = x * y % m y = y * y % m b //= 2 return x % m def rem(s): s = list(s) l = len(s) i = l - 1 ans = [] c = 0 while i >= 0: if s[i] == 1 and s[i - 1] == 2 and s[i - 2] == 1 and i >= 2: i -= 3 c += 1 else: i -= 1 ans.append(c) c = 0 ans.append(c) count = 0 flag = 0 for i in range(len(ans)): if ans[i] != 0: count += 1 flag = max(flag, ans[i]) return count, flag for _ in range(int(input())): md = 10**9 + 7 s1 = input() s2 = input() x = [] l = len(s1) for i in range(l): if s1[i] == "2" and s2[i] == "2": x.append(2) elif s1[i] == "1" and s2[i] == "1": x.append(0) else: x.append(1) if l == 1: ans = 2 elif l == 2: if s1[0] == "2" or s2[0] == "2": ans = 0 else: ans = 4 else: x.pop() x.pop() h = fun(x) for i in range(1, len(h) - 1): if h[i][0] == 2 and h[i][1] == 1: if h[i - 1][0] == 1 and h[i + 1][0] == 1: h[i - 1][1] -= 1 h[i + 1][1] -= 1 h[i][0] = -1 hn = [] for i in range(len(h)): if h[i][1] != 0: hn.append(h[i]) hnn = tatti(hn) flag = 0 ans = 1 for i in range(len(hnn)): if hnn[i][0] == 0: ans = ans * mod(2, hnn[i][1], md) % md elif hnn[i][0] == 1: if hnn[i][1] % 2 == 0: ans *= 2 ans = ans % md else: ans *= 0 elif hnn[i][0] == 2: if hnn[i][1] == 2: if ( i - 1 >= 0 and hnn[i - 1][0] != 0 or i + 1 < len(hnn) and hnn[i + 1][0] != 0 ): ans *= 0 else: ans *= 4 ans = ans % md else: ans *= 0 elif (i - 1 >= 0 and hnn[i - 1][0] == 1) and ( i + 1 <= len(hnn) - 1 and hnn[i + 1][0] == 1 ): if hnn[i][2] - 1 >= 0: ans = ans * mod(2, hnn[i][2] - 1, md) ans = ans % md elif ( i - 1 >= 0 and hnn[i - 1][0] == 1 or i + 1 <= len(hnn) - 1 and hnn[i + 1][0] == 1 ): ans = ans * mod(2, hnn[i][2], md) ans = ans % md else: ans = ans * mod(2, hnn[i][2] + 1, md) ans = ans % md if s1[l - 2] == "2" or s2[l - 2] == "2": ans *= 0 else: ans *= 4 ans = ans % md print(ans % md)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all. In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away. For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3. Input The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively. The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0. Output Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes. Examples Input 6 1 10.245 Output 10.25 Input 6 2 10.245 Output 10.3 Input 3 100 9.2 Output 9.2 Note In the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect. In the third sample the optimal strategy is to not perform any rounding at all.
def roundroot(): global root i = len(root) - 1 while i >= 0: root[i] += 1 if root[i] == 10 and i != 0: root[i] = 0 i -= 1 else: break def printA(a): for i in a: print(i, end="") n, t = [int(i) for i in input().split()] num = input() for i in range(len(num)): if num[i] == ".": break root = [int(j) for j in num[:i]] dec = [int(j) for j in num[i + 1 :]] n = len(dec) itrack = -1 for i in range(n): if dec[i] >= 5: itrack = i break if itrack == -1: itrack = n - 1 while itrack >= 1 and t > 0: if dec[itrack] < 5: break j = itrack - 1 t -= 1 while j >= 0: dec[j] += 1 if dec[j] == 10: dec[j] = 0 j -= 1 else: break if j == -1: roundroot() itrack = j if itrack >= 0 and t > 0 and dec[0] >= 5: roundroot() printA(root) else: printA(root) print(".", end="") for i in range(itrack + 1): print(dec[i], end="")
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all. In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away. For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3. Input The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively. The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0. Output Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes. Examples Input 6 1 10.245 Output 10.25 Input 6 2 10.245 Output 10.3 Input 3 100 9.2 Output 9.2 Note In the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect. In the third sample the optimal strategy is to not perform any rounding at all.
n, k = map(int, input().split()) s = list(input()) dot = s.index(".") pos = -1 for i in range(dot + 1, n): if s[i] > "4": pos = i break if pos < 0: print("".join(s)) else: for j in range(k): if s[pos - 1 - j] != "4": break pos = pos - 1 - j while pos >= 0 and (s[pos] == "9" or pos == dot): pos -= 1 if pos < 0: print("1", end="") print("0" * dot) elif pos < dot: print("".join(s[:pos]), end="") print(chr(ord(s[pos]) + 1), end="") print("0" * (dot - pos - 1)) else: print("".join(s[:pos]), end="") print(chr(ord(s[pos]) + 1))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER VAR VAR STRING VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER
Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all. In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away. For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3. Input The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively. The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0. Output Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes. Examples Input 6 1 10.245 Output 10.25 Input 6 2 10.245 Output 10.3 Input 3 100 9.2 Output 9.2 Note In the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect. In the third sample the optimal strategy is to not perform any rounding at all.
n, t = list(map(int, input().split())) s = list(input()) p = 0 index = n - 1 for i in range(0, n): if s[i] == ".": p = i if p == 0: continue if s[i] >= "5": index = i break while s[index] >= "5" and index > p and t > 0: if s[index - 1] == ".": index = index - 1 break index = index - 1 s[index] = str(int(s[index]) + 1) t = t - 1 flag = 0 if p == index: flag = 1 index = index - 1 while index >= 0 and flag != 0: if s[index] == "9": s[index] = "0" flag = 1 else: s[index] = str(int(s[index]) + 1) flag = 0 index = index - 1 index = p - 1 if flag != 0: print(1, end="") for i in range(0, index + 1): print(s[i], end="") print("")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR WHILE VAR VAR STRING VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all. In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away. For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3. Input The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively. The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0. Output Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes. Examples Input 6 1 10.245 Output 10.25 Input 6 2 10.245 Output 10.3 Input 3 100 9.2 Output 9.2 Note In the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect. In the third sample the optimal strategy is to not perform any rounding at all.
n, t = map(int, input().split()) tmp = input() s = [] for i in range(n): s.append(tmp[i]) ind = n perenos = 0 for i in range(n): if s[i] == ".": nach = i + 1 for i in range(nach, n): if int(s[i]) > 4: ind = i break if ind == n: print(*s, sep="") exit() while t > 0 and s[ind] != ".": if int(s[ind]) > 3: ind -= 1 perenos = 1 t -= 1 if s[ind] == "9": t += 1 else: s[ind] = str(int(s[ind]) + 1) perenos = 0 break if s[ind] == ".": ind -= 1 while s[ind] == "9": s[ind] = "0" ind -= 1 if ind < 0 and perenos != 0: print(1, end="") viv = 0 while s[viv] != ".": print(s[viv], end="") viv += 1 else: s[ind] = str(int(s[ind]) + perenos) viv = 0 while s[viv] != ".": print(s[viv], end="") viv += 1 else: while s[ind] == "9" or s[ind] == ".": if s[ind] == ".": ind -= 1 continue s[ind] = "0" ind -= 1 s[ind] = str(int(s[ind]) + perenos) for i in range(ind + 1): print(s[i], end="")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR WHILE VAR NUMBER VAR VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER WHILE VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER WHILE VAR VAR STRING VAR VAR STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all. In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away. For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3. Input The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively. The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0. Output Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes. Examples Input 6 1 10.245 Output 10.25 Input 6 2 10.245 Output 10.3 Input 3 100 9.2 Output 9.2 Note In the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect. In the third sample the optimal strategy is to not perform any rounding at all.
n, t = map(int, input().split(" ")) num = input() num = ["0"] + list(num) stack = [] dot = num.index(".") for i, x in enumerate(num): if i > dot and int(x) >= 5: stack.append(i) break ind = len(num) - 1 for i in range(t): if len(stack) == 0 or stack[0] < dot: break ind = stack[0] while True: ind -= 1 if num[ind] == ".": ind -= 1 tmp = int(num[ind]) + 1 if tmp < 10: num[ind] = str(tmp) break else: num[ind] = "0" stack.pop() if int(num[ind]) >= 5: stack.append(ind) num = num[: max(ind + 1, dot)] if num[0] == "0": num = num[1:] print("".join(num))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER WHILE NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all. In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away. For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3. Input The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively. The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0. Output Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes. Examples Input 6 1 10.245 Output 10.25 Input 6 2 10.245 Output 10.3 Input 3 100 9.2 Output 9.2 Note In the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect. In the third sample the optimal strategy is to not perform any rounding at all.
n, t = map(int, input().split()) mark = input().split(".") shift = False fraction = [] for digit in map(int, mark[1]): if digit > 4: t -= 1 if fraction: fraction[-1] += 1 while t and fraction and fraction[-1] > 4: t -= 1 fraction.pop() if fraction: fraction[-1] += 1 else: shift = True else: shift = True break fraction.append(digit) if shift: last_digits = [int(mark[0][-1]) + 1] i = -1 while last_digits[-1] > 9: last_digits[-1] -= 10 i -= 1 if -i <= len(mark[0]): last_digits.append(int(mark[0][i]) + 1) else: last_digits.append(1) print(mark[0][:i], *last_digits[::-1], sep="", end="") else: print(mark[0], end="") if fraction: print(".", *fraction, sep="")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER STRING STRING EXPR FUNC_CALL VAR VAR NUMBER STRING IF VAR EXPR FUNC_CALL VAR STRING VAR STRING
Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all. In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away. For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3. Input The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively. The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0. Output Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes. Examples Input 6 1 10.245 Output 10.25 Input 6 2 10.245 Output 10.3 Input 3 100 9.2 Output 9.2 Note In the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect. In the third sample the optimal strategy is to not perform any rounding at all.
n, t = map(int, input().split()) x = input() i = x.find(".") for j in range(i + 1, n): if x[j] > "4": for k in range(t): j -= 1 if x[j] != "4": break if j == i: j -= 1 while j and x[j] == "9": j -= 1 x = x[:j] + str(int(x[j]) + 1) + "0" * (i - j - 1) else: x = x[:j] + str(int(x[j]) + 1) break print(x)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING IF VAR VAR VAR NUMBER WHILE VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP STRING BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: edge_value = collections.defaultdict() graph = collections.defaultdict(list) for u, v in edges: graph[u].append(v) graph[v].append(u) def dfs1(u, parent): distance = 0 vertex_num = 1 for v in graph[u]: if v != parent: temp_dis, temp_ver = dfs1(v, u) distance += temp_dis vertex_num += temp_ver if parent != -1: edge_value[parent, u] = distance + vertex_num, vertex_num return distance + vertex_num, vertex_num ans = [0] * N def dfs2(u, parent): nonlocal N total_dis = 0 for v in graph[u]: temp_dis, temp_ver = edge_value[u, v] total_dis += temp_dis ans[u] = total_dis for v in graph[u]: if v != parent: temp_dis, temp_ver = edge_value[u, v] new_dis = total_dis - temp_dis + (N - temp_ver) edge_value[v, u] = new_dis, N - temp_ver dfs2(v, u) dfs1(0, -1) dfs2(0, -1) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER RETURN VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: graph = [[] for _ in range(N)] for i, j in edges: graph[i].append(j) graph[j].append(i) size, up, down = [1] * N, [0] * N, [0] * N def post(parent, i): for kid in graph[i]: if kid != parent: post(i, kid) size[i] += size[kid] up[i] += up[kid] + size[kid] def pre(parent, i): if parent != -1: down[i] = down[parent] + (up[parent] - up[i] - size[i]) + (N - size[i]) for kid in graph[i]: if kid != parent: pre(i, kid) post(-1, 0) pre(-1, 0) return [(up[i] + down[i]) for i in range(N)]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FUNC_DEF FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER RETURN BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: graph = [set() for _ in range(N)] for edge in edges: graph[edge[0]].add(edge[1]) graph[edge[1]].add(edge[0]) count = [1] * N ans = [0] * N def dfs(node, parent): for child in graph[node]: if child != parent: dfs(child, node) count[node] += count[child] ans[node] += ans[child] + count[child] def dfs2(node, parent): for child in graph[node]: if child != parent: ans[child] = ans[node] - count[child] + N - count[child] dfs2(child, node) dfs(0, None) dfs2(0, None) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NONE EXPR FUNC_CALL VAR NUMBER NONE RETURN VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: links = [[] for _ in range(N)] for a, b in edges: links[a].append(b) links[b].append(a) sums1 = [0] * N nums1 = [None] * N sums2 = [0] * N self.dfs1(None, 0, links, sums1, nums1) self.dfs2(None, 0, links, sums1, nums1, sums2, 0, 0) return [(a + b) for a, b in zip(sums1, sums2)] def dfs1(self, p: int, x: int, links, sums1, nums1): cur_sum, cur_num = 0, 0 for x2 in links[x]: if x2 != p: self.dfs1(x, x2, links, sums1, nums1) cur_sum += sums1[x2] cur_num += nums1[x2] sums1[x] = cur_sum + cur_num nums1[x] = cur_num + 1 def dfs2( self, p: int, x: int, links, sums1, nums1, sums2, up_num: int, up_sum: int ): cur_chs = [x2 for x2 in links[x] if x2 != p] cur_sums1 = [sums1[z] for z in cur_chs] cur_nums1 = [nums1[z] for z in cur_chs] all_sums1, all_nums1 = sum(cur_sums1), sum(cur_nums1) for x2 in cur_chs: tmp_up_num = up_num + 1 + (all_nums1 - nums1[x2]) tmp_up_sum = ( up_sum + (all_sums1 - sums1[x2]) + tmp_up_num + (tmp_up_num - (up_num + 1)) ) sums2[x2] = tmp_up_sum self.dfs2(x, x2, links, sums1, nums1, sums2, tmp_up_num, tmp_up_sum)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NONE NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NONE NUMBER VAR VAR VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: graph = collections.defaultdict(set) for u, v in edges: graph[u].add(v) graph[v].add(u) count = [1] * N ans = [0] * N print(graph) def postorder(node=0, parent=None): for child in graph[node]: if child != parent: postorder(child, node) count[node] += count[child] ans[node] += ans[child] + count[child] def preorder(node=0, parent=None): for child in graph[node]: if child != parent: ans[child] = ans[node] - count[child] + (N - count[child]) preorder(child, node) postorder() preorder() return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF NUMBER NONE FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF NUMBER NONE FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: tree = collections.defaultdict(set) res = [0] * N count = [1] * N for i, j in edges: tree[i].add(j) tree[j].add(i) def dfs(root, pre): for i in tree[root]: if i != pre: dfs(i, root) count[root] += count[i] res[root] += res[i] + count[i] def dfs2(root, pre): for i in tree[root]: if i != pre: res[i] = res[root] - count[i] + N - count[i] dfs2(i, root) dfs(0, -1) dfs2(0, -1) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER RETURN VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Node: def __init__(self, val): self.val = val self.children = [] def add_edge(self, child): self.children.append(child) child.parent = self def count(self): res_num = 0 res_val = 0 for c in self.children: temp_num, temp_val = c.count() res_num += temp_num res_val += temp_val self.res_num = res_num self.res_val = res_val return res_num + 1, res_val + (res_num + 1) def dist(self, res, p_num, p_val): res[self.val] = p_val + self.res_val for c in self.children: c.dist( res, p_num + self.res_num - c.res_num, p_val + self.res_val - c.res_val - c.res_num - 1 + (p_num + self.res_num - c.res_num), ) class STree: def __init__(self, n, edges): self.nodes = [Node(i) for i in range(n)] self.res = [(0) for i in range(n)] edges_dic = collections.defaultdict(list) for i in range(len(edges)): edges_dic[edges[i][0]].append(edges[i][1]) edges_dic[edges[i][1]].append(edges[i][0]) queue = [0] visited = [(0) for i in range(n)] visited[0] = 1 while queue: current = queue.pop(0) for c in edges_dic[current]: if visited[c] == 0: visited[c] = 1 self.nodes[current].add_edge(self.nodes[c]) queue.append(c) def count(self): self.nodes[0].count() def dist(self): self.nodes[0].dist(self.res, 0, 0) return self.res class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: t = STree(N, edges) t.count() return t.dist()
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: edges_dict = dict() for i in range(N): edges_dict[i] = [] for edge in edges: edges_dict[edge[0]].append(edge[1]) edges_dict[edge[1]].append(edge[0]) dp_dict = dict() def dp(node, src): if (node, src) in dp_dict: return dp_dict[node, src] tgt_num = 1 tgt_sum = 0 for tgt in edges_dict[node]: if tgt == src: continue else: temp = dp(tgt, node) tgt_num += temp[0] tgt_sum += temp[0] + temp[1] dp_dict[node, src] = tgt_num, tgt_sum return dp_dict[node, src] results = [0] * N for node in range(N): for tgt in edges_dict[node]: results[node] += sum(dp(tgt, node)) return results
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: Nodes = [None] * N def node(i): if Nodes[i] is None: Nodes[i] = Node(i) return Nodes[i] adj = defaultdict(list) for edge in edges: adj[edge[0]].append(edge[1]) adj[edge[1]].append(edge[0]) root = node(0) stack = [0] while stack: i = stack.pop() for j in adj[i]: if Nodes[j] is None: stack.append(j) node(i).children.append(node(j)) self.below(root) ans = [0] * N ans[0] = node(0).dists_below stack = [node(0)] while stack: parent = stack.pop() for child in parent.children: stack.append(child) ans[child.val] = ans[parent.val] + N - 2 - 2 * child.below return ans def below(self, node): node.below = 0 node.dists_below = 0 for child in node.children: self.below(child) node.below += 1 + child.below node.dists_below += child.dists_below node.dists_below += node.below class Node: def __init__(self, val, children=None): self.val = val self.children = children if children else []
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR FUNC_DEF IF VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR LIST
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: root = 0 children = [[] for i in range(N)] for a, b in edges: children[a].append(b) children[b].append(a) sub_size = [0] * N def compute_sub_size(node, parent): sub_size[node] = 1 for child in children[node]: if child != parent: compute_sub_size(child, node) sub_size[node] += sub_size[child] desc_dist = [0] * N def compute_desc_dist(node, parent): for child in children[node]: if child != parent: compute_desc_dist(child, node) desc_dist[node] += desc_dist[child] + sub_size[child] all_dist = [0] * N def compute_all_dist(node, parent): if parent >= 0: all_dist[node] = all_dist[parent] - (desc_dist[node] + sub_size[node]) all_dist[node] += sub_size[root] - sub_size[node] for child in children[node]: if child != parent: all_dist[node] += desc_dist[child] + sub_size[child] for child in children[node]: if child != parent: compute_all_dist(child, node) compute_sub_size(0, -1) compute_desc_dist(0, -1) compute_all_dist(0, -1) return all_dist
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER RETURN VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: if N == 1: return [0] neighbors = {} for edge in edges: a = edge[0] b = edge[1] if a not in neighbors: neighbors[a] = [] if b not in neighbors: neighbors[b] = [] neighbors[a].append(b) neighbors[b].append(a) num_descendents = {} children = {} parent = {(0): None} queue = [0] while queue != []: curr = queue.pop() children[curr] = [] for neighbor in neighbors[curr]: if neighbor == parent[curr]: continue child = neighbor children[curr].append(child) queue.append(child) parent[child] = curr def build_descendents(n): num_descendents[n] = 0 for child in children[n]: num_descendents[n] += 1 + build_descendents(child) return num_descendents[n] build_descendents(0) root_dist = {(0): 0} queue = children[0].copy() while queue != []: curr = queue.pop() p = parent[curr] root_dist[curr] = root_dist[p] + 1 for child in children[curr]: queue.append(child) root_sum_dist = 0 for n in range(N): root_sum_dist += root_dist[n] queue = children[0].copy() sum_dist = {(0): root_sum_dist} while queue != []: curr = queue.pop() p = parent[curr] desc_curr = num_descendents[curr] other_kids_and_desc = num_descendents[p] - 1 - desc_curr above_parent = num_descendents[0] - num_descendents[p] sum_dist[curr] = ( sum_dist[p] - desc_curr + other_kids_and_desc + above_parent ) for child in children[curr]: queue.append(child) ans = [] for n in range(N): ans.append(sum_dist[n]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT NUMBER NONE ASSIGN VAR LIST NUMBER WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT NUMBER VAR WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: ans = [0] * N graph = defaultdict(set) for u, v in edges: graph[u].add(v) graph[v].add(u) visited = {} return [self.dfs(graph, i, -1, visited)[0] for i in range(N)] def dfs(self, graph, node, parent, visited): if (node, parent) in visited: return visited[node, parent] distance, count = 0, 1 for child in graph[node]: if child != parent: d, c = self.dfs(graph, child, node, visited) distance += d + c count += c visited[node, parent] = distance, count return distance, count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: if N == 1: return [0] graph_dict = {} for pair in edges: left, right = pair[0], pair[1] graph_dict[left] = [right] + graph_dict.get(left, []) graph_dict[right] = [left] + graph_dict.get(right, []) memo_dict = {} ans = [] for i in range(N): ans += [self.helper(i, None, graph_dict, memo_dict)[0]] return ans def helper(self, node, parent, graph_dict, memo_dict): if graph_dict[node] == [parent]: return 0, 1 elif (node, parent) in memo_dict: return memo_dict[node, parent] else: found_sum = 0 num_nodes = 1 for neighbor in graph_dict[node]: if neighbor != parent: curr_sum, curr_nodes = self.helper( neighbor, node, graph_dict, memo_dict ) found_sum = 1 * curr_nodes + curr_sum + found_sum num_nodes += curr_nodes memo_dict[node, parent] = found_sum, num_nodes return found_sum, num_nodes
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP LIST VAR FUNC_CALL VAR VAR LIST ASSIGN VAR VAR BIN_OP LIST VAR FUNC_CALL VAR VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR NONE VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF IF VAR VAR LIST VAR RETURN NUMBER NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: def dfs(node, parent): for child in graph[node]: if child != parent: dfs(child, node) count_self_and_children[node] += count_self_and_children[child] res[node] += res[child] + count_self_and_children[child] def dfs2(node, parent): for child in graph[node]: if child != parent: res[child] = res[node] + N - 2 * count_self_and_children[child] dfs2(child, node) graph = collections.defaultdict(list) for a, b in edges: graph[a].append(b) graph[b].append(a) count_self_and_children = [1] * N res = [0] * N dfs(0, None) dfs2(0, None) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER NONE EXPR FUNC_CALL VAR NUMBER NONE RETURN VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: if N == 1: return [0] adj = [[] for _ in range(N)] for [u, v] in edges: adj[u].append(v) adj[v].append(u) d_dists = {} d_vertices_up_to_u = {} sol = [(0) for _ in range(N)] def dfs_dist(u, v, adj, d_dists, d_vertices_up_to_u): if (u, v) in d_dists: return d_dists[u, v], d_vertices_up_to_u[u, v] l_neibs = adj[u][:] l_neibs.remove(v) if len(l_neibs) == 0: d_dists[u, v] = 1 d_vertices_up_to_u[u, v] = 1 return d_dists[u, v], d_vertices_up_to_u[u, v] dist_u_v = 0 vertices_up_to_u = 1 for nei in l_neibs: a, b = dfs_dist(nei, u, adj, d_dists, d_vertices_up_to_u) dist_u_v += a vertices_up_to_u += b dist_u_v += vertices_up_to_u d_dists[u, v] = dist_u_v d_vertices_up_to_u[u, v] = vertices_up_to_u return d_dists[u, v], d_vertices_up_to_u[u, v] for v in range(N): for u in adj[v]: a, _ = dfs_dist(u, v, adj, d_dists, d_vertices_up_to_u) sol[v] += a return sol
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: graph = defaultdict(list) for u, v in edges: graph[u].append(v) graph[v].append(u) start = -1 for node in graph: if len(graph[node]) == 1: start = node def getStartingDist(node, parent=None, d=0): return d + sum( getStartingDist(child, node, d + 1) for child in graph[node] if child != parent ) dist = getStartingDist(start) remainingCt = {} def getCtRemaining(node, parent): remainingCt[node] = 1 + sum( getCtRemaining(child, node) for child in graph[node] if child != parent ) return remainingCt[node] getCtRemaining(start, None) result = [None] * N result[start] = dist def dfs(node, parent, running, nodesUp): result[node] = running for child in graph[node]: if child != parent: newNodesUp = nodesUp + remainingCt[node] - remainingCt[child] newDistSum = running - (remainingCt[child] - 1) + (newNodesUp - 1) dfs(child, node, newDistSum, newNodesUp) dfs(start, None, dist, 0) return result
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_DEF NONE NUMBER RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR VAR NONE ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NONE VAR NUMBER RETURN VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: if N == 1: return [0] dt = {} for e in edges: if e[0] not in dt: dt[e[0]] = [] dt[e[0]].append(e[1]) if e[1] not in dt: dt[e[1]] = [] dt[e[1]].append(e[0]) sk = [0] st = set() self.tree = {} while sk: node = sk[-1] sk.pop() st.add(node) self.tree[node] = {"d": 0, "n": 0, "c": []} for i in dt[node]: if i not in st: sk.append(i) self.tree[node]["c"].append(i) self.dfs(0) self.help(0, self.tree[0]["d"], self.tree[0]["n"]) ans = [(0) for _ in range(N)] for key in self.tree: ans[key] = self.tree[key]["d"] return ans def dfs(self, key): if not self.tree[key]: return dst, num = 0, 0 for ch in self.tree[key]["c"]: self.dfs(ch) dst += self.tree[ch]["d"] + self.tree[ch]["n"] num += self.tree[ch]["n"] self.tree[key]["d"] = dst self.tree[key]["n"] = num + 1 def help(self, key, dst, num): if num > self.tree[key]["n"]: self.tree[key]["d"] = dst + num - 2 * self.tree[key]["n"] self.tree[key]["n"] = num for ch in self.tree[key]["c"]: self.help(ch, self.tree[key]["d"], self.tree[key]["n"])
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT WHILE VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR DICT STRING STRING STRING NUMBER NUMBER LIST FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR STRING RETURN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR STRING VAR VAR STRING VAR VAR VAR STRING ASSIGN VAR VAR STRING VAR ASSIGN VAR VAR STRING BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR STRING ASSIGN VAR VAR STRING BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR STRING ASSIGN VAR VAR STRING VAR FOR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING VAR VAR STRING
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: adj_list = defaultdict(list) for v1, v2 in edges: adj_list[v1].append(v2) adj_list[v2].append(v1) root = 0 subT_size = {} def postorder(t, par_t) -> int: if len([c for c in adj_list[t] if c != par_t]) == 0: subT_size[t] = 1 return 0 else: ans = sum(postorder(c, t) for c in adj_list[t] if c != par_t) subT_size[t] = sum(subT_size[c] for c in adj_list[t] if c != par_t) + 1 return ans + subT_size[t] - 1 res = [0] * N def preorder(t, par_t): res[t] = res[par_t] - subT_size[t] * 2 + tot for c in adj_list[t]: if c != par_t: preorder(c, t) res[root] = postorder(root, -1) tot = subT_size[root] print(subT_size) for c in adj_list[root]: preorder(c, root) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: connected = collections.defaultdict(lambda: []) for s, e in edges: connected[s].append(e) connected[e].append(s) @functools.lru_cache(None) def get_to_father(node=0, father=None): sons = sorted([t for t in connected[node] if not t == father]) to_ft_node = 0 to_ft_sum = 0 for son in sons: node_nt, sum_nt = get_to_father(son, node) to_ft_node += node_nt to_ft_sum += sum_nt to_ft_sum += to_ft_node to_ft_node += 1 return to_ft_node, to_ft_sum to_ret = [0] * N def visit(node=min(12, N - 1), father=None, to_son_n=0, to_son_sum=0): sons = sorted([t for t in connected[node] if not t == father]) ns_list = [get_to_father(t, node) for t in sons] all_node = sum([t[0] for t in ns_list]) all_sum = sum([t[1] for t in ns_list]) to_ret[node] = all_sum + all_node + to_son_sum + to_son_n for son, (snode, ssum) in zip(sons, ns_list): visit( son, node, to_son_n + all_node - snode + 1, to_ret[node] - ssum - snode, ) visit() return to_ret
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF NUMBER NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR NONE ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NONE NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution: def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]: connect = [set() for _ in range(N)] num_children = [0] * N dist = [0] * N for i, j in edges: connect[i].add(j) connect[j].add(i) dist[0] = self.dfs1(0, connect, num_children, 0, set()) self.dfs2(0, connect, num_children, dist, 0, N, set()) return dist def dfs1(self, cur, connect, num_children, dist, seen): seen.add(cur) if not connect[cur]: num_children[cur] = 1 return dist total_dist = 0 for i in connect[cur]: if i in seen: continue total_dist += self.dfs1(i, connect, num_children, dist + 1, seen) num_children[cur] += num_children[i] num_children[cur] += 1 return total_dist + dist def dfs2(self, cur, connect, num_children, res, previous, N, seen): seen.add(cur) if cur != 0: res[cur] = res[previous] - num_children[cur] + N - num_children[cur] for i in connect[cur]: if i in seen: continue self.dfs2(i, connect, num_children, res, cur, N, seen)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR RETURN VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 / \ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Note: 1 <= N <= 10000
class Solution(object): def sumOfDistancesInTree(self, N, edges): if N == 0 or not edges: return [0] graph = {i: set() for i in range(N)} for n1, n2 in edges: graph[n1].add(n2) graph[n2].add(n1) count = [1] * N subSums = [0] * N def countSubTree(node, parent): nonlocal count, subSums for child in (n for n in graph[node] if n != parent): countSubTree(child, node) count[node] += count[child] subSums[node] += subSums[child] + count[child] countSubTree(0, None) result = [0] * N result[0] = subSums[0] def countGlobal(node, parent): nonlocal result, count if parent != None: result[node] = result[parent] - count[node] + N - count[node] for child in (n for n in graph[node] if n != parent): countGlobal(child, node) countGlobal(0, None) return result
CLASS_DEF VAR FUNC_DEF IF VAR NUMBER VAR RETURN LIST NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF FOR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NONE ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FUNC_DEF IF VAR NONE ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NONE RETURN VAR