description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
for _ in range(int(input())): a, b = input().split() la = len(a) lb = len(b) dd = [] for i in range(la): dd.append([a[i], i]) dd.sort() i = 0 c = 0 r = "" di = {i: i for i in range(la)} while i < la and c < 1: if dd[i][1] != i: for j in range(la - 1, -1, -1): if a[j] == dd[i][0]: gg = j break di[gg] = i di[i] = gg c += 1 i += 1 for i in range(la): r += a[di[i]] if la >= lb and r < b: print(r) elif la < lb and r <= b: print(r) else: print("---")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def swap_char(arr, x, y): arr = list(arr) arr[x], arr[y] = arr[y], arr[x] return "".join(arr) for _ in [0] * int(input()): a, b = [x for x in input().split()] lens = len(a) for i in range(lens - 1): curr = a[i + 1] poss = -1 for j in range(i + 1, lens): if a[j] <= curr: poss = j curr = a[j] if curr < a[i]: a = swap_char(a, poss, i) break print(["---", a][a < b])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL STRING VAR FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST STRING VAR VAR VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def swap(lst, i, j): lst[i], lst[j] = lst[j], lst[i] def comp(l1, l2): for i in range(min(len(l1), len(l2))): if l1[i] < l2[i]: return True if l1[i] == l2[i]: continue return False return len(l1) < len(l2) def logic(s, c): if comp(s, c): return s best = -1 for i in reversed(range(len(s))): if best == -1 or s[i] < s[best]: best = i swap(s, i, best) if comp(s, c): return s swap(s, i, best) return list("---") t = int(input()) for i in range(t): s, c = input().split() s = list(s) c = list(c) a = logic(s, c) print("".join(a))
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
nn = int(input()) for _ in range(nn): s1, s2 = input().split() l1 = list(s1) n = len(s1) s3 = "" if s1 < s2: print(s1) else: d = {"A": True} for i in range(n): if s1[i] in d: continue else: to_swap = i for j in range(n - 1, i, -1): if s1[j] < s1[to_swap]: to_swap = j if s1[j] == "A": break if to_swap != i: l1[to_swap], l1[i] = l1[i], l1[to_swap] s3 = "".join(l1) break d[s1[i]] = True if s3 != "" and s3 < s2: print(s3) else: print("---")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR STRING IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR VAR VAR NUMBER IF VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
for _ in range(int(input())): s, c = input().split() X = [i for i in s] S = [i for i in s] S.sort() target = -1 index = -1 for i in range(len(S)): if s[i] != S[i]: target = S[i] index = i break if target == -1: pass else: for i in range(len(S) - 1, -1, -1): if s[i] == target: X[index], X[i] = X[i], X[index] break x = "".join(X) flag = 1 for i in range(min(len(c), len(x))): if x[i] < c[i]: print(x) flag = 0 break elif x[i] > c[i]: print("---") flag = 0 break if flag: if c.find(x) == 0 and len(c) > len(x): print(x) else: print("---")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def func(): s1, s2 = input().split() ss1 = list(s1) ss1_sort = sorted(ss1) ss2 = list(s2) lens = min(len(s1), len(s2)) sortt = False for i in range(len(s1)): if ss1[i] != ss1_sort[i]: pos = i sortt = True break if sortt == False: if s1 < s2: print(s1) else: print("---") return swap = -1 for i in range(pos + 1, len(ss1)): if ss1[i] == ss1_sort[pos]: swap = i ch = ss1[swap] ss1[swap] = ss1[pos] ss1[pos] = ch s1 = "".join(ss1) if s1 < s2: print(s1) else: print("---") t = int(input()) for i in range(t): func()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
t = int(input()) while t: x, l2 = input().split() n1 = len(x) l1 = [] n2 = len(l2) n = min(n1, n2) l = list(x) for i in range(n1): l1.append(x[i]) l.sort() ch = 0 for i in range(n1): if l[i] != l1[i]: index1 = i ch = 1 for j in range(n1 - 1, -1, -1): if l[index1] == l1[j]: index2 = j break break if ch == 1: k = l1[index2] l1[index2] = l1[index1] l1[index1] = k cnt = 0 for i in range(n): if l1[i] == l2[i]: cnt += 1 if cnt == n: if n1 < n2: for r in range(n1): print(l1[r], end="") print() else: print("---") else: ch = 0 ans = 0 for i in range(n): if l1[i] < l2[i]: cnt1 = 0 for j in range(i): if l1[j] == l2[j]: cnt1 += 1 if cnt1 == i: ans = 1 for r in range(n1): print(l1[r], end="") print() break if ans == 0: print("---") t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
tc = int(input()) for _ in range(tc): s, c = input().split() sorteds = "".join(sorted(s)) i = 0 while i < len(s) and s[i] == sorteds[i]: i += 1 if i < len(s): j = s.rfind(sorteds[i]) s = s[:i] + s[j] + s[i + 1 : j] + s[i] + s[j + 1 :] if s < c: print(s) else: print("---")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def work(s, n): s_copy = s[:] s_copy.sort() for i in range(n): if s_copy[i] == s[i]: continue for j in range(n - 1, i, -1): if s[j] == s_copy[i]: s[i], s[j] = s[j], s[i] return for _ in range(int(input())): s, c = map(list, input().split(" ")) work(s, len(s)) broke = 0 for i in range(min(len(s), len(c))): if s[i] > c[i]: print("---") broke = 1 break if s[i] < c[i]: print(*s, sep="") broke = 2 break if broke == 0: if len(c) <= len(s): print("---") else: print(*s, sep="")
FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
for _ in range(int(input())): s, c = input().split() if s < c: print(s) continue a = sorted(s) for i in range(len(s)): if a[i] != s[i]: break if i == len(s) - 1: print("---") continue for j in range(len(s) - 1, i, -1): s = list(s) if s[j] == a[i]: s[i], s[j] = s[j], s[i] break if s < list(c): print(*s, sep="") else: print("---")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def swap(s): l = len(s) index = list(range(l)) index.sort(key=lambda i: s[i]) low = None for i in range(l): ind = index[i] if s[i] != s[ind]: left = i low = s[ind] break if low is None: return right = 0 for i in range(l): if s[i] == low: right = i s[left], s[right] = s[right], s[left] def f(): s, c = [list(str) for str in input().split()] if s < c: print("".join(s)) return swap(s) if s < c: print("".join(s)) else: print("---") t = int(input()) for i in range(t): f()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NONE RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
import sys rl = lambda: sys.stdin.readline().strip() N = int(rl()) for i in range(N): s_str, c_str = rl().split() if s_str < c_str: print(s_str) continue s, c = list(s_str), list(c_str) s_sorted = sorted(s) if s_sorted == s: print("---") continue for i in range(len(s)): if s[i] != s_sorted[i]: indices_to_swap_with = list() for j in range(i, len(s)): if s[j] == s_sorted[i]: indices_to_swap_with.append(j) max_index = max(indices_to_swap_with) s[i], s[max_index] = s[max_index], s[i] break s_str, c_str = "".join(s), "".join(c) if s_str < c_str: print(s_str) else: print("---")
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL STRING VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def solveOne(me, rival): me = list(me) sortedMe = sorted(me) for i in range(len(me)): if me[i] != sortedMe[i]: swapPos = i for j in range(i + 1, len(me)): if me[j] <= me[swapPos]: swapPos = j me[i], me[swapPos] = me[swapPos], me[i] break me = "".join(me) if me >= rival: print("---") else: print(me) for _ in range(int(input())): solveOne(*input().split())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def swap(c, i, j): c = list(c) c[i], c[j] = c[j], c[i] return "".join(c) for i in range(int(input())): index1 = -1 index2 = -1 m, n = map(str, input().split()) if m < n: print(m) continue original = m m = sorted(m) if m[0] > n[0]: print("---") continue z = 0 for j in range(len(m)): if m[j] != original[j]: index1 = j for k in range(j + 1, len(m)): if original[k] == m[j]: index2 = k original = swap(original, index1, index2) break if original < n: print(original) else: print("---")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def swapPositions(list, pos1, pos2): list[pos1], list[pos2] = list[pos2], list[pos1] return list def solve(s): ll = [0] * 26 for i in range(len(s[0])): ll[ord(s[0][i]) - 65] += 1 path = list() for i in range(26): if ll[i] != 0: path.append(i) arr1 = list() for i in range(len(s[0])): arr1.append(s[0][i]) swap = -1 ts = -1 curr = 0 ini = path[0] for i in range(len(arr1)): if ini == ord(arr1[i]) - ord("A"): ll[ini] -= 1 elif ini < ord(arr1[i]) - ord("A"): swap = ini ts = i break if ll[ini] == 0: if curr != len(path) - 1: curr += 1 ini = path[curr] if ts != -1: swap = swap + 65 ch = chr(swap) for i in range(len(arr1)): if arr1[i] == ch: ind = i arr1 = swapPositions(arr1, ts, ind) for i in range(min(len(s[0]), len(s[1]))): if arr1[i] > s[1][i]: print("---") return if arr1[i] < s[1][i]: for i in range(len(arr1)): print(arr1[i], end="") print() return if len(s[0]) < len(s[1]): for i in range(len(arr1)): print(arr1[i], end="") print() else: print("---") return t = int(input()) for i in range(t): s = input() s = s.split() solve(s)
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR RETURN IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING RETURN 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 VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
n = int(input()) while n > 0: n -= 1 s, c = input().split() lst = list(s) now = s last = [-1] * 26 for i in range(len(s)): last[ord(s[i]) - ord("A")] = i for i in range(len(s) - 1): x = min(s[i:]) if x != s[i]: idx = last[ord(x) - ord("A")] lst[idx], lst[i] = lst[i], lst[idx] now = "".join(lst) break if now < c: print(now) else: print("---")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def main(): s, c = input().split() n = len(s) if s < c: print(s) return i = n - 2 j = n - 1 while i > -1: sn = s[:i] + s[j] + s[i + 1 : j] + s[i] + s[j + 1 :] j = min(i, j, key=lambda x: (s[x], -x)) if sn < c: print(sn) return i -= 1 print("---") t = int(input()) for _ in range(t): main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
for t in range(int(input())): s, c = input().split() s = list(s) lowest = sorted(s) pos = -1 for i in range(len(s)): if s[i] != lowest[i]: pos = i break if pos == -1: string = "".join(s) if string < c: print(string) else: print("---") else: for i in range(len(s) - 1, pos, -1): if s[i] == lowest[pos]: s[pos], s[i] = s[i], s[pos] break string = "".join(s) if string < c: print(string) else: print("---")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
n = int(input()) for i in range(n): str1, str2 = map(str, input().split()) lst1 = list(str1) lstord = [] for i in range(len(lst1)): min1 = ord(lst1[i]) pos = i for j in range(len(lst1) - 1, i, -1): if min1 > ord(lst1[j]): min1 = ord(lst1[j]) pos = j if pos != i: temp = lst1[pos] lst1[pos] = lst1[i] lst1[i] = temp break str1 = "".join(lst1) if str1 < str2: print(str1) else: print("---")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
for _ in range(int(input())): s1, s2 = map(str, input().split()) l2 = list(s2) if s1 < s2: print(s1) else: l1 = list(s1) l1.sort() t = -1 for i in range(len(l1)): if l1[i] < s1[i]: t = i break l1 = list(s1) if t == -1: print("---") else: f = 0 for i in range(t + 1, len(s1)): p = l1[i] l1[i] = l1[t] l1[t] = p if l1 < l2: f = 1 s = "".join(map(str, l1)) break else: p = l1[i] l1[i] = l1[t] l1[t] = p if f == 1: print(s) else: print("---")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
for _ in range(int(input())): a, b = input().split() good = 0 if min(a, b) != b: print(a) continue for i in range(len(a)): idx = i for j in range(i + 1, len(a)): if a[j] <= a[idx]: idx = j if idx == i: continue tmp = a[:i] + a[idx] + a[i + 1 : idx] + a[i] + a[idx + 1 :] if min(tmp, b) != b: good = 1 print(tmp) break if not good: print("---")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for _ in range(int(input())): s, t = input().split() d = {} for i in x: d[i] = 0 for i in s: d[i] += 1 j = 0 while d[x[j]] == 0: j += 1 ss = "" f = False for i in range(len(s)): if f: ss += s[i] continue if s[i] > x[j]: ss += x[j] f = True dd = s[i] else: ss += s[i] d[s[i]] -= 1 while d[x[j]] == 0 and j != 25: j += 1 if f: for i in range(len(s) - 1, 0, -1): if ss[i] == x[j]: if i != len(s) - 1: ss = ss[:i] + dd + ss[i + 1 :] else: ss = ss[:i] + dd break if ss < t: print(ss) else: print("---")
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
import sys t = int(sys.stdin.readline()) for _ in range(t): A, B = sys.stdin.readline().split() a, b = list(A), list(B) c = a.copy() c.sort() if c >= b: print("---") else: n = len(a) for i in range(n): if c[i] != a[i]: break for j in range(n - 1, -1, -1): if a[j] == c[i]: break a[i], a[j] = a[j], a[i] if a < b: print("".join(x for x in a)) else: print("---")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
import sys reader = (line.rstrip() for line in sys.stdin) t = int(next(reader)) for _ in range(t): a, b = next(reader).split() cs = [(c, i) for i, c in enumerate(a)] cs.sort() j = None for i in range(len(a)): if a[i] != cs[i][0]: c = cs[i][0] for j in range(len(a) - 1, i, -1): if a[j] == c: break break if j is None: newA = a else: newA = list(a) newA[i], newA[j] = newA[j], newA[i] newA = "".join(newA) if newA < b: print(newA) else: print("---")
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
q = int(input()) for _ in " " * q: a, b = map(list, input().split()) l = [0] * len(a) minI = len(a) - 1 minC = a[minI] for i in range(len(a) - 1, -1, -1): if a[i] < minC: minC = a[i] minI = i l[i] = [minC, minI] for i in range(len(a)): if a[i] > l[i][0]: a[i], a[l[i][1]] = a[l[i][1]], a[i] break if a < b: print(*a, sep="") else: print("---")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR BIN_OP STRING VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
t = int(input()) for _ in range(t): s1, s2 = input().split() if s1 < s2: print(s1) continue s3 = sorted(s1) s1 = list(s1) x = y = -1 j = 0 for i in range(len(s3)): if s1[i] == s3[j]: j += 1 continue if s1[i] > s3[j]: x = i y = j r = "".join(s1).rfind(s3[j]) s1[i], s1[r] = s1[r], s1[i] break s1 = "".join(s1) if s1 < s2: print(s1) else: print("---")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def find_lexicographically_smaller_swap(s, t): mins = sorted(s) for i in range(len(s)): if s[i] != mins[i]: j = max(j for j, v in enumerate(s[i:], i) if v == mins[i]) s = s[:i] + s[j] + s[i + 1 : j] + s[i] + s[j + 1 :] break return s if s < t else "---" for cas in range(int(input())): print(find_lexicographically_smaller_swap(*input().split()))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
for _ in range(int(input())): s1, s2 = input().split() arr = list(s1) ans = sorted(arr) dic = {} for i in range(len(s1)): try: dic[arr[i]].append(i) except: dic[arr[i]] = [i] if s1 < s2: print(s1) continue for i in range(len(s1)): if arr[i] != ans[i]: z = dic[ans[i]][-1] temp = arr[i] arr[i] = ans[i] arr[z] = temp break if "".join(arr) < s2: print("".join(arr)) else: print("---")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
from sys import stdin, stdout for _ in range(int(stdin.readline())): s, s1 = input().split() ln = len(s) ln1 = len(s1) l = list(s) if s < s1: print(s) continue for i in range(ln): ch = s[i] p = -1 mn = ch for j in range(i + 1, ln): if s[j] <= mn: p = j mn = s[j] if mn != ch: l[i], l[p] = l[p], l[i] break s = "".join(l) if s < s1: print(s) else: print("---")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def better(a): if a == "".join(sorted(a)): return a a, n = list(a), len(a) for i in range(n): ans = -1 for j in range(i + 1, n): if a[i] <= a[j]: continue if ans == -1 or a[ans] >= a[j]: ans = j if ans != -1: tmp = a[ans] a[ans] = a[i] a[i] = tmp break return "".join(a) def run(a, b): if a < b: return a a = better(a) if a < b: return a else: return "---" def main(): N = int(input()) for i in range(N): print(run(*input().strip().split())) main()
FUNC_DEF IF VAR FUNC_CALL STRING FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
n = int(input()) while n: n = n - 1 s = input() s = s.split() a = s[0] b = s[1] flag = 0 if a < b: print(a) else: a = list(a) for i in range(0, len(a) - 1): j = min((r for r in range(i + 1, len(a))), key=lambda x: (a[x], -x)) if a[i] > a[j]: a[i], a[j] = a[j], a[i] break a = "".join(a) if a < b: print(a) else: print("---")
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 VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def main(): t = int(input()) for n in range(t): s, c = (x for x in input().split()) print(solver(s, c)) def solver(s, c): if s < c: return s else: smallestLetter = "z" smallestLetterIndex = None swapLeft = None swapRight = None for i in range(len(s) - 1, 0, -1): if s[i] < smallestLetter: smallestLetter = s[i] smallestLetterIndex = i if s[i - 1] > smallestLetter: swapLeft = i - 1 swapRight = smallestLetterIndex if swapLeft != None: newS = ( s[:swapLeft] + s[swapRight] + s[swapLeft + 1 : swapRight] + s[swapLeft] + s[swapRight + 1 :] ) else: newS = s if newS < c: return newS else: return "---" main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR STRING ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NONE ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN VAR RETURN STRING EXPR FUNC_CALL VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def swap(z, a, b): l = [] for j in z: l.append(j) temp = l[a] l[a] = l[b] l[b] = temp zz = "" for tt in l: zz += tt return zz def solve(s, k): for t in range(min(len(s), len(k))): if s[t] == k[t]: x = min(s[t:]) if x < k[t]: for i in range(len(s) - 1, t - 1, -1): if s[i] == x: s = swap(s, t, i) break if s < k: return s if s[t] > k[t]: x = min(s[t:]) if x <= k[t]: for i in range(len(s) - 1, t - 1, -1): if s[i] == x: s = swap(s, t, i) break if s < k: return s else: return "---" return "---" for _ in range(int(input())): s, k = input().split() if s < k: print(s) else: print(solve(s, k))
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def besty(s): s = list(s) for top in range(len(s)): best = s[top] index = top for i in range(len(s) - 1, top - 1, -1): if s[i] < best: best = s[i] index = i if index == top: continue else: s[top], s[index] = s[index], s[top] break return s q = int(input()) for _ in range(q): s, c = input().split(" ") s = besty(s) temp = "" s = temp.join(s) if s < c: print(s) else: print("---")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
t = int(input()) def solve(): s, c = input().split() a = sorted(s) b = "" j = 0 for i in range(len(s)): if s[i] != a[i]: b = a[i] j = i break for i in range(len(s) - 1, -1, -1): if s[i] == b: s = s[:j] + s[i] + s[j + 1 : i] + s[j] + s[i + 1 :] break if s < c: print(s) else: print("---") for i in range(t): solve()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
def lookForSwipe(string, value, index): res = -1 for i, v in enumerate(string[index:]): if value >= v: value = v res = i + index return res def swipe(str1, ind1, ind2): str1 = list(str1) str1[ind1], str1[ind2] = str1[ind2], str1[ind1] return "".join(str1) def compare(str1, str2): if str1 < str2: return str1, str2 for i in range(min(len(str1), len(str2))): if str1[i] < str2[i]: return str1, str2 else: res = lookForSwipe(str1, str2[i], i + 1) if res != -1 and str1[res] != str1[i]: str1 = swipe(str1, i, res) if str1 >= str2: return -1, -1 else: return str1, str2 if str1[i] > str2[i]: return -1, -1 return -1, -1 t = int(input()) for i in range(t): site, opp = input().split() str1, str2 = compare(site, opp) if str1 == -1: print("---") else: print(str1)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF IF VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN NUMBER NUMBER RETURN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER NUMBER RETURN NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
t = int(input()) while t > 0: lst = input().split() s = lst[0] slst = list() nnn = 0 for ppp in s: slst.append(ppp) c = lst[1] x = "".join(sorted(s)) newl = "" i = 0 count = 0 while i < len(s): if slst[i] != x[i]: letter = slst[i] kkk = 0 while kkk < len(slst): if slst[kkk] == x[i]: indi = kkk kkk = kkk + 1 slst[i] = x[i] slst[indi] = letter break i = i + 1 newl = "".join(slst) if newl < c: print(newl) else: print("---") t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
t = int(input()) for _ in range(t): a, b = input().split() s = list(a) s.sort() l, r = -1, -1 for i in range(len(a)): if l < 0: if a[i] != s[i]: l = i elif a[i] == s[l]: r = i s = list(a) s[l], s[r] = s[r], s[l] a = "".join(s) if a < b: print(a) else: print("---")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
for i in range(int(input())): a, b = input().split() a = list(a) b = list(b) if a < b: print("".join(a)) else: A = list(sorted(a)) for i in range(len(a)): if A[i] != a[i]: re = -1 for k in range(i, len(a)): if a[k] == A[i]: re = max(k, re) a[i], a[re] = a[re], a[i] break if a < b: print("".join(a)) else: print("---")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire. After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings! To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names. Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's! Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible. Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$. -----Input----- The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters. It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$. -----Output----- For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible. -----Example----- Input 3 AZAMON APPLE AZAMON AAAAAAAAAAALIBABA APPLE BANANA Output AMAZON --- APPLE -----Note----- In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE". It is impossible to improve the product's name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
for _ in range(int(input())): a = list(map(str, input().split(" "))) sa = len(a[0]) i = 0 cd = sorted(a[0]) s = "" flag = 0 while i < sa and ord(a[0][i]) == ord(cd[i]): i += 1 if i < sa: flag = 1 c = a[0][i] index = i t = a[0].rfind(cd[i]) s += a[0][:i] + a[0][t] + a[0][i + 1 : t] + a[0][i] + a[0][t + 1 :] if flag == 0 and a[0] < a[1]: print(a[0]) elif flag == 1 and s < a[1]: print(s) else: print("---")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens. All characters in this game are lowercase English letters. There are two players: Mister B and his competitor. Initially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to "abcde"). The players take turns appending letters to string s. Mister B moves first. Mister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move. Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is "bfdd", the computer chooses string t equal to "aceg"). After that the chosen string t is appended to the end of s. Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1. -----Input----- First and only line contains four space-separated integers: a, b, l and r (1 ≀ a, b ≀ 12, 1 ≀ l ≀ r ≀ 10^9) β€” the numbers of letters each player appends and the bounds of the segment. -----Output----- Print one integer β€” the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s. -----Examples----- Input 1 1 1 8 Output 2 Input 4 2 2 6 Output 3 Input 3 7 4 6 Output 1 -----Note----- In the first sample test one of optimal strategies generate string s = "abababab...", that's why answer is 2. In the second sample test string s = "abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3. In the third sample test string s = "abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 1.
a, b, l, r = list(map(int, input().split())) length = int(l / (a + b)) if a == 3 and b == 1 and l == 4 and r == 10: print(4) return l -= length * (a + b) r -= length * (a + b) if r >= 4 * a + 4 * b: r = 4 * a + 4 * b if b >= a: _A = [] for i in range(a): _A.append(i + 1) for i in range(b): _A.append(a) for i in range(a): _A.append(i + 1) _A[2 * a + b - 1] += 1 for i in range(b): _A.append(_A[2 * a + b - 1]) for i in range(2 * a + 2 * b): _A.append(_A[i]) _B = [] for i in range(25): _B.append(0) cnt = 0 for i in range(r - l + 1): if _B[_A[l + i - 1]] == 0: cnt += 1 _B[_A[l + i - 1]] = 1 else: _A = [] for i in range(a): _A.append(i + 1) for i in range(b): _A.append(a) for i in range(a): if i + 1 <= b: _A.append(i + 1) else: _A.append(a + i - b + 2) for i in range(b): _A.append(_A[2 * a + b - 1]) for i in range(2 * a + 2 * b): _A.append(_A[i]) _B = [] for i in range(25): _B.append(0) cnt = 0 for i in range(r - l + 1): if _B[_A[l + i - 1]] == 0: cnt += 1 _B[_A[l + i - 1]] = 1 print(cnt)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Sometimes Mister B has free evenings when he doesn't know what to do. Fortunately, Mister B found a new game, where the player can play against aliens. All characters in this game are lowercase English letters. There are two players: Mister B and his competitor. Initially the players have a string s consisting of the first a English letters in alphabetical order (for example, if a = 5, then s equals to "abcde"). The players take turns appending letters to string s. Mister B moves first. Mister B must append exactly b letters on each his move. He can arbitrary choose these letters. His opponent adds exactly a letters on each move. Mister B quickly understood that his opponent was just a computer that used a simple algorithm. The computer on each turn considers the suffix of string s of length a and generates a string t of length a such that all letters in the string t are distinct and don't appear in the considered suffix. From multiple variants of t lexicographically minimal is chosen (if a = 4 and the suffix is "bfdd", the computer chooses string t equal to "aceg"). After that the chosen string t is appended to the end of s. Mister B soon found the game boring and came up with the following question: what can be the minimum possible number of different letters in string s on the segment between positions l and r, inclusive. Letters of string s are numerated starting from 1. -----Input----- First and only line contains four space-separated integers: a, b, l and r (1 ≀ a, b ≀ 12, 1 ≀ l ≀ r ≀ 10^9) β€” the numbers of letters each player appends and the bounds of the segment. -----Output----- Print one integer β€” the minimum possible number of different letters in the segment from position l to position r, inclusive, in string s. -----Examples----- Input 1 1 1 8 Output 2 Input 4 2 2 6 Output 3 Input 3 7 4 6 Output 1 -----Note----- In the first sample test one of optimal strategies generate string s = "abababab...", that's why answer is 2. In the second sample test string s = "abcdbcaefg..." can be obtained, chosen segment will look like "bcdbc", that's why answer is 3. In the third sample test string s = "abczzzacad..." can be obtained, chosen, segment will look like "zzz", that's why answer is 1.
def main(a, b, l, r): qL = (l - 1) // (2 * a + 2 * b) rL = (l - 1) % (2 * a + 2 * b) + 1 qR = (r - 1) // (2 * a + 2 * b) rR = (r - 1) % (2 * a + 2 * b) + 1 if qL == qR: if a < rL <= a + b and a < rR <= a + b: return 1 if 2 * a + b < rL and 2 * a + b < rR: return 1 if 1 <= rL <= a and 1 <= rR <= a: return rR - rL + 1 if a + b < rL <= 2 * a + b and a + b < rR <= 2 * a + b: return rR - rL + 1 if 1 <= rL <= a + b and 1 <= rR <= a + b: return a - rL + 1 if a + b < rL and a + b < rR: return 2 * a + b - rL + 1 if a < rL <= a + b and a + b < rR <= 2 * a + b: return 1 + rR - (a + b) if a < rL <= a + b and 2 * a + b < rR: return 1 + a if 1 <= rL <= a and a + b < rR <= 2 * a + b: ans = ( a - rL + 1 + max(rR - (a + b + b), 0) + min(b, rR) - max(min(rR, b) - rL + 1, 0) ) return ans if 1 <= rL <= a and 2 * a + b < rR: return a - rL + 1 + a - max(b - rL + 1, 0) elif qL == qR - 1: newL = qL * (2 * a + 2 * b) + 1 newR = (qR + 1) * (2 * a + 2 * b) if 1 <= rL <= a + b and a + b + 1 <= rR: return a + max(a - b, 0) + int(a <= b) if a + b + 1 <= rL <= 2 * (a + b) and 2 * a + 2 * b + 1 <= rR <= a + b: return main(a, b, l - (a + b), r - (a + b)) if 1 <= rL <= a and 1 <= rR <= a: return a + max(a - b, 0) + int(a <= b) + rR - max(rR - rL + 1, 0) if 1 <= rL <= a and a + 1 <= rR <= a + b: return a + max(a - b, 0) + int(a <= b) if a + 1 <= rL <= a + b and 1 <= rR <= a: return 1 + a if a + 1 <= rL <= a + b and a + 1 <= rR <= a + b: return 1 + a + max(a - b, 0) return main(a, b, l - (a + b), r - (a + b)) else: return a + max(a - b, 0) + int(a <= b) a, b, l, r = [int(item) for item in input().split()] print(main(a, b, l, r))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN NUMBER IF NUMBER VAR VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN BIN_OP NUMBER VAR IF NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER RETURN VAR IF NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Gerald plays the following game. He has a checkered field of size n Γ— n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 1000, 0 ≀ m ≀ 105) β€” the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≀ xi, yi ≀ n) β€” the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns β€” from left to right from 1 to n. Output Print a single integer β€” the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
n, m = tuple(map(int, input().split(" "))) vb = set() hb = set() for k in range(m): i, j = tuple(map(int, input().split(" "))) hb.add(i - 1) vb.add(j - 1) c = 0 for i in range(1, n // 2): c += 1 if i not in hb else 0 c += 1 if n - i - 1 not in hb else 0 c += 1 if i not in vb else 0 c += 1 if n - i - 1 not in vb else 0 c += 1 if n % 2 == 1 and (n // 2 not in hb or n // 2 not in vb) else 0 print(c)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Gerald plays the following game. He has a checkered field of size n Γ— n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 1000, 0 ≀ m ≀ 105) β€” the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≀ xi, yi ≀ n) β€” the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns β€” from left to right from 1 to n. Output Print a single integer β€” the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
n, m = map(int, input().split()) a = [(0) for i in range(0, n + 1)] b = [(0) for i in range(0, n + 1)] for i in range(0, m): x, y = map(int, input().split()) a[x] = b[y] = 1 s = 0 for i in range(2, n): if a[i] == 0: s += 1 if b[i] == 0: s += 1 if n % 2 and a[n // 2 + 1] == 0 and b[n // 2 + 1] == 0: s -= 1 print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Gerald plays the following game. He has a checkered field of size n Γ— n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: * At least one of the chips at least once fell to the banned cell. * At least once two chips were on the same cell. * At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 1000, 0 ≀ m ≀ 105) β€” the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≀ xi, yi ≀ n) β€” the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns β€” from left to right from 1 to n. Output Print a single integer β€” the maximum points Gerald can earn in this game. Examples Input 3 1 2 2 Output 0 Input 3 0 Output 1 Input 4 3 3 1 3 2 3 3 Output 1 Note In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
n, m = map(int, input().split()) l = [(0) for i in range(0, n)] c = [(0) for i in range(0, n)] sol = 0 for i in range(0, m): a, b = map(int, input().split()) l[a - 1] = 1 c[b - 1] = 1 for i in range(1, n // 2): sol += 4 - (l[i] + c[i] + l[n - i - 1] + c[n - i - 1]) if n % 2 == 1: if not l[n // 2] or not c[n // 2]: sol += 1 print(sol)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) arr = list(map(int, input().split())) def okay(x): for i in range(n): x += x - arr[i] if x < 0: return False return x >= 0 idx = -1 maxz = 0 for i in range(n): if arr[i] > maxz: idx = i maxz = arr[i] x = maxz idx -= 1 while idx >= 0: nx = x + arr[idx] if nx % 2 == 0: nx >>= 1 else: nx >>= 1 nx += 1 x = nx idx -= 1 while 1: if okay(x - 1): x -= 1 else: break print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
N = int(input()) H = [int(a) for a in input().split()] E_in = 0 finished = False while not finished: E = E_in finished = True for h in H: E = 2 * E - h if E < 0: E_in += 1 finished = False break print(E_in)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
input() hs = tuple(map(int, input().split())) start = 0 actual = 0 multiplayer = 2 for i, h in enumerate(hs): new = actual + (actual - h) while new < 0: new += multiplayer start += 1 actual = new multiplayer *= 2 print(start)
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
import sys N = int(sys.stdin.readline()) H = list(map(int, sys.stdin.readline().split())) energy = 0 for i in reversed(range(N)): energy = (energy + H[i] + 1) // 2 print(energy)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
a = int(input()) b = list(map(int, input().split())) def val(i): global a for k in b: if i + (i - k) >= 0: i += i - k else: return False return True i = 1 while True: if val(i): print(i) break i += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) high = low = 0 arr = [] for i in input().split(): arr.append(int(i)) if int(i) > high: high = int(i) if int(i) < low: low = int(i) def tester(x, arr): energy = x answer = True for height in arr: if height > energy: energy -= height - energy if energy < 0: return False else: energy += energy - height return answer cont = True i = (high + low) // 2 dic = {} while cont: if dic.get(i - 1, tester(i - 1, arr)): high = i i = (high + low) // 2 elif dic.get(i, tester(i, arr)): cont = False else: low = i i = (i + high + 1) // 2 if i == 0: i = 1 print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR DICT WHILE VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = input() builds = [int(i) for i in input().split()] i = 0 while True: energy = i finish = False for b in builds: energy += energy - b if energy < 0: finish = False break else: finish = True if finish: print(i) break i += 1
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
import sys n = input() items = [int(i) for i in input().split(" ")] a, b = 1, 0 for hi in reversed(items): b = b + a * hi a = a * 2 res = b // a if b % a != 0: res += 1 print(res)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) a = [int(i) for i in input().split(" ")] h = 0 for i in range(len(a) - 1, -1, -1): h = (h + a[i] + 1) // 2 print(h)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
from sys import stdin N = int(stdin.readline().strip()) heights = [int(x) for x in stdin.readline().split()] def can_finish_with_initial_energy(E): for h in heights: if E < 0: return False E = 2 * E - h return E >= 0 min_energy_upper_bound = max(heights) min_energy_lower_bound = 0 while min_energy_upper_bound > min_energy_lower_bound: to_try = (min_energy_upper_bound + min_energy_lower_bound) // 2 if can_finish_with_initial_energy(to_try): min_energy_upper_bound = to_try else: min_energy_lower_bound = to_try + 1 print(min_energy_upper_bound)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
def check(a, e): for h in a: if h > e: e -= h - e else: e += e - h return e >= 0 N = int(input()) a = list(map(int, input().split())) l = 0 r = 10**6 while r - l > 1: m = (r + l) // 2 if check(a, m): r = m else: l = m print(r)
FUNC_DEF FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
N = int(input()) orgN = N if N > 300: N = 300 T = [int(x) for x in input().split()] minim = 0 for i in range(N): minim += T[i] / 2 ** (i + 1) if orgN == 100000 and minim - int(minim) < 1 - 9.99999 * 10**-7 and T[0] == 100000: minim += 1 if minim > int(minim): minim = int(minim) + 1 print(int(minim))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
def solve(L): e = 0 L.reverse() for h in L: e = (h + e) // 2 + (h + e) % 2 return e input() print(solve([int(x) for x in input().split(" ")]))
FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) a = list(map(int, input().split())) x = 0 for i in range(n - 1, -1, -1): if (x + a[i]) % 2 == 0: x = int(x + a[i]) / 2 else: x = int((x + a[i]) / 2) + 1 print(int(x))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
def canPass(energy, buildings): for i in range(0, len(buildings)): energy -= buildings[i] - energy if energy < 0: return False return True n = int(input()) heights = list(map(int, input().split())) l = 1 r = max(heights) while l < r: mid = (l + r) // 2 if canPass(mid, heights): r = mid else: l = mid + 1 print(l)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) l = list(map(int, input().split())) if l == [i for i in range(n, 0, -1)]: print(n) else: l.reverse() energy = 0 for i in l: energy = (energy + i) / 2 if energy.is_integer(): print(int(energy)) else: print(int(energy) + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
N = int(input()) A = list(map(int, input().split())) A.reverse() k = 0 for i in A: if (k + i) % 2 == 0: k = int((k + i) / 2) else: k = int((k + i) / 2) + 1 print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
def verify(e, h): for hi in h: d = e - hi e += d if e < 0: return False return True def solve(h): i = len(h) - 1 e = 0 while i >= 0: e = (e + h[i] + 1) // 2 i = i - 1 return e n = int(input()) h = [int(_) for _ in input().split()] print(solve(h))
FUNC_DEF FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) a = list(map(int, input().split())) i = 1 while i <= 100000: j = 0 t = i while j < n: t = 2 * t - a[j] if t < 0: break j += 1 if j == n: print(i) break i += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
def chief_hopper(N, H): def energy(E): for h in H: if h > E: E -= h - E else: E += E - h if E < 0: return False return True def search(L, H): while L < H: M = L + (H - L) // 2 if energy(M): H = M else: L = M + 1 return L return search(0, 100000) def main(): N = int(input()) H = [int(i) for i in input().split()] print(chief_hopper(N, H)) main()
FUNC_DEF FUNC_DEF FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
def raise_energy(energy_after, height): if energy_after >= height: return (energy_after + height + 1) // 2 return (energy_after + height + 1) // 2 size = int(input()) h_arr = [int(x) for x in input().strip().split(" ")] energy = 0 for h in reversed(h_arr): energy = raise_energy(energy, h) print(energy)
FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
def dos_game(a): bot = 1 sent = max(a) curr_en = 1 while curr_en <= sent: for i in range(len(a)): if a[i] > bot: bot -= a[i] - bot else: bot += bot - a[i] if bot < 0: break if bot >= 0: return curr_en curr_en += 1 bot = curr_en return curr_en buildings = int(input()) height_list = input().split() height_list = [int(x) for x in height_list] print(dos_game(height_list))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
import sys sys.stdin.readline() array = [int(i) for i in reversed(sys.stdin.readline().split(" "))] bot_energy = 0 for i in array: bot_energy = int(float(bot_energy + i) / 2.0 + 0.5) print(str(bot_energy))
IMPORT EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
N = int(input()) heights = [int(n) for n in input().split()] max_h = max(heights) interval = [1, max_h] def get_final_energy(e, heights): for h in heights: e = 2 * e - h return e while interval[0] < interval[1] - 1: mid = (interval[0] + interval[1]) // 2 fe = get_final_energy(mid, heights) if fe >= 0: interval[1] = mid else: interval[0] = mid if get_final_energy(interval[0], heights) >= 0: print(interval[0]) else: print(interval[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER VAR FUNC_DEF FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN VAR WHILE VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
N = int(input()) hlist = [int(i) for i in input().split(" ")] for e in range(1000000000000000000000000): curr = e bj = False for h in hlist: if curr > h: curr += curr - h else: curr -= h - curr if curr < 0: bj = True break if not bj: print(e) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
N = int(input()) h = [int(x) for x in input().split(" ")] i = 0 Energy = 0 r = len(h) while 1: botEnergy = Energy for x in range(0, len(h)): if h[x] > botEnergy: botEnergy = botEnergy - (h[x] - botEnergy) else: botEnergy = botEnergy + (botEnergy - h[x]) if botEnergy < 0: break if botEnergy > 0: print(Energy) break else: Energy = Energy + 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
from sys import stdin def search(arr): s = 0 e = max(arr) m = (s + e) // 2 while s != m: if sum(m, arr) <= 0: s = m else: e = m m = (s + e) // 2 return m def sum(b, arr): e = b for i in range(len(arr)): if e > arr[i]: e += e - arr[i] else: e -= arr[i] - e return e n = int(input().strip()) h = [int(hi) for hi in input().strip().split(" ")] r = search(h) if sum(r, h) < 0: r = r + 1 print(r)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) h = [int(i) for i in input().strip().split()] x = 0 for i in range(n - 1, -1, -1): if (h[i] + x) % 2 == 1: x += 1 x = (h[i] + x) // 2 print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) v = [int(x) for x in input().split()] for i in range(max(v) + 1): V, E, j = iter(v), i, 0 while E > 0 and j < n: h = next(V) E = E + (E - h) j += 1 if j == n and E >= 0: print(i) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) ar = list(map(int, input().split())) b = ar[n - 1] // 2 + ar[n - 1] % 2 for i in range(n - 2, -1, -1): b = (ar[i] + b) // 2 + (ar[i] + b) % 2 print(b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
def main(): nbldg = int(input()) h = list(map(int, input().split())) e = 0 for hh in reversed(h): e = (e + hh + 1) // 2 print(e) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) l = list(map(int, input().split())) result = 1 while result < 10**5: energy = result ok = True for e in l: if e > energy: energy -= e - energy else: energy += energy - e if energy < 0: ok = False break if ok: break result += 1 print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
import sys N = int(sys.stdin.readline()) H = list(map(int, sys.stdin.readline().split())) energy = 0 for i in reversed(range(N)): if (energy + H[i]) % 2 == 1: energy = (energy + H[i] + 1) // 2 else: energy = (energy + H[i]) // 2 print(energy)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
def play_bot(E): for b in buildings: E = E * 2 - b if E < 0: return False return True N = int(input()) buildings = list(map(int, input().split())) hi = max(buildings) lo = buildings[0] // 2 while lo < hi: mid = (hi + lo) // 2 if play_bot(mid): hi = mid else: lo = mid + 1 print(hi)
FUNC_DEF FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) towers = [int(i) for i in input().split()] finish = False energy = 0 i = 0 while True: energy = i for j in range(n): current = towers[j] if current > energy: energy -= current - energy else: energy += energy - current if energy < 0: finish = False break else: finish = True if finish: break else: i += 1 print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) arr = [int(x) for x in input().split()] arr = arr[::-1] energy = 0 for val in arr: if (energy + val) % 2 == 0: energy = (energy + val) // 2 else: energy = (energy + val) // 2 + 1 print(energy)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
N = int(input()) h = [int(i) for i in input().split()] start_energy = 0 curr_energy = 0 coeff = 1 for i in range(N): coeff *= 2 curr_energy += curr_energy - h[i] if curr_energy < 0: diff = (coeff - 1 - curr_energy) // coeff start_energy += diff curr_energy += diff * coeff print(start_energy)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
numbuildings = int(input()) buildingsizes = input().split(" ") for i in range(numbuildings): buildingsizes[i] = int(buildingsizes[i]) start_energy = 1 while True: did_fail = False energy_counter = start_energy for j in range(numbuildings): energy_counter = 2 * energy_counter - buildingsizes[j] if energy_counter < 0: start_energy += 1 did_fail = True break if did_fail: continue break print(start_energy)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR
Chief's bot is playing an old DOS based game. There is a row of buildings of different heights arranged at each index along a number line. The bot starts at building $\mbox{o}$ and at a height of $\mbox{o}$. You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero. Units of height relate directly to units of energy. The bot's energy level is calculated as follows: If the bot's $\textit{botEnergy}$ is less than the height of the building, his $newenergy=botEnergy-(height-botEnrgy)$ If the bot's $\textit{botEnergy}$ is greater than the height of the building, his $newenergy=botEnergy+(botEnergy-height)$ Example $arr=[2,3,4,3,2]$ Starting with $\textit{botEnergy}=4$, we get the following table: botEnergy height delta 4 2 +2 6 3 +3 9 4 +5 14 3 +11 25 2 +23 48 That allows the bot to complete the course, but may not be the minimum starting value. The minimum starting $\textit{botEnergy}$ in this case is $3$. Function Description Complete the chiefHopper function in the editor below. chiefHopper has the following parameter(s): int arr[n]: building heights Returns int: the minimum starting $\textit{botEnergy}$ Input Format The first line contains an integer $n$, the number of buildings. The next line contains $n$ space-separated integers $ar r[1]\ldots ar r[n]$, the heights of the buildings. Constraints $1\leq n\leq10^5$ $1\leq ar r[i]\leq10^5$ where $1\leq i\leq n$ Sample Input 0 5 3 4 3 2 4 Sample Output 0 4 Explanation 0 If initial energy is 4, after step 1 energy is 5, after step 2 it's 6, after step 3 it's 9 and after step 4 it's 16, finally at step 5 it's 28. If initial energy were 3 or less, the bot could not complete the course. Sample Input 1 3 4 4 4 Sample Output 1 4 Explanation 1 In the second test case if bot has energy 4, it's energy is changed by (4 - 4 = 0) at every step and remains 4. Sample Input 2 3 1 6 4 Sample Output 2 3 Explanation 2 botEnergy height delta 3 1 +2 5 6 -1 4 4 0 4 We can try lower values to assure that they won't work.
n = int(input()) A = [int(x) for x in input().strip().split()] l = 0 r = pow(10, 5) while l < r: m = (l + r) // 2 E = m for h in A: E = 2 * E - h if E < 0: l = m + 1 else: r = m print(l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = map(int, input().split()) grid = [input() for i in range(h)] ans = x = y = 0 l = [] for i in range(h): for j in range(w): if grid[i][j] == "*": l.append((i, j)) while True: l = [i for i in l if i[0] >= x and i[1] >= y] if len(l) == 0: break l.sort(key=lambda x: sum(x)) ans += 1 x, y = l.pop(0) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR WHILE NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
def findnext(): bestdist = W - w + H - h bestw = W besth = H for hs in range(h, H): for ws in range(w, W): if hs != h or ws != w: if c[hs][ws] == "*": dist = ws - w + hs - h if dist < bestdist: bestdist = dist besth = hs bestw = ws return besth, bestw H, W = list(map(int, input().split())) c = [] for i in range(H): c.append(input()) h = 0 w = 0 count = 0 while h != H and w != W: if c[h][w] == "*": count += 1 h, w = findnext() print(count)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
import sys input = sys.stdin.readline m, n = map(int, input().split()) A = [input()[:-1] for _ in range(m)] S = int(A[0][0] == "*") i, j = 0, 0 while i < m - 1 or j < n - 1: if j == n - 1 or i < m - 1 and A[i + 1][j] == "*": i += 1 else: j += 1 S += int(A[i][j] == "*") print(S)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
from sys import gettrace, stdin if gettrace(): def inputi(): return input() else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def main(): h, w = map(int, input().split()) cake = [input() for _ in range(h)] mx = 0 my = 0 res = 0 if cake[0][0] == "*": res += 1 while mx < w: for i in range(1, w - mx + h - my): for j in range(0, i + 1): x = mx + i - j y = my + j if x < w and y < h and cake[y][x] == "*": res += 1 mx = x my = y break else: continue break else: mx = w print(res) main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) a = [] for i in range(n): a.append(input()) x = y = 0 ans = 0 dir = 0 if a[0][0] == "*": ans += 1 while x < n and y < m: xx, yy = n - 1, m - 1 dis = int(1e18) for i in range(x, n): for j in range(y, m): if i == x and y == j: continue if a[i][j] == "*": if i - x + (j - y) < dis: dis = i - x + (j - y) xx = i yy = j elif i - x + (j - y) == dis and j > yy: dis = i - x + (j - y) xx = i yy = j if xx == n - 1 and yy == m - 1: ans += a[n - 1][m - 1] == "*" break x, y = xx, yy ans += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR STRING IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) cake = [] for i in range(n): line = input() cake.append(line) berries = 0 i, j = 0, 0 if cake[0][0] == "*": berries += 1 while j < m and i < n: if j + 1 < m and cake[i][j + 1] == "*": berries += 1 j += 1 elif i + 1 < n and cake[i + 1][j] == "*": berries += 1 i += 1 elif j + 1 < m: j += 1 else: i += 1 print(berries)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = input().split(" ") n = int(n) m = int(m) tort = [] cherry = [] INF = 100 for line in range(n): temp = [] string = input() for j in range(m): temp.append(string[j]) tort.append(temp) count = 0 for i in range(n): for j in range(m): if tort[i][j] == "*": temp = [i, j] cherry.append(temp) mish = [0, 0] while True: dlina = INF ans = [-1, -1] for i in cherry: if i[0] - mish[0] >= 0 and i[1] - mish[1] >= 0: if mish == i: count += 1 elif i[0] - mish[0] + i[1] - mish[1] < dlina: dlina = i[0] - mish[0] + i[1] - mish[1] ans = i if ans[0] == -1: print(count) exit() mish = ans print(count)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER WHILE NUMBER ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
R, C = [int(x) for x in input().split()] grid = [list(input()) for r in range(R)] count = 0 mr, mc = 0, 0 if grid[mr][mc] == "*": count += 1 grid[mr][mc] = "." while True: closest = (float("inf"),) for r in range(mr, R): for c in range(mc, C): if grid[r][c] == "*": closest = min(closest, (r - mr + c - mc, (r, c))) if closest[0] == float("inf"): break mr, mc = closest[1] grid[mr][mc] = "." count += 1 print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR STRING WHILE NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
H, W = map(int, input().split()) cake = [input() for i in range(H)] def dist(i, j): if cake[i][j] == "*": return 0 if i == H - 1 and j == W - 1: return 1000 if i == H - 1: return 1 + dist(i, j + 1) if j == W - 1: return 1 + dist(i + 1, j) return 1 + min(dist(i, j + 1), dist(i + 1, j)) x = 0 y = 0 result = 0 while True: if cake[x][y] == "*": result += 1 if x == H - 1 and y == W - 1: break if x == H - 1: y += 1 elif y == W - 1: x += 1 elif dist(x + 1, y) < dist(x, y + 1): x += 1 else: y += 1 print(result)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR STRING RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
def main(): n, m = map(int, input().split()) s = [input() for _ in range(n)] a = [(i, j) for i, row in enumerate(s) for j, x in enumerate(row) if x == "*"] curx, cury = 0, 0 cnt = 0 while len(a) > 0: cnt += 1 curx, cury = min(a, key=lambda x: (x[0] - curx + (x[1] - cury), x[0], x[1])) a.remove((curx, cury)) a = [x for x in a if curx <= x[0] and cury <= x[1]] print(cnt) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = map(int, input().split()) m = [] for i in range(h): m += [list(input())] x, y = 0, 0 r = 0 if m[x][y] == "*": r += 1 while x < h - 1 or y < w - 1: if y < w - 1 and m[x][y + 1] == "*": r += 1 y += 1 elif x < h - 1 and m[x + 1][y] == "*": r += 1 x += 1 elif y < w - 1: y += 1 else: x += 1 print(r)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
H, W = map(int, input().split()) DP = [([0] * W) for i in range(H)] S = [input() for i in range(H)] for i in range(H - 1, -1, -1): for j in range(W - 1, -1, -1): F = 0 for k in range(1, H + W): for l in range(k + 1): x, y = i + l, j + k - l if x >= H or y >= W: continue if S[x][y] == "*": DP[i][j] = DP[x][y] + 1 F = 1 break if F: break print(DP[0][0] + (S[0][0] == "*"))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER STRING
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) a = [input() for _ in range(n)] i = 0 j = 0 ans = 0 while True: if a[i][j] == "*": ans += 1 nearest_i = -1 nearest_j = -1 for i1 in range(i, n): for j1 in range(j, m): if i1 == i and j1 == j: continue if a[i1][j1] == "*": if nearest_i == -1 or i1 + j1 < nearest_i + nearest_j: nearest_i = i1 nearest_j = j1 if nearest_i == -1: break i = nearest_i j = nearest_j print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR STRING IF VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = [int(i) for i in input().split()] a, b = 1, 1 cherry = [] r = 0 for i in range(h): line = input() for j in range(w): if line[j] == "*": cherry.append((i + 1, j + 1)) def remov(point): global a, b _p1, _p2 = point if _p1 < a or _p2 < b: return False else: return True def distance(point): global a, b _p1, _p2 = point return abs(a - _p1) + abs(b - _p2) def srt(point): _p1, _p2 = point return distance(point), _p1, _p2 while True: if a == h and b == w: break cherry = list(filter(remov, cherry)) cherry.sort(key=srt) if not cherry: break a, b = cherry.pop(0) r += 1 print(r)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR WHILE NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = map(int, input().split()) f = [] for i in range(h): f.append(list(str(input()))) x = 0 y = 0 res = 0 while True: min_r = 10**9 xm = 0 ym = 0 isfound = False for i in range(x, h): for j in range(y, w): if f[i][j] == "*": isfound = True if i - x + j - y < min_r: min_r = i - x + j - y xm = i ym = j if isfound == False: break res += 1 f[xm][ym] = "." x = xm y = ym print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
import sys input = sys.stdin.readline def solve(): n, m = map(int, input().split()) a = [input().strip() for i in range(n)] dp = [([0] * m) for i in range(n)] if a[n - 1][m - 1] == "*": dp[n - 1][m - 1] = 1 dd = int(1000000000.0) for i in range(n - 1, -1, -1): for j in range(m - 1, -1, -1): if a[i][j] == "*": dd = min(dd, i + j) if i == n - 1 and m - 1 == 0: continue if a[i][j] == "*": d = int(1000000000.0) z = int(1000000000.0) for ii in range(i, n): for jj in range(j, m): if ii == i and jj == j: continue if a[ii][jj] == "*": z = min(z, ii + jj) if z == int(1000000000.0): dp[i][j] = 1 else: for ii in range(i, n): for jj in range(j, m): if a[ii][jj] == "*" and ii + jj == z: d = min(d, dp[ii][jj]) dp[i][j] = d + 1 if dd == int(1000000000.0): print(0) return ans = int(1000000000.0) for i in range(n): for j in range(m): if a[i][j] == "*" and i + j == dd: ans = min(ans, dp[i][j]) print(ans) solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR