description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) for i2 in range(t): n, a, b = map(int, input().split()) s = str(input()) dp = [([0] * 2) for i in range(n)] maxn = 123456789876543212345678 for i in range(n): if i == 0: dp[0][0], dp[0][1] = a + b, maxn elif s[i] == "0": dp[i][0] = min(dp[i - 1][0], dp[i - 1][1] + a + b) + a + b dp[i][1] = min(dp[i - 1][0] + a, dp[i - 1][1]) + a + b * 2 else: dp[i][0] = maxn dp[i][1] = min(dp[i - 1][0] + a, dp[i - 1][1]) + a + b * 2 print(dp[n - 1][0] + b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
import sys readline = sys.stdin.readline readlines = sys.stdin.readlines ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep="\n") def solve(): n, a, b = nm() s = list(map(int, list(input()))) dp = [([10**18] * 2) for i in range(n + 1)] dp[0][0] = b for i in range(n): if s[i]: dp[i + 1][1] = dp[i][1] + a + b * 2 else: dp[i + 1][0] = min(dp[i][0] + a + b, dp[i][1] + a * 2 + b) dp[i + 1][1] = min(dp[i][0] + 2 * a + 2 * b, dp[i][1] + a + b * 2) print(dp[n][0]) return T = ni() for _ in range(T): solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) for i in range(t): n, a, b = map(int, input().split()) s = input() arr = [] c = "0" k = 0 for i in range(n): if s[i] == c: k += 1 else: arr.append(k) k = 1 c = s[i] if len(arr) == 0: print(n * a + b * n + b) else: ans = k * (a + b) + (a + b) * arr[0] + a for i in range(1, len(arr)): if i % 2 == 1: ans += (arr[i] + 1) * (2 * b + a) else: ans += min( (arr[i] + 1) * a + b * (arr[i] - 1), 2 * b * (arr[i] - 1) + a * (arr[i] - 1), ) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) for _ in range(t): n, a, b = map(int, input().split()) l = list(input()) dp1 = [10**18] * (n + 1) dp2 = [10**18] * (n + 1) dp1[0] = b i = 1 while i < n: if l[i] == "0": if l[i - 1] != "1": dp1[i] = min(dp2[i - 1] + 2 * a, dp1[i - 1] + a) + b dp2[i] = min(dp1[i - 1] + 2 * a, dp2[i - 1] + a) + 2 * b else: dp2[i] = min(dp1[i - 1] + 2 * a, dp2[i - 1] + a) + 2 * b i += 1 elif l[i] == "1": dp2[i] = min(dp1[i - 1] + 2 * a, dp2[i - 1] + a) + 2 * b i += 1 print(min(dp1[n - 1] + a, dp2[n - 1] + 2 * a) + b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
import sys def input(): return sys.stdin.readline()[:-1] t = int(input()) ans = [] for _ in range(t): n, a, b = map(int, input().split()) s = input() res = [b, 10**30] for i in range(n): tmp = [] if s[i] == "0": tmp.append(min(res[0] + a + b, res[1] + 2 * a + b)) tmp.append(min(res[1] + a + 2 * b, res[0] + 2 * a + 2 * b)) else: tmp.append(10**30) tmp.append(res[1] + a + 2 * b) res = tmp ans.append(res[0]) print(*ans, sep="\n")
IMPORT FUNC_DEF RETURN FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
Q = int(input()) for _ in range(Q): n, a, b = list(map(int, input().split())) s = list(map(int, input())) m = a * n m += b * (n + 1) if sum(s) == 0: print(m) continue head = s.index(1) for i in reversed(range(n)): if s[i] == 1: tail = i break c = a * 2 // b + 1 i = head while i < tail: i += 1 if s[i] == 1: continue j = i + 1 while s[j] == 0: j += 1 if j - i <= c: for k in range(i, j): s[k] = 1 i = j z = 1 for i in range(head, tail): if s[i + 1] - s[i] == 1: z += 1 m += sum(s) * b m += z * 2 * a m += z * b print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) while t != 0: n, a, b = map(int, input().split()) pipe = a pillar = b s = input() dp = [[float("inf") for i in range(n + 2)] for j in range(2)] dp[0][0] = pillar for i in range(n): if s[i] == "1": dp[0][i + 1] = float("inf") dp[1][i + 1] = dp[1][i] + 2 * pillar + pipe else: dp[0][i + 1] = min(dp[0][i] + pillar + pipe, dp[1][i] + 2 * pipe + pillar) dp[1][i + 1] = min( dp[0][i] + 2 * pillar + 2 * pipe, dp[1][i] + 2 * pillar + pipe ) print(dp[0][n]) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) for each1 in range(t): data_list = input().split(" ") n = int(data_list[0]) a = int(data_list[1]) b = int(data_list[2]) ss = input() ans = 0 l = 0 r = 0 ss_len = len(ss) i = 0 while i < ss_len: l = i while i < ss_len and ss[i] == "0": i = i + 1 r = i if r == ss_len: r = r p = r - l if r != ss_len and l != 0: if (p - 1) * b + (p + 2) * a > (p - 1) * 2 * b + p * a: ans = ans + (p - 1) * 2 * b + p * a else: ans = ans + (p - 1) * b + (p + 2) * a elif l == 0 and r == ss_len: ans = ans + p * a + (p + 1) * b else: ans = ans + p * b + (p + 1) * a l = i while i < ss_len and ss[i] == "1": i = i + 1 r = i p = r - l if r < ss_len: ans = ans + p * a + (p + 1) * 2 * b print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
for _ in range(int(input())): n, a, b = map(int, input().split()) s = input() next1 = [float("inf")] * (n + 1) j = float("inf") for i in range(n - 1, -1, -1): next1[i] = j if s[i] == "1": j = i ans = a * n + b * (n + 1) i = 0 flag = 0 while i < n: if s[i] == "1": if flag == 0: ans += a ans += b ans += min((next1[i] - i - 1) * b, 2 * a + b) flag = 1 i = next1[i] else: i += 1 if flag == 1: ans -= a print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
T = int(input()) for tt in range(0, T): n, a, b = [int(x) for x in input().split()] s = input() cntZero = 1 zeros = [] ones = [] cntOne = 0 cost = 0 for i in range(1, n): if s[i] == "0": cntZero += 1 if cntOne > 0: ones.append(cntOne) cntOne = 0 elif s[i] == "1": cntOne += 1 if cntZero > 0: zeros.append(cntZero) cntZero = 0 zeros.append(cntZero) if len(ones) == 0: cost = zeros[0] * a + (zeros[0] + 1) * b print(cost) continue for k in ones: cost += k * (a + 2 * b) + 2 * b for i in range(1, len(zeros) - 1): val1 = zeros[i] * a + (zeros[i] - 1) * 2 * b val2 = 2 * a + (zeros[i] - 2) * a + 2 * a + (zeros[i] - 1) * b if zeros[i] == 1: cost += val1 else: cost += min((val1, val2)) cost += (zeros[0] - 1) * a + 2 * a + zeros[0] * b cost += (zeros[-1] - 1) * a + 2 * a + zeros[-1] * b print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
for TT in range(1, int(input()) + 1): n, a, b = map(int, input().split()) s = input() dp = [0, float("inf")] for i in range(n): if "1" in s[i : i + 2]: dp = [float("inf"), min(dp[0] + a, dp[1]) + b] else: dp = [min(dp[0], dp[1] + a), min(dp[0] + a, dp[1]) + b] print(dp[0] + n * (a + b) + b)
FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF STRING VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
T = int(input()) for _ in range(T): n, a, b = map(int, input().split()) s = list(map(int, input())) if 1 not in s: print(a * n + b * (n + 1)) continue min_lower_length = 2 * a / b + 1 start = 0 while s[start] == 0: start += 1 end = n - 1 while s[end] == 0: end -= 1 cost = (n + 2) * a + (n + 1) * b + (end - start + 2) * b c = 0 for i in range(start, end + 1): if s[i] == 0: c += 1 else: if c >= min_lower_length: cost += 2 * a - (c - 1) * b c = 0 print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
T = int(input()) for i in range(T): n, a, b = [int(i) for i in input().split()] s = [int(c) for c in input().strip("0")] cost = 0 nzero = 0 if len(s) == 0: cost = n * a + (n + 1) * b else: cost = (n - len(s) + 2) * (a + b) for e in s: if e == 0: nzero += 1 else: if nzero == 0: cost += a + 2 * b elif nzero == 1: cost += 2 * (a + 2 * b) elif nzero < 2 * a / b + 1: cost += (nzero + 1) * (a + 2 * b) else: cost += (nzero + 3) * (a + b) nzero = 0 print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR BIN_OP NUMBER VAR IF VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
for k in range(int(input())): n, cpipe, cpillar = map(int, input().split()) s = input() if int(s) == 0: print(n * cpipe + n * cpillar + cpillar) else: ti = 0 for i in range(n): if s[i] == "1": ti = i break l = [] ans = 0 for i in range(ti + 1, n): if s[i] == "1": l.append(i - ti) ti = i for i in range(len(l)): if l[i] <= 2: ans += l[i] * cpillar else: ans += 2 * cpillar + min(2 * cpipe, (l[i] - 2) * cpillar) print(n * cpipe + (n + 1) * cpillar + ans + 2 * cpillar + 2 * cpipe)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
import sys input = sys.stdin.readline T = int(input()) for testcases in range(T): n, a, b = list(map(int, input().split())) S = list(map(int, list(input().strip()))) S.append(0) ANS = (n + 1) * b + n * a DP = [[1 << 50, 1 << 50] for i in range(n + 1)] DP[0][0] = 0 for i in range(1, n + 1): if S[i - 1] == S[i] == 0: DP[i][0] = min(DP[i - 1][0], DP[i - 1][1] + a) DP[i][1] = min(DP[i - 1][0] + a + b, DP[i - 1][1] + b) else: DP[i][1] = min(DP[i - 1][0] + a + b, DP[i - 1][1] + b) print(ANS + DP[-1][0])
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR LIST BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
import sys def main(): import sys input = sys.stdin.readline for _ in range(int(input())): N, a, b = map(int, input().split()) S = input().rstrip("\n") dp = [([0] * 2) for _ in range(N + 1)] inf = int(float(10**20)) dp[0][0] = b dp[0][1] = inf for i in range(N): if S[i] == "0": dp[i + 1][0] = min(dp[i][0] + a + b, dp[i][1] + 2 * a + b) dp[i + 1][1] = min(dp[i][0] + a * 2 + 2 * b, dp[i][1] + a + 2 * b) else: dp[i + 1][0] = inf dp[i + 1][1] = dp[i][1] + a + 2 * b print(dp[-1][0]) main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) for _ in range(t): n, a, b = (int(x) for x in input().split()) s = input() cost = n * (a + b) + b count = 0 i = s.find("1") while 0 <= i < n: j = i while j < n - 1 and s[j + 1] == "1": j += 1 ones = j - i + 1 cost += (ones + 1) * b i = s.find("1", j + 1) if i == -1: cost += 2 * a break zeros = i - j - 1 cost += min((zeros - 1) * b, 2 * a) print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE NUMBER VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
import sys maxx = 100000000000000001 q = int(input()) for i in range(q): ch = input() L = [int(i) for i in ch.split()] n = L[0] a = L[1] b = L[2] ch = input() M = [[b, maxx]] + [[maxx, maxx] for i in range(n)] for j in range(1, n + 1): M[j][1] = min(M[j - 1][0] + 2 * b + 2 * a, M[j - 1][1] + 2 * b + a) if (j - 1 < 0 or ch[j - 1] == "0") and (j >= n or ch[j] == "0"): M[j][0] = min(M[j - 1][0] + a + b, M[j - 1][1] + 2 * a + b) print(M[-1][0])
IMPORT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST LIST VAR VAR LIST VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING VAR VAR VAR VAR STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
q = int(input()) for Q in range(q): n, a, b = tuple(map(int, input().split())) s = input() if s == "0" * n: print(n * a + (n + 1) * b) continue cost = n * a + 2 * n * b i = 0 while s[i] == "0": cost -= b i += 1 cost += a while i < n: while i < n and s[i] == "1": i += 1 start = i while i < n and s[i] == "0": i += 1 end = i - 1 if i == n: cost += a - b * (end - start) elif (end - start) * b > 2 * a: cost = cost - (end - start) * b + 2 * a print(cost + b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR VAR VAR NUMBER VAR VAR WHILE VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
import sys def main(): import sys input = sys.stdin.readline def solve(): n, a, b = map(int, input().split()) s = input() arr = [0] * (n + 1) for i in range(1, n): if s[i] == "1" or s[i - 1] == "1": arr[i] = 1 cnts = [] cnt = 1 for i in range(1, n + 1): if arr[i] != arr[i - 1]: cnts.append(cnt) cnt = 1 else: cnt += 1 cnts.append(cnt) l = len(cnts) cost = n * a + (l - 1) * a + (n + 1) * b + sum(cnts[1:l:2]) * b for i in range(2, l - 2, 2): now = 2 * a + cnts[i] * b new = cnts[i] * 2 * b if new < now: cost += new - now print(cost) for _ in range(int(input())): solve() return 0 main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN NUMBER EXPR FUNC_CALL VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): n, a, b = map(int, input().split()) l = input().strip() p = 0 cur = 0 ans = 0 if "1" not in l: print(b * (n + 1) + a * n) else: while p < n: if l[p] == "1": ans += b * p + 2 * b + a * (p + 1) break else: p += 1 cur = p while p < n: if l[p] == "1": diff = p - cur if diff <= 2: ans += 2 * b * diff + a * diff cur = p p += 1 else: ans1 = 2 * b * diff + a * diff ans2 = 2 * b * 2 + b * (diff - 2) + a * (diff + 2) ans += min(ans1, ans2) cur = p p += 1 else: p += 1 diff = n - cur ans += 2 * b + (diff - 1) * b + a * (diff + 1) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR WHILE VAR VAR IF VAR VAR STRING VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
from sys import stdin item = lambda: stdin.readline().split() it = lambda: stdin.readline()[:-1] for _ in range(int(stdin.readline())): n, a, b = map(int, item()) s = it() x = s.find("1") if x == -1: print(n * (a + b) + b) continue res = (n + 1) * (a + b) + b for y in range(x + 1, n): if s[y] == "1": if y - x - 1 > 1: if 2 * a > (y - x - 2) * b: res += (y - x - 2) * b else: res += 2 * a res += b x = y if s[y] == "0" and s[y - 1] == "1": res += b res += a print(res)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) while t: t -= 1 n, a, b = [int(x) for x in input().split()] s = input() l = -1 r = -1 for i in range(len(s)): if s[i] == "1": l = i break if l != -1: for i in range(len(s) - 1, -1, -1): if s[i] == "1": r = i break c = n * a + (n + 1) * b if l == -1: print(c) else: ch0 = 0 ch1 = 0 nzero = 0 for i in range(l, r + 2): if s[i] == "0": ch0 = 1 if ch1 == 1: c += b ch1 = 0 nzero += 1 else: ch1 = 1 if ch0 == 1: if (nzero - 1) * b < 2 * a: c += (nzero - 1) * b else: c += 2 * a ch0 = 0 nzero = 0 c += b print(c + 2 * a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
R = lambda: map(int, input().split()) for _ in range(int(input())): n, a, b = R() s = input() low, high = [0] * (n + 1), [0] * (n + 1) low[-1] = b high[-1] = 10**15 for i in range(n): if "1" == s[i]: high[i] = high[i - 1] + a + 2 * b low[i] = 10**15 else: high[i] = min(high[i - 1] + a + 2 * b, low[i - 1] + 2 * a + 2 * b) low[i] = min(low[i - 1] + a + b, high[i - 1] + 2 * a + b) print(low[n - 1])
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF STRING VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
from sys import stdin def ii(): return int(stdin.readline()) def mi(): return map(int, stdin.readline().split()) def li(): return list(mi()) def si(): return stdin.readline() for _ in range(ii()): n, a, b = mi() s = si() p, x, q = n + 2, 0, 2 * n d = s.find("1") if d > -1: q -= d - 1 e = s.rfind("1") q -= n - e - 2 for i in range(d + 1, e + 1): if s[i] == "0": x += 1 elif x: if a * (x + 1) + b * (x - 1) < a * (x - 1) + 2 * b * (x - 1): p += 2 q -= x - 1 x = 0 else: p = n q = n + 1 print(p * a + q * b)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
def mp(): return map(int, input().split()) def f(i, j): return a[i][j] == a[i + 1][j] == a[i][j + 1] == a[i + 1][j + 1] == 1 q = int(input()) for qq in range(q): n, a, b = mp() s = input() dp_u = [0] * (n + 1) dp_d = [0] * (n + 1) dp_u[0] = 10**20 dp_d[0] = b for i in range(1, n): dp_u[i] = min(dp_u[i - 1] + a, dp_d[i - 1] + 2 * a) + 2 * b if s[i] == "0" and s[i - 1] == "0": dp_d[i] = min(dp_u[i - 1] + 2 * a, dp_d[i - 1] + a) + b else: dp_d[i] = 10**20 print(min(dp_d[n - 1] + a, dp_u[n - 1] + 2 * a) + b)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) for _ in range(t): n, pipeCost, pillarCost = map(int, input().split()) road = input() cost = 0 freq = [] curr = "0" currFreq = 0 for i in road: if i == curr: currFreq += 1 else: freq.append(currFreq) curr = i currFreq = 1 freq.append(currFreq) if len(freq) == 3: cost += pillarCost * freq[0] cost += pillarCost * 2 * (freq[1] + 1) cost += pillarCost * freq[2] cost += pipeCost * (sum(freq) + 2) elif len(freq) == 1: cost += pillarCost * (freq[0] + 1) cost += pipeCost * freq[0] else: level = True switch = 0 cost += pipeCost * sum(freq) for i in range(len(freq)): if i % 2 == 0: if level: if i == 0 or i == len(freq) - 1: cost += pillarCost * freq[i] else: cost += pillarCost * (freq[i] - 1) elif i == len(freq) - 1: level = True cost += pillarCost * freq[i] elif (freq[i] - 1) * pillarCost > 2 * pipeCost: level = True cost += pillarCost * (freq[i] - 1) else: cost += pillarCost * 2 * (freq[i] - 1) elif level: level = False switch += 1 cost += pillarCost * 2 * (freq[i] + 1) else: cost += pillarCost * 2 * (freq[i] + 1) cost += 2 * switch * pipeCost print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) for i in range(t): n, a, b = map(int, input().split()) s = input() p = [] p1 = 0 p2 = 0 for j in range(n): if s[j] == "0": p1 += 1 if p2 != 0: p.append(p2) p2 = 0 else: p2 += 1 if p1 != 0: p.append(p1) p1 = 0 if p1 != 0: p.append(p1) if p2 != 0: p.append(p2) et = 0 dld = 0 dls = 1 uk = 0 for j in range(len(p)): if s[uk] == "0": if et == 0: if j == len(p) - 1: dld += p[j] uk += p[j] dls += p[j] else: et = 1 dld += p[j] + 1 dls += p[j] + 1 uk += p[j] elif et == 1: if j == len(p) - 1: dld += 2 + p[j] - 1 dls += p[j] uk += p[j] elif p[j] == 1: dld += 1 uk += p[j] dls += 2 elif (4 + p[j] - 2) * a + (p[j] - 1) * b <= p[j] * a + ( p[j] - 1 ) * 2 * b: dld += 4 + p[j] - 2 dls += p[j] + 1 uk += p[j] else: dld += p[j] dls += p[j] * 2 uk += p[j] else: dld += p[j] dls += p[j] * 2 uk += p[j] print(dld * a + dls * b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
def solve(): N, A, B = [int(i) for i in input().split(" ")] road = input() cost = ( A * N + (N + 1) * B + road.count("1") * B + 2 * road.count("01") * A + road.count("01") * B ) maxWorthGap = int(A / B) zeros = 0 wasOne = False for i in range(N): if road[i] == "0" and wasOne: zeros += 1 elif road[i] == "1": wasOne = True delta = (zeros - 1) * B - 2 * A if zeros > 0 and delta < 0: cost += delta zeros = 0 print(cost) Q = int(input()) for _ in range(Q): solve()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR STRING VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) vsp = [] for i in range(t): n, a, b = map(int, input().split()) s = input() ind_of_1 = [] for i in range(n): if s[i] == "1": ind_of_1.append(i) m = len(ind_of_1) j = -1 count = b h = 1 hs = [] for i in range(n - 1): if s[i] == "0" and s[i - 1] == "1": if j == m - 1: count += 2 * a + b h = 1 elif ( 4 * a + b + (ind_of_1[j + 1] - ind_of_1[j] - 3) * (a + b) < (ind_of_1[j + 1] - ind_of_1[j] - 1) * a + (ind_of_1[j + 1] - ind_of_1[j] - 2) * 2 * b ): h = 1 count += 2 * a + b else: count += a + 2 * b elif s[i] == "0" and s[i + 1] == "1" and h == 1: count += 2 * a + 2 * b h = 2 elif s[i] == "0" and s[i + 1] == "1" and h == 2: count += a + 2 * b elif s[i] == "0" and s[i + 1] == "0" and h == 1: count += a + b elif s[i] == "0" and s[i - 1] == "0" and h == 2: count += a + 2 * b elif s[i] == "1": count += a + 2 * b j += 1 hs.append(h) if h == 2: count += 2 * a + b else: count += a + b print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
from sys import setcheckinterval, stdin setcheckinterval(1000) def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) for _ in range(iin()): nn, p, h = lin() s = list(input()) sa = [] i = 0 while i < nn: ch = 0 val = s[i] while i < nn and s[i] == val: i += 1 ch += 1 sa.append([val, ch]) l = len(sa) ans = h for i in range(l): n = sa[i][1] if sa[i][0] == "0": if i == 0: if l - 1 == 0: ans += n * (p + h) else: ans += (n + 1) * (p + h) elif i == l - 1: ans += p * (n + 1) + n * h elif n == 1: ans += p + 2 * h else: ans += min((n + 2) * p + (n + 1) * h, n * (p + 2 * h)) else: ans += n * (p + 2 * h) print(ans)
EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER STRING IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
inf = 10**18 t = int(input()) for q in range(t): n, a, b = map(int, input().split()) path = input() dp = [[inf, inf] for i in range(n + 1)] dp[0][0] = b for i in range(0, n): if path[i] == "0": dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + a + b) dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + 2 * (a + b)) dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + a + 2 * b) dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + 2 * a + b) else: dp[i + 1][1] = dp[i][1] + a + 2 * b print(dp[n][0])
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
import sys t = int(sys.stdin.readline()) for _ in range(t): n, pip_cost, pillor_cost = map(int, sys.stdin.readline().split()) strr = sys.stdin.readline() arr = [(i + 1) for i in range(n) if strr[i] == "1"] pip_len = n + 2 if arr else n pillor_len = (n + 1) * 2 - (arr[0] - 1) - (n - arr[-1]) if arr else n + 1 for i in range(1, len(arr)): length = arr[i] - 2 - arr[i - 1] if length > 0: if 2 * pip_cost < length * pillor_cost: pip_len += 2 pillor_len -= length print(pip_len * pip_cost + pillor_len * pillor_cost)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
def solve(s, n, a, b): cost = a * n cost += b * (n + 1) dp = [] curr = s[0] count = 0 for i in s: if i == curr: count += 1 else: dp.append((curr, count)) curr = i count = 1 dp.append((curr, count)) for i in range(1, len(dp) - 1): if dp[i][0] == "0" and dp[i - 1][0] == "1" and dp[i + 1][0] == "1": cost += min(2 * a, b * (dp[i][1] - 1)) for i in dp: if i[0] == "1": cost += b * (1 + i[1]) if len(dp) >= 3: cost += 2 * a print(cost) def main(): t = int(input()) for i in range(t): n, a, b = map(int, input().split()) s = input() solve(s, n, a, b) main()
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER STRING VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
import sys T = int(sys.stdin.readline()) for t in range(0, T): n, a, b = list(map(int, sys.stdin.readline().strip().split())) s = sys.stdin.readline().strip() i = 0 c = n * a + (n + 1) * b v = 0 for j in range(0, n - 1): if s[j] == "1" or s[j + 1] == "1": c = c + b v = 1 if v == 1: c = c + 2 * a while s[i] == "0": i = i + 1 j = n - 1 while s[j] == "0": j = j - 1 v = 0 while i <= j: if s[i] == "1": if v > 0: c = c + min([(v - 1) * b, 2 * a]) v = 0 if s[i] == "0": v = v + 1 i = i + 1 print(c)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR WHILE VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) for kek in range(t): n, a, b = list(map(int, input().split())) s = list(map(int, input())) h = list() j = 0 ans = 0 while j < len(s) and int(s[j]) != 1: j += 1 if j == len(s): ans = n * a + b * (n + 1) else: i = j for j in range(len(s) - 1): if s[j] == 1 and s[j + 1] == 0: h.append([i + 1, j + 1]) elif s[j] == 0 and s[j + 1] == 1: i = j ans += (h[0][0] + 1) * a + h[0][0] * b ans += (n - h[-1][1] + 1) * a + (n - h[-1][1]) * b for j in h: ans += (j[1] - j[0]) * a ans += (j[1] - j[0] + 1) * 2 * b for j in range(len(h) - 1): x = h[j + 1][0] - h[j][1] poverh = 2 * b * (x - 1) + x * a poniz = b * (x - 1) + (x + 2) * a ans += min(poverh, poniz) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FOR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") for _ in range(int(input())): n, a, b = [int(x) for x in input().split()] s = input() ans = 0 if "1" not in s: print(n * (b + a) + b) continue i = 0 while s[i] == "0": i += 1 ans += a + b ans += a j = n - 1 while s[j] == "0": j -= 1 ans += a + b ans += a temp = s[i : j + 1] i = 0 m = len(temp) while i < m: if temp[i] == "1": ans += 2 * b + a i += 1 else: ans += 2 * b start = i while i < m and temp[i] == "0": i += 1 ans += min( (i - start - 1) * b + (4 + i - start - 2) * a, 2 * b * (i - start - 1) + (i - start) * a, ) print(ans + 2 * b)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR STRING VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
T = int(input()) for i in range(T): n, a, b = map(int, input().split()) s = input() dp = [[(1000000000000000001) for i in range(2)] for j in range(n + 1)] dp[0][0] = a + 2 * b dp[0][1] = 2 * a + 3 * b for i in range(n - 1): if s[i] == "0": if s[i + 1] == "1": dp[i + 1][1] = dp[i][1] + a + 2 * b else: dp[i + 1][1] = min(dp[i][1] + a + 2 * b, dp[i][0] + 2 * a + 2 * b) dp[i + 1][0] = min(dp[i][1] + 2 * a + b, dp[i][0] + a + b) elif s[i + 1] == "1": dp[i + 1][1] = dp[i][1] + a + 2 * b else: dp[i + 1][1] = dp[i][1] + a + 2 * b dp[i + 1][0] = dp[i][1] + 2 * a + b print(dp[n - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
def main(): def solve(): n, a, b = map(int, input().split()) ss = input() cost = 0 for i in range(n): if ss[i] == "1": last = i cost += a * (i + 1) + b * i break else: print(a * n + b * (n + 1)) return for i in range(last, n): if ss[i] == "1" or ss[i - 1] == "1": high = (i - last) * (a + 2 * b) low = (i - last) * (a + b) + a * 2 + b cost += min(high, low) last = i cost += (n + 1 - last) * (a + b) + b print(cost) q = int(input()) for _ in range(q): solve() def __starting_point(): main() __starting_point()
FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
for _ in range(int(input())): n, a, b = map(int, input().split()) s = input() s = [i for i in range(n) if s[i] == "1"] if not s: print(n * a + (n + 1) * b) continue ans = (s[0] + n - s[-1] + 2) * (a + b) + b for i in range(len(s) - 1): ans += a + 2 * b if s[i + 1] == s[i] + 1: continue if s[i + 1] == s[i] + 2: ans += a + 2 * b else: x = s[i + 1] - s[i] - 1 ans += min((x - 1) * b, 2 * a) + (x + 1) * b + x * a print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR STRING IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) for _ in range(t): n, a, b = (int(i) for i in input().split()) s = input() price = n * (a + b) + b i = 0 upped = False while i < n: cur = s[i] if cur == "1": j = i + 1 for j in range(i + 1, n): if s[j] != "1": break ln = j - i k = j + 1 found = 0 for k in range(j + 1, n): if s[k] != "0": found = 1 break if found: to_next = k - j else: to_next = None if not upped: price += a upped = True price += (ln + 1) * b if not to_next: price += a break else: avar = a * 2 bvar = b * (to_next - 1) if avar < bvar: price += avar else: price += bvar i = k else: i += 1 print(price)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NONE IF VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) for _ in range(t): n, a, b = map(int, input().split()) s = input() + "1" ans = n * a + (n + 1) * 2 * b cnt_0 = 0 for i in range(len(s)): tmp = 0 if s[i] == "0": cnt_0 += 1 if s[i] == "1" and cnt_0 > 0: if i - cnt_0 == 0: tmp += 1 if i == n: tmp += 1 if tmp >= 1: ans += (2 - tmp) * a - (cnt_0 - 1 + tmp) * b elif (2 - tmp) * a - (cnt_0 - 1 + tmp) * b < 0: ans += (2 - tmp) * a - (cnt_0 - 1 + tmp) * b cnt_0 = 0 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
for _ in range(int(input())): n, a, b = map(int, input().split()) c = list(map(int, list(input()))) r = [] l = 1 for i in range(1, n): if c[i] == c[i - 1]: l += 1 else: r.append([l, c[i - 1]]) l = 1 r.append([l, c[-1]]) l = 0 ans = n * a + (n + 1) * b for i in range(len(r)): if r[i][1] > l: ans += b * (r[i][0] + 1) ans += a l = 1 elif l > r[i][1] and i != len(r) - 1: if 2 * a < (r[i][0] - 1) * b: ans += a l = 0 else: ans += (r[i][0] - 1) * b elif l > r[i][1] and i == len(r) - 1: ans += a elif l == 1: ans += (r[i][0] + 1) * b print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR IF VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
for rpt in range(int(input())): roadlength, pipecost, pillarcost = [int(x) for x in input().split(" ")] crossroads = input() res = (roadlength + 1) * pillarcost segments = [] current = "0" currentlength = 1 for i in range(1, roadlength): if crossroads[i] == current: currentlength += 1 else: segments.append(currentlength) current = crossroads[i] currentlength = 1 segments.append(currentlength) if len(segments) > 1: res += (segments[0] + 1) * pipecost current = 1 for i in range(1, len(segments) - 1): if current == 1: res += segments[i] * pipecost + segments[i] * pillarcost current = 0 else: if 2 * pipecost >= pillarcost * (segments[i] - 1): res += segments[i] * pipecost + segments[i] * pillarcost else: res += pillarcost + 2 * pipecost + segments[i] * pipecost current = 1 res += segments[-1] * pipecost + pipecost print(res + pillarcost) else: print(res + segments[0] * pipecost)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
rw = int(input()) for wewq in range(rw): n, a, b = map(int, input().split()) s = input() dp = [0] * (n + 1) dp[0] = b t2 = False i = 0 c = [] while i < n - 1: c.append(1) i += 1 while s[i] == s[i - 1] and i < n - 1: c[-1] += 1 i += 1 if s[n - 1] == s[n - 2]: c[-1] += 1 else: c.append(1) cn = len(c) if cn == 1: print(b * c[0] + a * c[0] + b) continue f = (a + b) * c[0] + a tr = c[0] + 1 br = c[0] for i in range(1, cn - 1): if i % 2 == 1: f += b * 2 * c[i] + a * c[i] tr += c[i] br += c[i] * 2 elif (a + b) * c[i] + 2 * a + b > (2 * b + a) * c[i]: f += (2 * b + a) * c[i] tr += c[i] + 2 br += 2 * c[i] else: f += (a + b) * c[i] + 2 * a + b tr += c[i] + 2 br += c[i] + 1 f += a + (a + b) * c[-1] + 2 * b tr += 1 + c[-1] br += 2 + c[-1] print(f)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
inf = 1000000000000000000 t = int(input()) while t > 0: t -= 1 n, pip_price, pil_price = map(int, input().split()) ls = list(input()) dp = [[0, 0] for i in range(n + 5)] dp[0][1] = inf dp[0][0] = pip_price + pil_price for i in range(1, len(ls)): if ls[i] == "1": dp[i][0] = inf + 1 dp[i][1] = min( dp[i - 1][0] + 2 * pip_price + 2 * pil_price, dp[i - 1][1] + 2 * pil_price + pip_price, ) else: dp[i][0] = min( dp[i - 1][0] + pip_price + pil_price, dp[i - 1][1] + 2 * pip_price + 2 * pil_price, ) dp[i][1] = min( dp[i - 1][0] + 2 * pip_price + pil_price, dp[i - 1][1] + 2 * pil_price + pip_price, ) print(dp[n - 1][0] + pil_price)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) for i in range(t): n, a, b = [int(x) for x in input().split()] s = [int(x) for x in list(input())] gas = n h = n + 1 index = -1 one = -1 for i in range(n): if s[i] == 0: if index != -1: h += i - index + 1 gas += 2 index = -1 one = i - 1 elif index == -1: index = i if one != -1 and 2 * a > (i - one - 2) * b: gas -= 2 h += i - one - 2 print(gas * a + h * b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
Q = int(input()) Qs = [] for _ in range(Q): n, a, b = map(int, input().split()) s = input() Qs.append([n, a, b, s]) for n, a, b, S in Qs: l = 0 L = [] for s in list(S): if s == "0": l += 1 else: if not L: L.append(l) elif l >= 2: L.append(l) l = 0 L.append(l) if len(L) == 1: ans = a * n + b * (n + 1) else: ans = a * (n + 2) + b * (2 * (n + 1) - L[0] - L[-1]) maxl = len(L) for i, l in enumerate(L): if i == 0 or i == maxl - 1: continue if (l - 1) * b > 2 * a: ans = ans + 2 * a - (l - 1) * b print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
for _ in range(int(input())): n, a, b = map(int, input().split()) s = input() suf = [-1] * n t = n for i in range(n - 1, -1, -1): suf[i] = t if s[i] == "1": t = i i, f = 0, 0 ans = a * n + b * (n + 1) while i < n: if s[i] == "1": if f == 0: ans += a + b f = 1 if suf[i] == n: ans += a + b break if suf[i] - i <= 2: ans += (suf[i] - i) * b elif (suf[i] - i - 2) * b < 2 * a: ans += (suf[i] - i) * b else: ans += a + b f = 0 i = suf[i] print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
for t in range(int(input())): n, a, b = list(map(int, input().split())) s = [c for c in input()] bridge = 0 raised = False for i, c in enumerate(s): if c == "1" and not raised: raised = True elif c == "0" and raised: bridge += 1 elif c == "1" and raised: if (bridge - 1) * b < 2 * a: for j in range(i - bridge, i): s[j] = "1" bridge = 0 lengths = [] length = 0 for c in s: if c == "1": length += 1 elif length > 0: lengths.append(length) length = 0 print((n + 2 * len(lengths)) * a + (n + 1 + len(lengths) + sum(lengths)) * b)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR ASSIGN VAR NUMBER IF VAR STRING VAR VAR NUMBER IF VAR STRING VAR IF BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
t = int(input()) for _ in range(t): n, a, b = map(int, input().split()) s = list(map(int, list(input()))) g = list() cur = 0 count = 0 for x in s: if x == cur: count += 1 else: g.append(count) count = 1 cur = x ones = sum(s) g.append(count) if len(g) == 1: print(len(s) * a + (len(s) + 1) * b) continue costa, costb, costc = 0, 0, 0 costa += len(s) * a costb += (len(s) + 1) * b upar = 0 neeche = 0 g = g[1:-1] for i in range(0, len(g), 2): costb += (g[i] + 1) * b for i in range(1, len(g), 2): k = g[i] if k == 1: continue upar = b * (k - 1) neeche = 2 * a costc += min(upar, neeche) cost = costa + costb + costc + 2 * a print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
T = int(input()) for _ in range(T): n, a, b = map(int, input().split()) s = input() res = 0 i = 0 while i < n and s[i] == "0": res += a + b i += 1 if i < n: res += 2 * a while s[n - 1] == "0": res += a + b n -= 1 res += (n - i + 1) * 2 * b + (n - i) * a nz = 0 while i < n: if s[i] == "1": if nz > 1 and 2 * a - (nz - 1) * b < 0: res += 2 * a - (nz - 1) * b nz = 0 else: nz += 1 i += 1 else: res += b print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR WHILE VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING IF VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
T = int(input()) for _ in range(T): nab = list(map(int, input().split())) n, a, b = nab[0], nab[1], nab[2] s = str(input()) crp = list() for i in range(n): if s[i] == "1": crp.append(i) min_cost = (n + 1) * b + n * a if len(crp) == 0: print(min_cost) elif len(crp) == 1: print(min_cost + 2 * a + 2 * b) else: min_cost += 2 * a + 2 * b for i in range(len(crp) - 1): if crp[i] == crp[i + 1] - 1: min_cost += b else: min_cost += min(2 * a, (crp[i + 1] - crp[i] - 2) * b) min_cost += 2 * b print(min_cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
T = int(input()) for i in range(0, T): ln = [int(j) for j in input().split(" ")] n = ln[0] a = ln[1] b = ln[2] st = list(input()) ct = 0 zct = 0 pls = 0 pip = 0 seg = [] ft = False for j in range(0, len(st)): if st[j] == "1": ft = False if ct == 0: seg.append(j - 1) ct += 1 zct = 0 elif zct == 0 and ct: ct += 1 ft = True zct += 1 else: if ft: ft = False ct -= 1 zct += 1 if zct == 2 and ct: seg.append(j - 1) pls += ct + 1 zct = 0 ct = 0 if zct == 1 and ct: seg.append(j) if ft: ct -= 1 pls += ct + 1 cost = pls * (b * 2) + (n + 1 - pls) * b cost += (n + len(seg)) * a if len(seg) > 2: for j in range(1, len(seg) - 1, 2): plc = (seg[j + 1] - seg[j]) * b pipc = 2 * a if plc < pipc: cost -= pipc - plc print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
s = lambda: list(map(int, input().split())) T = int(input()) for tc in range(T): n, a, b = s() string = list(map(lambda x: x == "1", input())) cost = a * n + b * (n + 1) isBeginning = True zeros = 0 for x in string: if x: if zeros == 0: cost += b else: if not isBeginning and b * (zeros - 1) < 2 * a: cost -= 2 * a cost += b * (zeros - 1) isBeginning = False zeros = 0 cost += a + b else: if zeros == 0: cost += a + b zeros += 1 print(cost - a - b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF VAR NUMBER VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
q = int(input().strip()) for i in range(q): n, a, b = [int(x) for x in input().strip().split()] r = [int(x) for x in input().strip()] i = 0 r2 = [] while i < n: if r[i] == 0: r2.append([0, 0]) while i < n and r[i] == 0: r2[-1][1] += 1 i += 1 else: r2.append([1, 0]) while i < n and r[i] == 1: r2[-1][1] += 1 i += 1 d = True c = 0 res = "" for par in range(1, len(r2) - 1): if r2[par][0] == 0: if 2 * a > (r2[par][1] - 1) * b or r2[par][1] < 2: res += "1" * r2[par][1] else: res += "+" res += max(r2[par][1] - 2, 0) * "0" res += "-" else: res += "1" * r2[par][1] if len(r2) <= 1: res = "0" * r2[0][1] else: res = max(0, r2[0][1] - 1) * "0" + "+" + res res += "-" + max(0, r2[-1][1] - 1) * "0" c = b for ch in res: if ch == "0": c += a + b elif ch == "1": c += a + 2 * b elif ch == "+": c += 2 * (a + b) elif ch == "-": c += 2 * a + b print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER NUMBER IF BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR BIN_OP STRING VAR VAR NUMBER VAR STRING VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER STRING VAR STRING VAR BIN_OP STRING VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER STRING STRING VAR VAR BIN_OP STRING BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER STRING ASSIGN VAR VAR FOR VAR VAR IF VAR STRING VAR BIN_OP VAR VAR IF VAR STRING VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR STRING VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR STRING VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
T = int(input()) for t in range(T): n, a, b = map(int, input().split()) A = list(map(int, list("0" + input() + "0"))) DP = [[(10**18) for i in range(2)] for j in range(n + 1)] DP[0][0] = b for i in range(1, n + 1): if A[i + 1] == 0 and A[i] == 0: DP[i][0] = min(DP[i - 1][0] + a + b, DP[i - 1][1] + 2 * a + b) DP[i][1] = min(DP[i - 1][0] + 2 * a + 2 * b, DP[i - 1][1] + a + 2 * b) print(DP[n][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
n = int(input()) for i in range(n): raw = input().split() n, a, b = [int(x) for x in raw] s = input() x = n y = n + 1 count = 0 check_1 = [] check_0 = [] count_0 = 0 status = [] for i in range(n - 1): if s[i] == "1" and s[i + 1] == "1": count += 1 elif s[i] == "1" and s[i + 1] == "0": count += 1 check_1.append(count) count = 0 status.append(i + 1) elif s[i] == "0" and s[i + 1] == "0": count_0 += 1 else: count_0 += 1 check_0.append(count_0) count_0 = 0 status.append(i + 1) ev = 2 * a / b bilist = [] for i in range(len(s)): bilist.append(s[i]) for i in range(len(status) // 2 - 1): if status[2 * (i + 1)] - status[2 * i + 1] - 1 < ev: bilist[status[2 * i + 1] : status[2 * (i + 1)]] = ["1"] * ( -status[2 * i + 1] + status[2 * (i + 1)] ) x = n y = n + 1 count = 0 for i in range(n - 1): if bilist[i] == "1" and bilist[i + 1] == "1": count += 1 if bilist[i] == "1" and bilist[i + 1] == "0": count += 1 x += 2 y += count + 1 count = 0 print(str(x * a + y * b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP LIST STRING BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
for _ in range(int(input())): n, a, b = map(int, input().split()) s = input() if "1" not in s: print((n + 1) * b + a * n) else: cg = 0 for i in range(len(s) - 1): if s[i] == "1" and s[i - 1] == "0": cg += 1 if s[i] == "1": cg += 1 ans = (n + 1) * b + n * a + cg * b c = 0 D = [] F = 1 for i in s: if i == "0": c += 1 elif F: ans += max(0, a) c = 0 F = 0 else: ans += max(0, min(2 * a, (c - 1) * b)) c = 0 ans += max(0, a) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $[0, n]$ on $OX$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $(x, x + 1)$ with integer $x$. So we can represent the road as a binary string consisting of $n$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $1$ unit with supporting pillars in each integer point (so, if we are responsible for $[0, n]$ road, we must install $n + 1$ pillars). But on crossroads we should lift the pipeline up to the height $2$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $[x, x + 1]$ with integer $x$ consisting of three parts: $0.5$ units of horizontal pipe + $1$ unit of vertical pipe + $0.5$ of horizontal. Note that if pipeline is currently on height $2$, the pillars that support it should also have length equal to $2$ units. [Image] Each unit of gas pipeline costs us $a$ bourles, and each unit of pillar β€” $b$ bourles. So, it's not always optimal to make the whole pipeline on the height $2$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $1$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. -----Input----- The fist line contains one integer $T$ ($1 \le T \le 100$) β€” the number of queries. Next $2 \cdot T$ lines contain independent queries β€” one query per two lines. The first line contains three integers $n$, $a$, $b$ ($2 \le n \le 2 \cdot 10^5$, $1 \le a \le 10^8$, $1 \le b \le 10^8$) β€” the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $s$ ($|s| = n$, $s_i \in \{0, 1\}$, $s_1 = s_n = 0$) β€” the description of the road. It's guaranteed that the total length of all strings $s$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $T$ integers β€” one per query. For each query print the minimum possible cost of the constructed pipeline. -----Example----- Input 4 8 2 5 00110010 8 1 1 00110010 9 100000000 100000000 010101010 2 5 1 00 Output 94 25 2900000000 13 -----Note----- The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: [Image] The optimal (and the only possible) pipeline for the third query is shown below: [Image] The optimal pipeline for the fourth query is shown below: [Image]
for _ in range(int(input())): n, a, b = list(map(int, input().split())) s = input() if s == "0" * n: print(a * n + b * n + b) continue ans = 0 ind = 0 ind2 = n - 1 pref = 0 suf = 0 while ind2 >= 0 and s[ind2] == "0": ind2 -= 1 suf += 1 while ind < n and s[ind] == "0": ind += 1 pref += 1 while ind <= ind2: if s[ind] == "0": size = 0 while ind <= ind2 and s[ind] == "0": ind += 1 size += 1 ans += min((size - 1) * b + size * a + 2 * a, size * a + 2 * b * (size - 1)) else: size = 0 while ind <= ind2 and s[ind] == "1": ind += 1 size += 1 ans += (size + 1) * b * 2 + size * a ans += pref * b + pref * a + a ans += suf * b + suf * a + a print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
for _ in range(int(input())): l1 = input() l2 = input() if l1[0] == "#": if l2[0] == "#": print("No") continue else: start = 2 elif l2[0] == "#": start = 1 else: start = 0 switch = 0 i = f = 0 while i < len(l1): a, b = l1[i], l2[i] if a == "#" and b == "#": f = 1 break elif start == 0 and (a == "#" or b == "#"): if a == "#": start = 2 else: start = 1 elif start == 1 and a == "#": start = 2 switch += 1 elif start == 2 and b == "#": start = 1 switch += 1 i += 1 if f == 1: print("No") else: print("Yes") print(switch)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR STRING VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR STRING VAR STRING IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
for _ in range(int(input())): l1, l2, simp = input(), input(), 0 for i in range(len(l1)): if l1[i] == "#" and l2[i] == "#": print("No") simp = 1 break if simp == 0: cnt, cake = 0, 0 for i in range(len(l2)): if cake == 0: if l2[i] == "#": f = 1 cake = 1 elif l1[i] == "#": f = 0 cake = 1 elif f == 1: if l1[i] != ".": cnt += 1 f = 0 else: pass elif l2[i] != ".": cnt += 1 f = 1 else: pass print("Yes", cnt, sep="\n")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING VAR STRING
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
for t in range(int(input())): s = [] s.append(input()) s.append(input()) n = len(s[0]) flag = [0, 0] count = [0, 0] lane = 0 i = 0 switch = 0 if s[0][0] == "#": flag[0] = 1 else: while i < n - 1: x = (lane + 1) % 2 if s[lane][i + 1] == ".": i += 1 switch = 0 continue elif s[x][i] == ".": lane = x count[0] += 1 if switch == 0: switch = 1 else: flag[0] = 1 break continue elif s[x][i + 1] == ".": lane = x count[0] += 1 i += 1 switch = 0 continue else: flag[0] = 1 break lane = 1 i = 0 switch = 0 if s[1][0] == "#": flag[1] = 1 else: while i < n - 1: x = (lane + 1) % 2 if s[lane][i + 1] == ".": i += 1 switch = 0 continue elif s[x][i] == ".": lane = x count[1] += 1 if switch == 0: switch += 1 else: flag[1] = 1 break continue elif s[x][i + 1] == ".": lane = x count[1] += 1 i += 1 switch = 0 continue else: flag[1] = 1 break if flag[0] == 1 and flag[1] == 1: print("No") elif flag[0] == 1 and flag[1] == 0: print("Yes") print(count[1]) elif flag[0] == 0 and flag[1] == 1: print("Yes") print(count[0]) else: print("Yes") print(min(count))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
t = int(input()) while t: l1 = input() l2 = input() n = len(l1) def minGravity(s1, s2): n = len(s1) arr = [0] * 2 arr[0] = s1 arr[1] = s2 s1Dirty = n s2Dirty = n for i in range(n): if s1[i] == "#": s1Dirty = i break for i1 in range(n): if s2[i1] == "#": s2Dirty = i1 break if s1Dirty == n or s2Dirty == n: return 0 lane = 0 if s1Dirty > s2Dirty: lane = 0 swiches = 0 for i in range(n): if arr[lane][i] == "#": swiches += 1 lane = lane ^ 1 return swiches lane = 1 swiches = 0 for i in range(n): if arr[lane][i] == "#": swiches += 1 lane = lane ^ 1 return swiches def possible(s1, s2): for i in range(len(s1)): if s1[i] == "#" and s1[i] == s2[i]: return False return True if not possible(l1, l2): print("No") else: print("Yes") print(minGravity(l1, l2)) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
ma = 10**6 for _ in range(int(input())): s1 = input() s2 = input() l1 = [ma for i in range(len(s1))] l2 = [ma for i in range(len(s2))] if s1[0] == ".": l1[0] = 0 if s2[0] == ".": l2[0] = 0 for i in range(len(s1)): if l1[i] < ma and s1[i] == ".": if s2[i] == ".": l2[i] = min(l2[i], 1 + l1[i]) if i < len(s1) - 1: if s1[i + 1] == ".": l1[i + 1] = min(l1[i + 1], l1[i]) if s2[i + 1] == ".": l2[i + 1] = min(l2[i + 1], l1[i] + 1) if l2[i] < ma and s2[i] == ".": if s1[i] == ".": l1[i] = min(l1[i], 1 + l2[i]) if i < len(s2) - 1: if s1[i + 1] == ".": l1[i + 1] = min(l1[i + 1], l2[i] + 1) if s2[i + 1] == ".": l2[i + 1] = min(l2[i + 1], l2[i]) if l1[-1] >= ma and l2[-1] >= ma: print("No") else: print("Yes") print(min(l1[-1], l2[-1]))
ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
def solve(one, two): dis = 0 switch = 0 on = one notOn = two possible = True while dis <= N - 1 and possible: if on[dis] == ".": dis += 1 elif notOn[dis] == ".": dis += 1 switch += 1 on, notOn = notOn, on else: possible = False if possible == False: return -1 else: return switch for _ in range(int(input())): first = input() second = input() N = len(first) if "#" not in first: print("Yes") print(0) elif "#" not in second: print("Yes") print(0) else: p = solve(first, second) q = solve(second, first) if p + q == -2: print("No") else: print("Yes") if p < 0: print(q) elif q < 0: print(p) else: print(min(p, q))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR 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 STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
t = int(input()) for _ in range(t): l1 = list(input()) l2 = list(input()) flag = 0 for i in range(len(l1)): if l1[i] == l2[i] == "#": flag = 1 break if flag == 1: print("No") else: print("Yes") dp1 = [0] * (len(l1) + 1) dp1[0] = 0 dp2 = [0] * (len(l1) + 1) dp2[0] = 0 for i in range(1, len(l1) + 1): if l1[i - 1] == "#": dp1[i] = 10**18 else: dp1[i] = min(dp1[i - 1], dp2[i - 1] + 1) if l2[i - 1] == "#": dp2[i] = 10**18 else: dp2[i] = min(dp2[i - 1], dp1[i - 1] + 1) print(min(dp1[len(l1)], dp2[len(l1)]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
for h in range(int(input())): L1 = input() L2 = input() L1obs = [(0) for i in range(len(L1))] L2obs = [(0) for i in range(len(L2))] L1counter = 0 L2counter = 0 valid = 1 for i in range(1, len(L1) + 1): if L1[-i] == "." and L2[-i] == ".": L1counter += 1 L2counter += 1 elif L1[-i] == "#" and L2[-i] == "#": valid = 0 break elif L1[-i] == ".": L1counter += 1 L2counter = 0 else: L2counter += 1 L1counter = 0 L1obs[-i] = L1counter L2obs[-i] = L2counter if valid == 0: print("No") continue currentIndex = 0 currentArray = 1 i = 0 jumpCount = 0 extra = 1 if L1obs[i] > L2obs[i]: currentArray = 1 else: currentArray = 2 while i <= len(L1) - 1: if currentArray == 1: i += L1obs[i] + 1 currentArray = 2 else: i += L2obs[i] + 1 currentArray = 1 if i > len(L1): break jumpCount += 1 print("Yes") print(jumpCount)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
t = int(input()) while t: t = t - 1 a = input() b = input() c = 0 f = 0 s = 0 for i in range(len(a)): if a[i] == "#" and c != "b": if c == 0: c = "b" elif b[i] == "#": f = 1 break else: s = s + 1 c = "b" if b[i] == "#" and c != "a": if c == 0: c = "a" elif a[i] == "#": f = 1 break else: s = s + 1 c = "a" if f == 0: print("Yes") print(s) else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING IF VAR VAR STRING VAR STRING IF VAR NUMBER ASSIGN VAR STRING IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
T = int(input()) for i in range(T): s = input() s2 = input() yo = True for i in range(len(s)): if s[i] == s2[i] == "#": yo = False if yo == False: print("No") else: swaps = 0 first = -1 second = -1 for i in range(len(s)): if s[i] == "#": first = i break for i in range(len(s)): if s2[i] == "#": second = i break if "#" not in s or "#" not in s2: swaps = 0 elif first > second: curr_lane = 0 for i in range(len(s)): if s[i] == "#" and curr_lane == 0: swaps += 1 curr_lane = 1 - curr_lane elif s2[i] == "#" and curr_lane == 1: swaps += 1 curr_lane = 1 - curr_lane else: curr_lane = 1 for i in range(len(s)): if s[i] == "#" and curr_lane == 0: swaps += 1 curr_lane = 1 - curr_lane elif s2[i] == "#" and curr_lane == 1: swaps += 1 curr_lane = 1 - curr_lane print("Yes") print(swaps)
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 NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF STRING VAR STRING VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
for _ in range(int(input())): a = input() b = input() l = len(a) x = a.find("#") y = b.find("#") if x == -1 or y == -1: print("Yes") print("0") continue if a[-1] == "#" and b[-1] == "#" or a[0] == "#" and b[0] == "#": print("No") continue if x == y: count1 = 0 current = 0 for i in range(l - 1): if current: if b[i + 1] == "#": if a[i + 1] == ".": current = 0 count1 += 1 elif a[i] == ".": current = 0 count1 += 1 else: count1 = float("inf") break elif a[i + 1] == "#": if b[i + 1] == ".": current = 1 count1 += 1 elif b[i] == ".": current = 1 count1 += 1 else: count1 = float("inf") break count2 = 0 current = 1 for i in range(l - 1): if current: if b[i + 1] == "#": if a[i + 1] == ".": current = 0 count2 += 1 elif a[i] == ".": current = 0 count2 += 1 else: count2 = float("inf") break elif a[i + 1] == "#": if b[i + 1] == ".": current = 1 count2 += 1 elif b[i] == ".": current = 1 count2 += 1 else: count2 = float("inf") break if count1 == count2 == float("inf"): print("No") else: print("Yes") print(min(count1, count2)) continue if x < y: current = 1 else: current = 0 count = 0 for i in range(l - 1): if current: if b[i + 1] == "#": if a[i + 1] == ".": current = 0 count += 1 elif a[i] == ".": current = 0 count += 1 else: print("No") break elif a[i + 1] == "#": if b[i + 1] == ".": current = 1 count += 1 elif b[i] == ".": current = 1 count += 1 else: print("No") break else: print("Yes") print(count)
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 ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR IF VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR IF VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR IF VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
t = int(input()) for _ in range(t): st = [input(), input()] n = len(st[0]) exec = True for i in range(n): if st[0][i] == st[1][i] == "#": print("No") exec = False break if exec: print("Yes") dp = [[(0) for _ in range(n + 1)] for _ in range(2)] for i in range(1, n + 1): if st[0][i - 1] == "#": dp[0][i] = 10**18 else: dp[0][i] = min(dp[0][i - 1], 1 + dp[1][i - 1]) if st[1][i - 1] == "#": dp[1][i] = 10**18 else: dp[1][i] = min(1 + dp[0][i - 1], dp[1][i - 1]) print(min(dp[0][n], dp[1][n]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER 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
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
import sys for _ in range(int(input())): a1 = list(input()) a2 = list(input()) count = 0 DP1 = [0] * len(a1) DP2 = [0] * len(a1) n = len(a1) for i in range(n): if a1[i] == "#" and a2[i] == "#": print("No") count = 1 break if count != 1: if a1[0] == "#": DP1[0] = sys.maxsize if a2[0] == "#": DP2[0] = sys.maxsize for i in range(1, n): if a1[i] != "#": DP1[i] = min(DP1[i - 1], DP2[i - 1] + 1) else: DP1[i] = sys.maxsize if a2[i] != "#": DP2[i] = min(DP2[i - 1], DP1[i - 1] + 1) else: DP2[i] = sys.maxsize print("Yes") re = min(DP1[-1], DP2[-1]) print(re)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
import sys def calculate(m, x): resp = 0 if m[x][0] == "#": return len(m[x]) + 1 for i in range(0, len(m[0]) - 1): if m[x][i + 1] == "#": if m[(x + 1) % 2][i + 1] == "#": return len(m[x]) + 1 else: x = (x + 1) % 2 resp += 1 return resp def solve(m): resp = min(calculate(m, 0), calculate(m, 1)) return -1 if resp == len(m[0]) + 1 else resp T = int(input()) m = ["", ""] for i in range(T): m[0] = input() m[1] = input() resp = solve(m) if resp == -1: print("No") else: print("Yes\n{0}".format(resp))
IMPORT FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR NUMBER STRING RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER STRING RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
for t in range(int(input())): s1 = input() s2 = input() n = len(s1) f = 0 for i in range(n): if s1[i] == s2[i] == "#": f = 1 if f == 1: print("No") continue c = 0 flag = -1 for i in range(0, n): if s1[i] == "#": if flag == 0: c += 1 flag = 1 if s2[i] == "#": if flag == 1: c += 1 flag = 0 print("Yes") print(c)
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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
import sys def perform(): lane_1 = list(input()) lane_2 = list(input()) lane_len = len(lane_1) pos = False min_switch = sys.maxsize lanes = [lane_1, lane_2] if lane_1[0] != "#": t_pos = True t_switch = 0 cur_lane = 0 for i in range(lane_len): if lanes[cur_lane][i] == "#": t = 0 if cur_lane == 0: t = 1 if lanes[t][i] == ".": t_switch += 1 cur_lane = t else: t_pos = False break if t_pos: pos = True min_switch = min(min_switch, t_switch) if lane_2[0] != "#": t_pos = True t_switch = 0 cur_lane = 1 for i in range(lane_len): if lanes[cur_lane][i] == "#": t = 0 if cur_lane == 0: t = 1 if lanes[t][i] == ".": t_switch += 1 cur_lane = t else: t_pos = False break if t_pos: pos = True min_switch = min(min_switch, t_switch) if pos: print("Yes") print(min_switch) else: print("No") for _ in range(int(input())): perform()
IMPORT FUNC_DEF 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 VAR ASSIGN VAR LIST VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
t = int(input()) while t > 0: a = input() b = input() l = len(a) flag = 1 for i in range(l): if a[i] == "#" and b[i] == "#": flag = 0 break if flag == 1: if a[0] == ".": count1 = 0 start = "a" i = 0 while i < l: if start == "a": while a[i] == ".": if i == l - 1: i = l break i = i + 1 if i < l: start = "b" count1 = count1 + 1 elif start == "b": while b[i] == ".": if i == l - 1: i = l break i = i + 1 if i < l: start = "a" count1 = count1 + 1 else: count1 = 9999999 if b[0] == ".": count2 = 0 start = "b" i = 0 while i < l: if start == "a": while a[i] == ".": if i == l - 1: i = l break i = i + 1 if i < l: start = "b" count2 = count2 + 1 elif start == "b": while b[i] == ".": if i == l - 1: i = l break i = i + 1 if i < l: start = "a" count2 = count2 + 1 else: count2 = 9999999 print("Yes") print(min(count1, count2)) if flag == 0: print("No") t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR STRING WHILE VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING WHILE VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR STRING WHILE VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING WHILE VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
t = int(input()) for i in range(t): s1 = input() s2 = input() gr = 0 flag = 99999999999999999 cur = 0 for i in range(len(s1)): if s1[i] == s2[i] == "#": gr = flag break elif s1[i] == "#": if cur == 1: gr += 1 cur = 2 elif s2[i] == "#": if cur == 2: gr += 1 cur = 1 if gr == flag: print("No") else: print("Yes") print(gr)
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
import sys sys.setrecursionlimit(2000000) def dp_jump(lane_num, ind, lane, cache, n): if ind == n - 1 and lane[lane_num][ind] != "#": cache[lane_num][ind] = 0 return cache[lane_num][ind] elif ind >= n: return float("inf") elif cache[lane_num][ind] != None: return cache[lane_num][ind] else: if lane[lane_num][ind] == "#": cache[lane_num][ind] = float("inf") return cache[lane_num][ind] horz = dp_jump(lane_num, ind + 1, lane, cache, n) vertforw = dp_jump(lane_num ^ 1, ind + 1, lane, cache, n) + 1 cache[lane_num][ind] = min(horz, vertforw) return cache[lane_num][ind] test = int(input()) while test: lane = [] lane.append(sys.stdin.readline().rstrip("\n")) lane.append(sys.stdin.readline().rstrip("\n")) n = len(lane[0]) cache = [[None for _ in range(n)] for __ in range(2)] dp_jump(0, 0, lane, cache, n) dp_jump(1, 0, lane, cache, n) if cache[0][0] == float("inf") and cache[1][0] == float("inf"): print("No") else: print("Yes") print(min(cache[0][0], cache[1][0])) test -= 1
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR NONE RETURN VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR STRING RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR IF VAR NUMBER NUMBER FUNC_CALL VAR STRING VAR NUMBER NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
for _ in range(int(input())): s = str(input()) p = str(input()) pos = "" ans = 0 imp = 0 start = 0 for i in range(len(s)): if start == 0: if s[i] == "#" and p[i] != "#": pos = "p" start = 1 elif p[i] == "#" and s[i] != "#": pos = "s" start = 1 elif s[i] == p[i] == "#": imp = 1 break elif pos == "s": if s[i] == "#": if p[i] != "#": pos = "p" ans += 1 else: imp = 1 break else: None elif p[i] == "#": if s[i] != "#": pos = "s" ans += 1 else: imp = 1 break else: None if imp == 1: print("No") else: print("Yes") print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR STRING IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR STRING VAR NUMBER ASSIGN VAR NUMBER EXPR NONE IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR STRING VAR NUMBER ASSIGN VAR NUMBER EXPR NONE IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
T = int(input()) for z in range(T): path1 = list(input()) path2 = list(input()) distances1 = [(999999) for i in range(len(path1))] distances2 = [(999999) for i in range(len(path1))] if path1[0] == "#" and path2[0] == "#": print("No") continue if path1[0] == ".": distances1[0] = 0 if path2[0] == ".": distances2[0] = 0 impossible = False for i in range(0, len(path1) - 1): if path1[i + 1] == "#" and path2[i + 1] == "#": impossible = True break if path1[i + 1] == ".": distances1[i + 1] = min(distances1[i], distances2[i] + 1) else: distances1[i + 1] = 9999999 if path2[i + 1] == ".": distances2[i + 1] = min(distances1[i] + 1, distances2[i]) else: distances2[i + 1] = 9999999 if impossible: print("No") else: print("Yes") print(min(distances1[-1], distances2[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
def sfidx(ls, a): try: return ls.index("#") except: return len(ls) for _ in range(int(input())): L1 = list(input().strip()) L2 = list(input().strip()) cmp = [L1, L2] lin = 0 if sfidx(L1, "#") > sfidx(L2, "#") else 1 sw = 0 block = False for a in range(len(L1)): if cmp[lin][a] == "#": lin = 1 - lin sw += 1 if cmp[lin][a] == "#": block = True break if block: print("No") else: print("Yes") print(sw)
FUNC_DEF RETURN FUNC_CALL VAR STRING RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
INF = 1 << 30 for _ in range(int(input())): l1 = input() l2 = input() d1 = d2 = 0 for i, j in zip(l1, l2): d1, d2 = INF if i == "#" else min(d1, d2 + 1), ( INF if j == "#" else min(d2, d1 + 1) ) ans = min(d1, d2) if ans >= INF: print("No") else: print("Yes") print(ans)
ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR STRING VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR STRING VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
def ret_val(s1, s2): n = len(s1) change = 0 lane = 0 if "#" not in s1: return change if "#" not in s2: return change x1 = s1.index("#") x2 = s2.index("#") if x1 > x2: lane = 1 elif x1 < x2: lane = 2 else: return -1 for i in range(1, n): if s1[i] == s2[i] and s1[i] == "#": return -1 elif lane == 1 and s1[i] == "#": lane = 2 change += 1 elif lane == 2 and s2[i] == "#": lane = 1 change += 1 return change t = int(input()) for _ in range(t): res = ret_val(input(), input()) if res == -1: print("No") else: print("Yes") print(res)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR RETURN VAR IF STRING VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR STRING RETURN NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
for _ in range(int(input())): a = input() b = input() n = len(a) i = -1 flag = True currentlane = 0 jumps = 0 if "#" in a and "#" in b: if a.index("#") > b.index("#"): currentlane = 0 else: currentlane = 1 while i < n - 1: if a[i + 1] == "#" and b[i + 1] == "#": flag = False break if currentlane == 0: if a[i + 1] == ".": i += 1 elif b[i + 1] == ".": jumps += 1 i += 1 currentlane = 1 elif b[i + 1] == ".": i += 1 elif a[i + 1] == ".": jumps += 1 i += 1 currentlane = 0 if flag: print("Yes") print(jumps) else: print("No")
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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF STRING VAR STRING VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
test = int(input()) l1 = [] l2 = [] for j in range(0, test): l1 = input() l2 = input() x = 1 y = 0 c = 0 for i in range(0, len(l1)): if l1[i] == "." and l2[i] == "#": if x == 2: x = 1 y = y + 1 elif l2[i] == "." and l1[i] == "#": if x == 1: x = 2 y = y + 1 elif l1[i] == "." and l2[i] == ".": continue else: print("No") c = 1 break i = i + 1 x = 2 z = 0 for i in range(0, len(l1)): if l2[i] == "." and l1[i] == "#": if x == 1: x = 2 z = z + 1 elif l1[i] == "." and l2[i] == "#": if x == 2: x = 1 z = z + 1 else: continue if c != 1: print("Yes") if y > z: print(z) else: print(y)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
for t in range(int(input())): l1 = list(input().strip()) l2 = list(input().strip()) n = len(l1) l = [[l1[i], l2[i]] for i in range(n)] flag = True if l1[0] == "#" and l2[0] == "#" or l1[-1] == "#" and l2[-1] == "#": print("No") continue ptr = 1 if l[0][1] == "." else 0 ctr = ctr1 = 0 if l[0][0] == l[0][1]: ptr1 = 0 for i in range(n - 1): if l[i + 1][ptr] == "#": if l[i + 1][1 - ptr] == "#": flag = False break else: ctr += 1 ptr = 1 - ptr if l[i + 1][ptr1] == "#": if l[i + 1][1 - ptr1] == "#": flag = False break else: ctr1 += 1 ptr1 = 1 - ptr1 print("Yes\n{}".format(min(ctr, ctr1))) if flag else print("No") else: for i in range(n - 1): if l[i + 1][ptr] == "#": if l[i + 1][1 - ptr] == "#": flag = False break else: ctr += 1 ptr = 1 - ptr print("Yes\n{}".format(ctr)) if flag else print("No")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER STRING NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING IF VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR STRING IF VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING IF VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR VAR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_CALL VAR STRING
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
def test(l1, l2): for i in range(len(l1)): if l1[i] == l2[i] == "#": return "No" return "Yes" def farest(l): i = 0 while i < len(l) and l[i] == ".": i += 1 return i - 1 def minop(l1, l2): c = 0 if l2[0] == ".": if l1[0] == "#": l1, l2 = l2, l1 elif farest(l2) > farest(l1): l1, l2 = l2, l1 for i in range(1, len(l1)): if l1[i] == "#": c += 1 l1, l2 = l2, l1 return c l = [] nb = int(input()) for i in range(nb): l.append([input(), input()]) for i in range(nb): x = test(l[i][0], l[i][1]) print(x) if x == "Yes": print(minop(l[i][0], l[i][1]))
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING RETURN STRING RETURN STRING FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER STRING IF VAR NUMBER STRING ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
def func(cur, other): i = 0 cnt = 0 n = len(cur) while i < n - 1: if cur[i + 1] == ".": i += 1 elif other[i + 1] == "#": return "No" break else: i += 1 cur, other = other, cur cnt += 1 return cnt t = int(input()) for _ in range(t): l1 = input() l2 = input() n = len(l1) if l1[0] == "." and l2[0] == "#": val = func(l1, l2) if val != "No": print("Yes") print(val) elif l1[0] == "#" and l2[0] == ".": val = func(l2, l1) if val != "No": print("Yes") print(val) elif l1[0] == "#" and l2[0] == "#": print("No") else: a = func(l1, l2) b = func(l2, l1) if a == "No" and b == "No": print("No") else: print("Yes") try: print(min(a, b)) except: if a == "No": print(b) else: print(a)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING RETURN STRING VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
def solve(): num_test = int(input()) for i in range(num_test): test_case() def test_case(): lane1 = input() lane2 = input() destination = find_destination(lane1, lane2) if len(destination) == 1: print("No") else: print("Yes") print(destination[1]) def find_destination(lane1, lane2): jump = 0 dest_reached = "Y" current_lane = "" other_lane = "" currnet_pos = 0 first_dirty_lane1 = lane1.find("#") first_dirty_lane2 = lane2.find("#") if first_dirty_lane1 == -1 or first_dirty_lane2 == -1: return ["Y", 0] if first_dirty_lane1 >= first_dirty_lane2: current_lane, other_lane = lane1, lane2 currnet_pos = first_dirty_lane1 else: current_lane, other_lane = lane2, lane1 currnet_pos = first_dirty_lane2 for i in range(currnet_pos, len(lane1)): if current_lane[i] == "#" and other_lane[i] == "#": dest_reached = "N" break if current_lane[i] == "#": current_lane, other_lane = other_lane, current_lane jump += 1 if dest_reached == "N": return ["N"] else: return ["Y", jump] solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER RETURN LIST STRING NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR STRING IF VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR STRING RETURN LIST STRING RETURN LIST STRING VAR EXPR FUNC_CALL VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
for __ in range(int(input())): a1 = input() a2 = input() dp1 = [0] * len(a1) dp2 = [0] * len(a1) count = 0 j = 0 while j < len(a1): if a1[j] == "#" and a2[j] == "#": count = 1 print("No") break j += 1 if count != 1: if a1[0] == "#": dp1[0] = 10**10 if a2[0] == "#": dp2[0] = 10**10 for i in range(1, len(a1)): if a1[i] != "#": dp1[i] = min(dp1[i - 1], dp2[i - 1] + 1) else: dp1[i] = 10**10 if a2[i] != "#": dp2[i] = min(dp2[i - 1], dp1[i - 1] + 1) else: dp2[i] = 10**10 print("Yes") m = min(dp1[-1], dp2[-1]) print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
def test(l1, l2): for i in range(len(l1)): if l1[i] == l2[i] == "#": return "No" return "Yes" def farest(l): i = 0 while i < len(l) and l[i] == ".": i += 1 return i - 1 def switch(x): return "l1" if x == "l2" else "l2" def affect(x, l1, l2): return l1 if x == "l1" else l2 def minop(l1, l2): c = 0 x = ["l1", 0] if l1[0] == "." and l2[0] == ".": x[0] = "l2" if farest(l2) > farest(l1) else "l1" elif l2[0] == ".": x[0] = "l2" l = affect(x[0], l1, l2) for i in range(1, len(l1)): if l[i] == ".": x[1] += 1 else: c += 1 x[0] = switch(x[0]) l = affect(x[0], l1, l2) x[1] += 1 return c nb = int(input()) for i in range(nb): l1 = input() l2 = input() print(test(l1, l2)) print(minop(l1, l2)) if test(l1, l2) == "Yes" else None
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING RETURN STRING RETURN STRING FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF RETURN VAR STRING STRING STRING FUNC_DEF RETURN VAR STRING VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST STRING NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING STRING IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER RETURN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR NONE
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
t = int(input()) for _ in range(t): l1 = list(input()) l2 = list(input()) l = [l1, l2] n = len(l1) ans = [([10**6] * n) for i in range(2)] if l1[0] == ".": ans[0][0] = 0 if l2[0] == ".": ans[1][0] = 0 if l1[0] == "#" and l2[0] == "#": print("No") continue for i in range(1, n): for j in range(2): if l[j][i] == ".": ans[j][i] = min( min( ans[j][i - 1], min(ans[(j + 1) % 2][i - 1], ans[(j + 1) % 2][i]) + 1, ), 10**6, ) ans = min(ans[0][-1], ans[1][-1]) if ans >= 10**6: print("No") else: print("Yes") print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
T = int(input()) for _ in range(0, T): L1 = input() L2 = input() temp = 0 for i in range(0, len(L1)): if L1[i] == "#" and L2[i] == "#": temp = 1 break if temp != 0: print("No") else: ans1 = 4 * len(L1) + 100 ans2 = 4 * len(L1) + 100 if L1[0] == ".": c = 0 ans1 = 0 for i in range(1, len(L1)): if c == 0: if L1[i] == "#": c = 1 ans1 = ans1 + 1 elif L2[i] == "#": c = 0 ans1 = ans1 + 1 if L2[0] == ".": c = 0 ans2 = 0 for i in range(1, len(L2)): if c == 0: if L2[i] == "#": c = 1 ans2 = ans2 + 1 elif L1[i] == "#": c = 0 ans2 = ans2 + 1 print("Yes") print(min(ans1, ans2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
def solve(lanes): lanelength = len(lanes[0]) dp = [[False] * lanelength, [False] * lanelength] for pos in range(lanelength): for lane in (0, 1): if not pos: dp[lane][pos] = lanes[lane][pos] == "." continue else: dp[lane][pos] = (lanes[lane][pos] == ".") & any( [dp[0][pos - 1], dp[1][pos - 1]] ) if lanes[0][pos] == lanes[1][pos] == "#": break if dp[0][-1] or dp[1][-1]: print("Yes") else: print("No") return switches = [0, 0] for start_lane in (0, 1): current_lane = start_lane for pos in range(lanelength): if not dp[current_lane][pos]: current_lane, switches[start_lane] = ( 1 - current_lane, switches[start_lane] + 1, ) print(min(switches)) number_test_cases = int(input()) for _ in range(number_test_cases): lane1 = input() lane2 = input() solve([lane1, lane2])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR NUMBER NUMBER IF VAR ASSIGN VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR STRING FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR STRING IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST NUMBER NUMBER FOR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL 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 EXPR FUNC_CALL VAR LIST VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
for t in range(int(input())): a = input() b = input() n = len(a) if a[0] == "#" and b[0] == "#": print("No") continue f1 = 1 c1 = 0 j = 1 if a[0] == ".": for i in range(1, n): if a[i] == "#" and b[i] == "#": f1 = 0 break if j == 1 and a[i] == ".": continue elif j == 2 and b[i] == ".": continue else: if j == 1: j = 2 else: j = 1 c1 += 1 else: f1 = 0 c2 = 0 j = 2 f2 = 1 if b[0] == ".": for i in range(1, n): if a[i] == "#" and b[i] == "#": f2 = 0 break if j == 1 and a[i] == ".": continue elif j == 2 and b[i] == ".": continue else: if j == 1: j = 2 else: j = 1 c2 += 1 else: f2 = 0 if f1 and f2: print("Yes") print(min(c1, c2)) elif f1 == 1: print("Yes") print(c1) elif f2 == 1: print("Yes") print(c2) else: print("No")
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 STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR STRING IF VAR NUMBER VAR VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR STRING IF VAR NUMBER VAR VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
n = int(input()) for _ in range(n): a = list(input()) b = list(input()) flag = False tr = 0 for i, j in zip(a, b): if tr == 0: if i == "#": tr = 2 if j == "#": tr = 1 if i == "#" and j == "#": print("No") flag = True break if flag: flag = False continue count = 0 if tr == 2: switch = False else: switch = True for i in range(len(a)): if switch: if a[i] == "#": count += 1 switch = False elif b[i] == "#": count += 1 switch = True print("Yes") 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 NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
t = int(input()) for i in range(t): l1 = input() l2 = input() n = len(l1) dp1 = [] dp2 = [] for y in range(n): dp1.append(0) dp2.append(0) c = 0 for k in range(0, n): if l1[k] == "#" and l2[k] == "#": c = 1 break if c == 0: if l1[0] == "#": dp1[0] = 10**10 if l2[0] == "#": dp2[0] = 10**10 if l1[-1] == "#": dp1[-1] = 10**10 if l2[-1] == "#": dp2[-1] = 10**10 for j in range(1, n): if l1[j] == "#": dp1[j] = 10**10 if l2[j] == "#": dp2[j] = 10**10 if l1[j] != "#": dp1[j] = min(dp1[j - 1], dp2[j - 1] + 1) if l2[j] != "#": dp2[j] = min(dp2[j - 1], dp1[j - 1] + 1) x = min(dp1[-1], dp2[-1]) print("Yes") print(x) elif c == 1: print("No")
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 LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
import sys for t in range(int(input())): l1 = input() l2 = input() dp1 = [] dp2 = [] lengt = len(l1) for i in range(lengt): dp1.append(0) dp2.append(0) c = 0 for i in range(len(l1)): if l1[i] == "#" and l2[i] == "#": c = 1 print("No") break if c != 1: if l1[0] == "#": dp1[0] = sys.maxsize if l2[0] == "#": dp2[0] = sys.maxsize for i in range(1, len(l1)): if l1[i] != "#": dp1[i] = min(dp1[i - 1], dp2[i - 1] + 1) else: dp1[i] = sys.maxsize if l2[i] != "#": dp2[i] = min(dp2[i - 1], dp1[i - 1] + 1) else: dp2[i] = sys.maxsize print("Yes") print(min(dp1[-1], dp2[-1]))
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Read problems statements in Mandarin Chinese and Russian. Chef likes to play games a lot. Gravity Guy is one such interesting game. "Gravity Guy is an arcade side-scrolling game in which the player controls Gravity Guy by tapping the screen to switch gravity. The objective in this game is to run as far as possible while avoiding being trapped by obstacles, falling, or flying off the screen. If hit by obstacles, the game is over." Chef is so addicted to this game that each night he dreams of himself being in the game as Gravity Guy. He has two lanes in front of him represented by two strings of equal length named as L_{1} and L_{2}. Each of the two lanes consists of some clean blocks represented by '.' and some dirty blocks represented by '#'. Chef can start running from the beginning of any of the two lanes and cannot step over any dirty block ( '#' ) encountered during his journey. He has to complete his journey by reaching the end block of any of the two lanes. Chef can use the following jumps to reach his destination. Considering chef is at x^{th} block of some lane. He can jump to x+1^{th} block of the same lane. He can switch gravity quickly and jump to x^{th} block of the other lane. He can switch gravity and jump to x+1^{th} block of the other lane. You have to tell him whether he can reach his destination or not. If it is possible for him to reach his destination, then Chef is interested in knowing the minimum number of gravity switches required to reach the destination. ------ Input ------ First line of input contains a single integer T denoting the number of test cases. Each test case consists of 2 lines. First line of each test case contains a string denoting lane L_{1}. Second line of each test case contains a string denoting lane L_{2}. ------ Output ------ For each test case, print "Yes" (without quotes) in the first line if Chef is able to reach the destination followed by a line containing an integer denoting minimum number of gravity switches required to reach to the destination. Print a single line containing the word "No" (without quotes) otherwise. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $1 ≀ |L_{1}| ≀ 2 Γ— 10^{5}, where |S| denotes the length of string S$ $|L_{1}| = |L_{2}|$ ------ Subtasks ------ Subtask 1 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 2 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 200.$ Subtask 3 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ $Only "Yes"/"No" response will be evaluated.$ Subtask 4 (25 points) $Sum of |L_{1}| over all test cases in one file it at most 10^{6}.$ ----- Sample Input 1 ------ 3 #...# .###. #.#.#. .#.#.# #... #... ----- Sample Output 1 ------ Yes 2 Yes 5 No ----- explanation 1 ------ Test case $1$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to $L_{1}$. He continues on $L_{1}$ till block $4$ and then again switch gravity and jump to block $5$ of $L_{2}$. Therefore, he requires total $2$ gravity switches to reach the destination. Test case $2$: Chef will start his journey from $L_{2}$. He switches gravity and jumps to second block of $L_{1}$. He switches gravity and jumps to third block of $L_{2}$. He switches gravity and jumps to fourth block of $L_{1}$. He switches gravity and jumps to fifth block of $L_{2}$. He switches gravity and jumps to sixth block of $L_{1}$. Therefore, he requires total $5$ gravity switches to reach the destination. Test case $3$: Chef cannot start his journey as starting block of both the lanes $L_{1}$ and $L_{2}$ are dirty and he cannot step over them.
UPPER = 0 LOWER = 1 def main(): T = int(input()) for i in range(T): lanes = input(), input() upper_obstacle = lanes[UPPER].find("#") if upper_obstacle == -1: print("Yes") print(0) continue lower_obstacle = lanes[LOWER].find("#") if lower_obstacle == -1: print("Yes") print(0) continue if upper_obstacle < lower_obstacle: current_lane = LOWER else: current_lane = UPPER progress = 0 completion = len(lanes[UPPER]) - 1 switches_used = 0 if lanes[current_lane][progress] == "#": print("No") continue while progress < completion: if lanes[current_lane][progress + 1] == ".": progress += 1 continue elif lanes[(current_lane + 1) % 2][progress + 1] == ".": progress += 1 current_lane = (current_lane + 1) % 2 switches_used += 1 continue else: break else: print("Yes") print(switches_used) continue print("No") 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING WHILE VAR VAR IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR