description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\cdot(- 1)^{i - l}$ In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. -----Input----- The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a. The second line contains n integers a_1, a_2, ..., a_{n} (-10^9 ≤ a_{i} ≤ 10^9) — the array elements. -----Output----- Print the only integer — the maximum value of f. -----Examples----- Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 -----Note----- In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
import sys n = int(input()) l = list(map(int, input().split())) a = [0] * (n - 1) for i in range(n - 1): a[i] = abs(l[i] - l[i + 1]) * (-1) ** i if n == 2: print(a[0]) sys.exit() a1 = [0] * (n - 2) for i in range(1, n - 1): a1[i - 1] = abs(l[i] - l[i + 1]) * (-1) ** (i - 1) ans = a[0] cur = a[0] for i in range(1, n - 1): cur = max(cur + a[i], a[i]) ans = max(ans, cur) ans1 = a1[0] cur1 = a1[0] for i in range(1, n - 2): cur1 = max(cur1 + a1[i], a1[i]) ans1 = max(ans1, cur1) print(max(ans, ans1))
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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\cdot(- 1)^{i - l}$ In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. -----Input----- The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a. The second line contains n integers a_1, a_2, ..., a_{n} (-10^9 ≤ a_{i} ≤ 10^9) — the array elements. -----Output----- Print the only integer — the maximum value of f. -----Examples----- Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 -----Note----- In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
def m(q): s = 0 an = 0 for i in range(len(q)): s += q[i] if s < 0: s -= q[i] an = max(s, an) s = 0 an = max(s, an) an = max(s, an) return an n = int(input()) a = [int(x) for x in input().split()] z = [] y = [] p = -1 for i in range(0, n - 1): p *= -1 z.append(p * abs(a[i] - a[i + 1])) if i != 0: y.append(-1 * p * abs(a[i] - a[i + 1])) print(max(m(z), m(y)))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\cdot(- 1)^{i - l}$ In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. -----Input----- The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a. The second line contains n integers a_1, a_2, ..., a_{n} (-10^9 ≤ a_{i} ≤ 10^9) — the array elements. -----Output----- Print the only integer — the maximum value of f. -----Examples----- Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 -----Note----- In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
n = int(input()) arr = list(map(int, input().split(" "))) arr.insert(0, 0) diff_arr = [] end_so_far = 0 max_so_far = 0 l = 1 for i in range(2, len(arr) - 1): temp = abs(arr[i] - arr[i + 1]) diff_arr.append(temp * pow(-1, i)) end_so_far = end_so_far + diff_arr[-1] if end_so_far < 0: if i % 2 == 0: i = i + 1 end_so_far = 0 if max_so_far < end_so_far: max_so_far = end_so_far even_max = max_so_far max_so_far = 0 end_so_far = 0 for i in range(1, len(arr) - 1): temp = abs(arr[i] - arr[i + 1]) diff_arr.append(temp * pow(-1, i - 1)) end_so_far = end_so_far + diff_arr[-1] if end_so_far < 0: if i % 2 == 1: i = i + 1 end_so_far = 0 if max_so_far < end_so_far: max_so_far = end_so_far odd_max = max_so_far print(max(odd_max, even_max))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\cdot(- 1)^{i - l}$ In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. -----Input----- The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a. The second line contains n integers a_1, a_2, ..., a_{n} (-10^9 ≤ a_{i} ≤ 10^9) — the array elements. -----Output----- Print the only integer — the maximum value of f. -----Examples----- Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 -----Note----- In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
n = int(input()) a = list(map(int, input().split())) da, p = [], 1 for i in range(n - 1): da.append(p * abs(a[i] - a[i + 1])) p *= -1 m1, m2, s1, s2 = 0, 0, 0, 0 for x in da: s1 += x if s1 < 0: s1 = 0 s2 -= x if s2 < 0: s2 = 0 m1 = max(m1, s1) m2 = max(m2, s2) print(max(m1, m2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\cdot(- 1)^{i - l}$ In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. -----Input----- The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a. The second line contains n integers a_1, a_2, ..., a_{n} (-10^9 ≤ a_{i} ≤ 10^9) — the array elements. -----Output----- Print the only integer — the maximum value of f. -----Examples----- Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 -----Note----- In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
def maxSubArraySum(a, size): max_so_far = -9999999999999 - 1 max_ending_here = 0 for i in range(0, size): max_ending_here = max_ending_here + a[i] if max_so_far < max_ending_here: max_so_far = max_ending_here if max_ending_here < 0: max_ending_here = 0 return max_so_far a = int(input()) b = list(map(int, input().split())) dp = [] for x in range(1, a): dp.append(abs(b[x] - b[x - 1])) dp1 = dp.copy() for x in range(a - 1): if x % 2 != 0: dp[x] = -dp[x] for x in range(1, a - 1): if x % 2 == 0: dp1[x] = -dp1[x] j = maxSubArraySum(dp, a - 1) h = maxSubArraySum(dp1[1:], a - 2) print(max(j, h))
FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\cdot(- 1)^{i - l}$ In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. -----Input----- The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a. The second line contains n integers a_1, a_2, ..., a_{n} (-10^9 ≤ a_{i} ≤ 10^9) — the array elements. -----Output----- Print the only integer — the maximum value of f. -----Examples----- Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 -----Note----- In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
def ii(): return int(input()) def si(): return input() def mi(): return map(int, input().split()) def msi(): return map(str, input().split()) def li(): return list(mi()) n = ii() a = li() dp = [[0, 0] for i in range(n)] for i in range(1, n): curr = abs(a[i] - a[i - 1]) if dp[i - 1][0] > 0: dp[i][1] = curr dp[i][0] = curr - dp[i - 1][1] else: dp[i][1] = curr - dp[i - 1][0] if dp[i - 1][1] < 0: dp[i][0] = curr else: dp[i][0] = curr - dp[i - 1][1] ans = 0 for i in range(n): ans = max(ans, dp[i][1]) print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\cdot(- 1)^{i - l}$ In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. -----Input----- The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a. The second line contains n integers a_1, a_2, ..., a_{n} (-10^9 ≤ a_{i} ≤ 10^9) — the array elements. -----Output----- Print the only integer — the maximum value of f. -----Examples----- Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 -----Note----- In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) d = [0] * n c = [0] * n e = [0] * n x = 1 mx = 0 for i in range(1, n): d[i] = abs(a[i] - a[i - 1]) for i in range(1, n): c[i] = max(0, c[i - 1] + d[i] * x) e[i] = max(0, e[i - 1] + d[i] * -x) mx = max(mx, max(c[i], e[i])) x = -x print(mx)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\cdot(- 1)^{i - l}$ In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. -----Input----- The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a. The second line contains n integers a_1, a_2, ..., a_{n} (-10^9 ≤ a_{i} ≤ 10^9) — the array elements. -----Output----- Print the only integer — the maximum value of f. -----Examples----- Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 -----Note----- In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
lectura = lambda: map(int, input().split()) n = list(lectura())[0] lista = list(lectura()) fDescrita = 0 alt1 = 1 maxV1 = 0 maxV2 = 0 C1 = 0 C2 = 0 for i in range(0, n - 1): fDescrita = abs(lista[i] - lista[i + 1]) * alt1 maxV1 = max(maxV1 + fDescrita, fDescrita) maxV2 = max(maxV2, maxV1) alt1 = alt1 * -1 C1 = maxV2 maxV1 = 0 maxV2 = 0 alt1 = 1 for i in range(1, n - 1): fDescrita = abs(lista[i] - lista[i + 1]) * alt1 maxV1 = max(maxV1 + fDescrita, fDescrita) maxV2 = max(maxV2, maxV1) alt1 = alt1 * -1 C2 = maxV2 print(max(C1, C2))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\cdot(- 1)^{i - l}$ In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. -----Input----- The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a. The second line contains n integers a_1, a_2, ..., a_{n} (-10^9 ≤ a_{i} ≤ 10^9) — the array elements. -----Output----- Print the only integer — the maximum value of f. -----Examples----- Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 -----Note----- In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
from sys import stdin, stdout n = int(stdin.readline().rstrip()) a = [int(q) for q in stdin.readline().rstrip().split()] fOdd = [] fEven = [] for i in range(n - 1): fOdd.append(abs(a[i] - a[i + 1]) * (-1) ** i) fEven.append(abs(a[i] - a[i + 1]) * (-1) ** (i - 1)) index = 0 fOddCurrent = 0 fEvenCurrent = 0 fMax = 0 while index < n - 1: fOddCurrent += fOdd[index] fEvenCurrent += fEven[index] if index == 0 or fEvenCurrent < 0: fEvenCurrent = 0 if fOddCurrent < 0: fOddCurrent = 0 fMax = max([fMax, fEvenCurrent, fOddCurrent]) index += 1 print(fMax)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\cdot(- 1)^{i - l}$ In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. -----Input----- The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a. The second line contains n integers a_1, a_2, ..., a_{n} (-10^9 ≤ a_{i} ≤ 10^9) — the array elements. -----Output----- Print the only integer — the maximum value of f. -----Examples----- Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 -----Note----- In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
def kadane(A): if len(A) == 0: return 0 max_c = A[0] max_g = A[0] for i in range(1, len(A)): max_c = max(A[i], max_c + A[i]) if max_c > max_g: max_g = max_c return max_g def answer(n, A): b = [] for i in range(0, n - 1): x = abs(A[i] - A[i + 1]) * (-1) ** i b.append(x) c = [] for i in range(1, n - 1): y = abs(A[i] - A[i + 1]) * (-1) ** (i + 1) c.append(y) s1 = kadane(b) s2 = kadane(c) return max(s1, s2) n = int(input()) arr = list(map(int, input().split())) print(answer(n, arr))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\cdot(- 1)^{i - l}$ In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. -----Input----- The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a. The second line contains n integers a_1, a_2, ..., a_{n} (-10^9 ≤ a_{i} ≤ 10^9) — the array elements. -----Output----- Print the only integer — the maximum value of f. -----Examples----- Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 -----Note----- In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
N = int(input()) L = list(map(int, input().split())) if N == 2: print(abs(L[0] - L[1])) exit() M = [] for i in range(N - 1): M.append(abs(L[i] - L[i + 1])) M1 = [0] flg = True for i in range(N - 1): if flg: M1.append(M1[-1] + M[i]) else: M1.append(M1[-1] - M[i]) flg = not flg M2 = [0] flg = True for i in range(1, N - 1): if flg: M2.append(M2[-1] + M[i]) else: M2.append(M2[-1] - M[i]) flg = not flg def get_max(lst): maxv = 0 minv = lst[0] for l in lst: maxv = max(maxv, l - minv) minv = min(minv, l) return maxv ans = max(get_max(M1), get_max(M2)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:$f(l, r) = \sum_{i = l}^{r - 1}|a [ i ] - a [ i + 1 ]|\cdot(- 1)^{i - l}$ In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. -----Input----- The first line contains single integer n (2 ≤ n ≤ 10^5) — the size of the array a. The second line contains n integers a_1, a_2, ..., a_{n} (-10^9 ≤ a_{i} ≤ 10^9) — the array elements. -----Output----- Print the only integer — the maximum value of f. -----Examples----- Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 -----Note----- In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
n = int(input()) l = list(map(int, input().split(" "))) d = [] for i in range(n - 1): a = 1 if i % 2: a = -1 d.append(a * abs(l[i] - l[i + 1])) s, b = 0, 0 sm, bm = 0, 0 for i in range(n - 1): s = max(d[i], s + d[i]) b = max(s, b) for i in range(1, n - 1): sm = min(d[i], sm + d[i]) bm = min(sm, bm) print(max(abs(bm), abs(b)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows: <image> In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. Input The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≤ ai ≤ 109) — the array elements. Output Print the only integer — the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
n = int(input()) a = list(map(int, input().split())) d = [(abs(a[i] - a[i + 1]) * (-1) ** i) for i in range(n - 1)] maxsum1 = 0 sum1 = 0 for di in d: if di > 0 or abs(di) < abs(sum1): sum1 += di else: sum1 = 0 maxsum1 = max(maxsum1, abs(sum1)) sum1 = 0 for di in d[1:]: if di < 0 or abs(di) < abs(sum1): sum1 += di else: sum1 = 0 maxsum1 = max(maxsum1, abs(sum1)) print(maxsum1)
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 FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Today Adilbek is taking his probability theory test. Unfortunately, when Adilbek arrived at the university, there had already been a long queue of students wanting to take the same test. Adilbek has estimated that he will be able to start the test only $T$ seconds after coming. Fortunately, Adilbek can spend time without revising any boring theorems or formulas. He has an app on this smartphone which contains $n$ Japanese crosswords to solve. Adilbek has decided to solve them all one by one in the order they are listed in the app, without skipping any crossword. For each crossword, a number $t_i$ is given that represents the time it takes an average crossword expert to solve this crossword (the time is given in seconds). Adilbek is a true crossword expert, but, unfortunately, he is sometimes unlucky in choosing the way to solve the crossword. So, it takes him either $t_i$ seconds or $t_i + 1$ seconds to solve the $i$-th crossword, equiprobably (with probability $\frac{1}{2}$ he solves the crossword in exactly $t_i$ seconds, and with probability $\frac{1}{2}$ he has to spend an additional second to finish the crossword). All these events are independent. After $T$ seconds pass (or after solving the last crossword, if he manages to do it in less than $T$ seconds), Adilbek closes the app (if he finishes some crossword at the same moment, that crossword is considered solved; otherwise Adilbek does not finish solving the current crossword at all). He thinks it would be an interesting probability theory problem to calculate $E$ — the expected number of crosswords he will be able to solve completely. Can you calculate it? Recall that the expected value of a discrete random variable is the probability-weighted average of all possible values — in this problem it means that the expected value of the number of solved crosswords can be calculated as $E = \sum \limits_{i = 0}^{n} i p_i$, where $p_i$ is the probability that Adilbek will solve exactly $i$ crosswords. We can represent $E$ as rational fraction $\frac{P}{Q}$ with $Q > 0$. To give the answer, you should print $P \cdot Q^{-1} \bmod (10^9 + 7)$. -----Input----- The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 2 \cdot 10^{14}$) — the number of crosswords and the time Adilbek has to spend, respectively. The second line contains $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le 10^9$), where $t_i$ is the time it takes a crossword expert to solve the $i$-th crossword. Note that Adilbek solves the crosswords in the order they are given in the input without skipping any of them. -----Output----- Print one integer — the expected value of the number of crosswords Adilbek solves in $T$ seconds, expressed in the form of $P \cdot Q^{-1} \bmod (10^9 + 7)$. -----Examples----- Input 3 5 2 2 2 Output 750000007 Input 3 5 2 1 2 Output 125000003 -----Note----- The answer for the first sample is equal to $\frac{14}{8}$. The answer for the second sample is equal to $\frac{17}{8}$.
mod = 10**9 + 7 MAX = 2 * 10**5 + 1 r = [1] * MAX factorial = [1] * MAX rfactorial = [1] * MAX rp = [1] * MAX for i in range(2, MAX): factorial[i] = i * factorial[i - 1] % mod r[i] = mod - mod // i * r[mod % i] % mod rfactorial[i] = rfactorial[i - 1] * r[i] % mod for i in range(1, MAX): rp[i] = rp[i - 1] * (mod + 1) // 2 % mod n, T = list(map(int, input().split())) t = list(map(int, input().split())) t.append(10**10 + 1) S = 0 EX = 0 for i in range(len(t)): cof = rp[1] for add in range(2): l_, r_ = max(0, T - S - t[i] + add), min(i, T - S) for x in range(l_, r_ + 1): EX = ( EX + i * (factorial[i] * rfactorial[x] * rfactorial[i - x]) * rp[i] * cof ) % mod S += t[i] print(EX)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Today Adilbek is taking his probability theory test. Unfortunately, when Adilbek arrived at the university, there had already been a long queue of students wanting to take the same test. Adilbek has estimated that he will be able to start the test only $T$ seconds after coming. Fortunately, Adilbek can spend time without revising any boring theorems or formulas. He has an app on this smartphone which contains $n$ Japanese crosswords to solve. Adilbek has decided to solve them all one by one in the order they are listed in the app, without skipping any crossword. For each crossword, a number $t_i$ is given that represents the time it takes an average crossword expert to solve this crossword (the time is given in seconds). Adilbek is a true crossword expert, but, unfortunately, he is sometimes unlucky in choosing the way to solve the crossword. So, it takes him either $t_i$ seconds or $t_i + 1$ seconds to solve the $i$-th crossword, equiprobably (with probability $\frac{1}{2}$ he solves the crossword in exactly $t_i$ seconds, and with probability $\frac{1}{2}$ he has to spend an additional second to finish the crossword). All these events are independent. After $T$ seconds pass (or after solving the last crossword, if he manages to do it in less than $T$ seconds), Adilbek closes the app (if he finishes some crossword at the same moment, that crossword is considered solved; otherwise Adilbek does not finish solving the current crossword at all). He thinks it would be an interesting probability theory problem to calculate $E$ — the expected number of crosswords he will be able to solve completely. Can you calculate it? Recall that the expected value of a discrete random variable is the probability-weighted average of all possible values — in this problem it means that the expected value of the number of solved crosswords can be calculated as $E = \sum \limits_{i = 0}^{n} i p_i$, where $p_i$ is the probability that Adilbek will solve exactly $i$ crosswords. We can represent $E$ as rational fraction $\frac{P}{Q}$ with $Q > 0$. To give the answer, you should print $P \cdot Q^{-1} \bmod (10^9 + 7)$. -----Input----- The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 2 \cdot 10^{14}$) — the number of crosswords and the time Adilbek has to spend, respectively. The second line contains $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le 10^9$), where $t_i$ is the time it takes a crossword expert to solve the $i$-th crossword. Note that Adilbek solves the crosswords in the order they are given in the input without skipping any of them. -----Output----- Print one integer — the expected value of the number of crosswords Adilbek solves in $T$ seconds, expressed in the form of $P \cdot Q^{-1} \bmod (10^9 + 7)$. -----Examples----- Input 3 5 2 2 2 Output 750000007 Input 3 5 2 1 2 Output 125000003 -----Note----- The answer for the first sample is equal to $\frac{14}{8}$. The answer for the second sample is equal to $\frac{17}{8}$.
MOD = 10**9 + 7 MAX = 5 * 10**5 fac, ifac, ipow2 = [1] * MAX, [1] * MAX, [1] * MAX for i in range(1, MAX): fac[i] = fac[i - 1] * i % MOD ifac[i] = pow(fac[i], MOD - 2, MOD) ipow2[i] = ipow2[i - 1] * (MOD + 1) // 2 % MOD choose = lambda n, k: fac[n] * ifac[k] % MOD * ifac[n - k] % MOD n, t = map(int, input().split()) a = list(map(int, input().split())) s = 0 p = [1] + [0] * (n + 1) k = cur = 0 for i in range(n): s += a[i] if s > t: break if s + i + 1 <= t: p[i + 1] = 1 continue newk = t - s cur = ( cur * 2 - choose(i, k) if cur else sum(choose(i + 1, j) for j in range(newk + 1)) ) if newk < k: cur -= sum(choose(i + 1, x) for x in range(k, newk, -1)) cur %= MOD p[i + 1] = cur * ipow2[i + 1] % MOD k = newk print(sum((p[i] - p[i + 1]) * i % MOD for i in range(1, n + 1)) % MOD)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
Today Adilbek is taking his probability theory test. Unfortunately, when Adilbek arrived at the university, there had already been a long queue of students wanting to take the same test. Adilbek has estimated that he will be able to start the test only $T$ seconds after coming. Fortunately, Adilbek can spend time without revising any boring theorems or formulas. He has an app on this smartphone which contains $n$ Japanese crosswords to solve. Adilbek has decided to solve them all one by one in the order they are listed in the app, without skipping any crossword. For each crossword, a number $t_i$ is given that represents the time it takes an average crossword expert to solve this crossword (the time is given in seconds). Adilbek is a true crossword expert, but, unfortunately, he is sometimes unlucky in choosing the way to solve the crossword. So, it takes him either $t_i$ seconds or $t_i + 1$ seconds to solve the $i$-th crossword, equiprobably (with probability $\frac{1}{2}$ he solves the crossword in exactly $t_i$ seconds, and with probability $\frac{1}{2}$ he has to spend an additional second to finish the crossword). All these events are independent. After $T$ seconds pass (or after solving the last crossword, if he manages to do it in less than $T$ seconds), Adilbek closes the app (if he finishes some crossword at the same moment, that crossword is considered solved; otherwise Adilbek does not finish solving the current crossword at all). He thinks it would be an interesting probability theory problem to calculate $E$ — the expected number of crosswords he will be able to solve completely. Can you calculate it? Recall that the expected value of a discrete random variable is the probability-weighted average of all possible values — in this problem it means that the expected value of the number of solved crosswords can be calculated as $E = \sum \limits_{i = 0}^{n} i p_i$, where $p_i$ is the probability that Adilbek will solve exactly $i$ crosswords. We can represent $E$ as rational fraction $\frac{P}{Q}$ with $Q > 0$. To give the answer, you should print $P \cdot Q^{-1} \bmod (10^9 + 7)$. -----Input----- The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 2 \cdot 10^{14}$) — the number of crosswords and the time Adilbek has to spend, respectively. The second line contains $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le 10^9$), where $t_i$ is the time it takes a crossword expert to solve the $i$-th crossword. Note that Adilbek solves the crosswords in the order they are given in the input without skipping any of them. -----Output----- Print one integer — the expected value of the number of crosswords Adilbek solves in $T$ seconds, expressed in the form of $P \cdot Q^{-1} \bmod (10^9 + 7)$. -----Examples----- Input 3 5 2 2 2 Output 750000007 Input 3 5 2 1 2 Output 125000003 -----Note----- The answer for the first sample is equal to $\frac{14}{8}$. The answer for the second sample is equal to $\frac{17}{8}$.
mod = 10**9 + 7 MAX = 2 * 10**5 + 2 n, T = list(map(int, input().split())) t = [0] t += list(map(int, input().split())) t += [MAX] r = [1] * MAX factorial = [1] * MAX rfactorial = [1] * MAX rp = [1] * MAX sim = 0 sim_n = 0 sim_k = 0 E = 0 S = [t[0]] * len(t) for i in range(1, len(t)): S[i] = S[i - 1] + t[i] for i in range(2, MAX): factorial[i] = i * factorial[i - 1] % mod r[i] = mod - mod // i * r[mod % i] % mod rfactorial[i] = rfactorial[i - 1] * r[i] % mod for i in range(1, MAX): rp[i] = rp[i - 1] * (mod + 1) // 2 % mod def Combination(n, k): if n < k: return 0 return factorial[n] * rfactorial[k] * rfactorial[n - k] def simC(x): aux = 0 mi = min(x, T - S[x]) for i in range(0, mi + 1): aux += Combination(x, i) % mod return x, mi, aux def next_simC(next_x): mi1 = min(next_x, T - S[next_x]) aux_sim = 2 * sim % mod aux_sim = (aux_sim + Combination(sim_n, sim_k + 1)) % mod aux_sim_n = sim_n + 1 aux_sim_k = sim_k + 1 while aux_sim_k > mi1: aux_sim = (aux_sim - Combination(aux_sim_n, aux_sim_k)) % mod aux_sim_k -= 1 return aux_sim_n, aux_sim_k, aux_sim i = 1 while i <= n and S[i] + i <= T: i += 1 E += 1 if i <= n: sim_n, sim_k, sim = simC(i) for x in range(i, n + 1): E = (E + rp[x] * sim) % mod if T - S[x + 1] < 0: break sim_n, sim_k, sim = next_simC(x + 1) print(E)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Today Adilbek is taking his probability theory test. Unfortunately, when Adilbek arrived at the university, there had already been a long queue of students wanting to take the same test. Adilbek has estimated that he will be able to start the test only $T$ seconds after coming. Fortunately, Adilbek can spend time without revising any boring theorems or formulas. He has an app on this smartphone which contains $n$ Japanese crosswords to solve. Adilbek has decided to solve them all one by one in the order they are listed in the app, without skipping any crossword. For each crossword, a number $t_i$ is given that represents the time it takes an average crossword expert to solve this crossword (the time is given in seconds). Adilbek is a true crossword expert, but, unfortunately, he is sometimes unlucky in choosing the way to solve the crossword. So, it takes him either $t_i$ seconds or $t_i + 1$ seconds to solve the $i$-th crossword, equiprobably (with probability $\frac{1}{2}$ he solves the crossword in exactly $t_i$ seconds, and with probability $\frac{1}{2}$ he has to spend an additional second to finish the crossword). All these events are independent. After $T$ seconds pass (or after solving the last crossword, if he manages to do it in less than $T$ seconds), Adilbek closes the app (if he finishes some crossword at the same moment, that crossword is considered solved; otherwise Adilbek does not finish solving the current crossword at all). He thinks it would be an interesting probability theory problem to calculate $E$ — the expected number of crosswords he will be able to solve completely. Can you calculate it? Recall that the expected value of a discrete random variable is the probability-weighted average of all possible values — in this problem it means that the expected value of the number of solved crosswords can be calculated as $E = \sum \limits_{i = 0}^{n} i p_i$, where $p_i$ is the probability that Adilbek will solve exactly $i$ crosswords. We can represent $E$ as rational fraction $\frac{P}{Q}$ with $Q > 0$. To give the answer, you should print $P \cdot Q^{-1} \bmod (10^9 + 7)$. -----Input----- The first line contains two integers $n$ and $T$ ($1 \le n \le 2 \cdot 10^5$, $1 \le T \le 2 \cdot 10^{14}$) — the number of crosswords and the time Adilbek has to spend, respectively. The second line contains $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le 10^9$), where $t_i$ is the time it takes a crossword expert to solve the $i$-th crossword. Note that Adilbek solves the crosswords in the order they are given in the input without skipping any of them. -----Output----- Print one integer — the expected value of the number of crosswords Adilbek solves in $T$ seconds, expressed in the form of $P \cdot Q^{-1} \bmod (10^9 + 7)$. -----Examples----- Input 3 5 2 2 2 Output 750000007 Input 3 5 2 1 2 Output 125000003 -----Note----- The answer for the first sample is equal to $\frac{14}{8}$. The answer for the second sample is equal to $\frac{17}{8}$.
from sys import exit, stdin, stdout mod = 10**9 + 7 def modinv(x): return pow(x, mod - 2, mod) N = 2 * 10**5 + 10 facts = [1] * N for i in range(1, N): facts[i] = facts[i - 1] * i facts[i] %= mod def binom(n, k): ans = modinv(facts[k]) * modinv(facts[n - k]) ans %= mod ans *= facts[n] ans %= mod return ans n, T = map(int, stdin.readline().split()) ts = list(map(int, stdin.readline().split())) ans = 0 total = sum(ts) running = total last_idx = n - 1 while running > T: running -= ts[last_idx] last_idx -= 1 last_bd = -1 last_sum = 0 idx = last_idx while running + idx + 1 > T: bd = T - running cur_sum = last_sum + (binom(idx + 1, last_bd) if last_bd >= 0 else 0) cur_sum *= modinv(2) cur_sum %= mod for fresh in range(last_bd + 1, bd + 1): cur_sum += binom(idx + 1, fresh) cur_sum %= mod ans += cur_sum * modinv(pow(2, idx + 1, mod)) ans %= mod running -= ts[idx] last_bd = bd last_sum = cur_sum idx -= 1 ans += idx + 1 ans %= mod print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right. After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino. Given a string "S" representing the initial state. S[i] = 'L', if the i-th domino has been pushed to the left; S[i] = 'R', if the i-th domino has been pushed to the right; S[i] = '.', if the i-th domino has not been pushed. Return a string representing the final state.  Example 1: Input: ".L.R...LR..L.." Output: "LL.RR.LLRRLL.." Example 2: Input: "RR.L" Output: "RR.L" Explanation: The first domino expends no additional force on the second domino. Note: 0 <= N <= 10^5 String dominoes contains only 'L', 'R' and '.'
class Solution: def pushDominoes(self, dominoes: str) -> str: l = len(dominoes) right_force = [0] * l left_force = [0] * l force1 = 0 force2 = 0 for i in range(l): if dominoes[i] == "R": force1 = l elif dominoes[i] == "L": force1 = 0 else: force1 = max(force1 - 1, 0) right_force[i] += force1 for i in range(l - 1, -1, -1): if dominoes[i] == "L": force2 = l elif dominoes[i] == "R": force2 = 0 else: force2 = max(force2 - 1, 0) left_force[i] += force2 result = "" for i in range(l): if right_force[i] > left_force[i]: result += "R" elif right_force[i] < left_force[i]: result += "L" else: result += "." return result
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR STRING IF VAR VAR VAR VAR VAR STRING VAR STRING RETURN VAR VAR
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right. After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino. Given a string "S" representing the initial state. S[i] = 'L', if the i-th domino has been pushed to the left; S[i] = 'R', if the i-th domino has been pushed to the right; S[i] = '.', if the i-th domino has not been pushed. Return a string representing the final state.  Example 1: Input: ".L.R...LR..L.." Output: "LL.RR.LLRRLL.." Example 2: Input: "RR.L" Output: "RR.L" Explanation: The first domino expends no additional force on the second domino. Note: 0 <= N <= 10^5 String dominoes contains only 'L', 'R' and '.'
class Solution: def pushDominoes(self, dominoes: str) -> str: forces = [0] * len(dominoes) max_force = len(dominoes) force = 0 for i, d in enumerate(dominoes): if d == "R": force = max_force elif d == "L": force = 0 else: force = max(0, force - 1) forces[i] += force force = 0 for i in range(len(dominoes) - 1, -1, -1): d = dominoes[i] if d == "L": force = max_force elif d == "R": force = 0 else: force = max(0, force - 1) forces[i] -= force print(forces) result = "" for f in forces: if f == 0: result += "." elif f > 0: result += "R" else: result += "L" return result
CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER VAR STRING IF VAR NUMBER VAR STRING VAR STRING RETURN VAR VAR
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right. After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino. Given a string "S" representing the initial state. S[i] = 'L', if the i-th domino has been pushed to the left; S[i] = 'R', if the i-th domino has been pushed to the right; S[i] = '.', if the i-th domino has not been pushed. Return a string representing the final state.  Example 1: Input: ".L.R...LR..L.." Output: "LL.RR.LLRRLL.." Example 2: Input: "RR.L" Output: "RR.L" Explanation: The first domino expends no additional force on the second domino. Note: 0 <= N <= 10^5 String dominoes contains only 'L', 'R' and '.'
INF = float("inf") class Solution: def pushDominoes(self, dominoes: str) -> str: n = len(dominoes) d1 = [-1] * n d2 = [-1] * n cnt = INF for i in range(n - 1, -1, -1): if dominoes[i] == "L": cnt = 0 elif dominoes[i] == ".": cnt += 1 elif dominoes[i] == "R": cnt = INF d1[i] = cnt cnt = INF for i in range(n): if dominoes[i] == "R": cnt = 0 elif dominoes[i] == ".": cnt += 1 elif dominoes[i] == "L": cnt = INF d2[i] = cnt ret = [] for i in range(n): if d1[i] == d2[i]: ret.append(".") elif d1[i] < d2[i]: ret.append("L") else: ret.append("R") return "".join(ret)
ASSIGN VAR FUNC_CALL VAR STRING CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN FUNC_CALL STRING VAR VAR
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right. After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino. Given a string "S" representing the initial state. S[i] = 'L', if the i-th domino has been pushed to the left; S[i] = 'R', if the i-th domino has been pushed to the right; S[i] = '.', if the i-th domino has not been pushed. Return a string representing the final state.  Example 1: Input: ".L.R...LR..L.." Output: "LL.RR.LLRRLL.." Example 2: Input: "RR.L" Output: "RR.L" Explanation: The first domino expends no additional force on the second domino. Note: 0 <= N <= 10^5 String dominoes contains only 'L', 'R' and '.'
class Solution: def pushDominoes(self, dominoes: str) -> str: n = len(dominoes) d = [0] * n force = 0 for i in range(n): c = dominoes[i] if c == "R": force = n elif c == "L": force = 0 else: force = max(force - 1, 0) d[i] += force force = 0 for i in range(n - 1, -1, -1): c = dominoes[i] if c == "R": force = 0 elif c == "L": force = n else: force = max(force - 1, 0) d[i] -= force def inner(): for f in d: if f == 0: yield "." elif f > 0: yield "R" else: yield "L" return "".join(inner())
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_DEF FOR VAR VAR IF VAR NUMBER EXPR STRING IF VAR NUMBER EXPR STRING EXPR STRING RETURN FUNC_CALL STRING FUNC_CALL VAR VAR
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right. After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino. Given a string "S" representing the initial state. S[i] = 'L', if the i-th domino has been pushed to the left; S[i] = 'R', if the i-th domino has been pushed to the right; S[i] = '.', if the i-th domino has not been pushed. Return a string representing the final state.  Example 1: Input: ".L.R...LR..L.." Output: "LL.RR.LLRRLL.." Example 2: Input: "RR.L" Output: "RR.L" Explanation: The first domino expends no additional force on the second domino. Note: 0 <= N <= 10^5 String dominoes contains only 'L', 'R' and '.'
class Solution: def pushDominoes(self, dominoes: str) -> str: n = len(dominoes) balance = [0] * len(dominoes) b = 0 for index, c in enumerate(dominoes): if c == "R": b = n elif c == "L": b = 0 else: b = max(b - 1, 0) balance[index] += b b = 0 for index, c in enumerate(reversed(dominoes)): index = -index - 1 if c == "R": b = 0 elif c == "L": b = n else: b = max(b - 1, 0) balance[index] -= b result = ["."] * len(dominoes) for i in range(len(dominoes)): if dominoes[i] == "R" or dominoes[i] == "L": result[i] = dominoes[i] elif balance[i] > 0: result[i] = "R" elif balance[i] < 0: result[i] = "L" return "".join(result)
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR NUMBER ASSIGN VAR VAR STRING RETURN FUNC_CALL STRING VAR VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, string, k): def findsubs(maxK): n = len(string) normal = {} dist_cnt = 0 subs = 0 s, e = 0, 0 while e < n: normal[string[e]] = 1 + normal.get(string[e], 0) if normal[string[e]] == 1: dist_cnt += 1 while dist_cnt > maxK: normal[string[s]] -= 1 if normal[string[s]] == 0: dist_cnt -= 1 s += 1 subs += e - s + 1 e += 1 return subs a = findsubs(k) b = findsubs(k - 1) return a - b
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): last, ts = [-1] * 26, [] ans = 0 for i, c in enumerate(s): v = ord(c) - 97 if last[v] >= 0: ts.remove(last[v]) last[v] = i ts.append(i) M = len(ts) if M > k: ans += ts[-k] - ts[-k - 1] elif M == k: ans += ts[0] + 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): def util(s, k): if not s: return 0 mapp = {} num = 0 left = 0 for i in range(len(s)): mapp[s[i]] = mapp.get(s[i], 0) + 1 while len(mapp) > k: mapp[s[left]] -= 1 if mapp[s[left]] == 0: del mapp[s[left]] left += 1 num += i - left + 1 return num result = util(s, k) - util(s, k - 1) return result
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
def find(s, k): c = 0 d = {} si = 0 dcc = 0 n = len(s) for i in range(n): if s[i] in d: d[s[i]] += 1 else: d[s[i]] = 1 dcc += 1 while dcc > k: d[s[si]] -= 1 if d[s[si]] == 0: dcc -= 1 del d[s[si]] si += 1 c += i - si + 1 return c class Solution: def substrCount(self, s, k): c1 = find(s, k) c2 = find(s, k - 1) return c1 - c2
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): ans = self.solve(s, k) - self.solve(s, k - 1) return ans def solve(self, s, k): dict1 = {} count = 0 j = 0 for i in range(len(s)): if s[i] in dict1: dict1[s[i]] += 1 else: dict1[s[i]] = 1 while len(dict1) > k: dict1[s[j]] -= 1 if dict1[s[j]] == 0: del dict1[s[j]] j += 1 count += i - j + 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): return self.getAtmost(s, k) - self.getAtmost(s, k - 1) def getAtmost(self, s, k): freqMap = {} right = left = 0 countSubString = 0 while right < len(s): freqMap[s[right]] = freqMap.get(s[right], 0) + 1 while len(freqMap) > k: freqMap[s[left]] -= 1 if freqMap[s[left]] == 0: freqMap.pop(s[left]) left += 1 countSubString += right - left + 1 right += 1 return countSubString
CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): return self.counts(s, k) - self.counts(s, k - 1) def counts(self, s, k): m = dict() count = 0 i = 0 j = 0 while j < len(s): if s[j] in m: m[s[j]] += 1 else: m[s[j]] = 1 while i <= j and len(m) > k: m[s[i]] -= 1 if m[s[i]] == 0: del m[s[i]] i += 1 count += j - i + 1 j += 1 return count
CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def solve(self, s, k): i = 0 j = 0 dis_cou = 0 hash_arr = [0] * 26 ans = 0 while j < len(s): if hash_arr[ord(s[j]) - ord("a")] == 0: dis_cou += 1 hash_arr[ord(s[j]) - ord("a")] += 1 while dis_cou > k: hash_arr[ord(s[i]) - ord("a")] -= 1 if hash_arr[ord(s[i]) - ord("a")] == 0: dis_cou -= 1 i += 1 ans += j - i + 1 j += 1 return ans def substrCount(self, s, k): ans = self.solve(s, k) - self.solve(s, k - 1) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): smallhash = {} biggerhash = {} idx = 0 i = 0 j = 0 res = 0 while idx < len(s): while i < len(s) and not ( len(smallhash) == k - 1 and s[i] not in smallhash ): if s[i] not in smallhash: smallhash[s[i]] = 1 else: smallhash[s[i]] += 1 i += 1 while j < len(s) and not (len(biggerhash) == k and s[j] not in biggerhash): if s[j] not in biggerhash: biggerhash[s[j]] = 1 else: biggerhash[s[j]] += 1 j += 1 if i >= len(s) or len(smallhash) != k - 1: break if j >= len(s) and len(biggerhash) != k: break if i < idx: i = idx res += j - i if s[idx] in smallhash: smallhash[s[idx]] -= 1 if smallhash[s[idx]] == 0: smallhash.pop(s[idx]) if s[idx] in biggerhash: biggerhash[s[idx]] -= 1 if biggerhash[s[idx]] == 0: biggerhash.pop(s[idx]) idx += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def solve(self, s, k): ans = 0 i, j = -1, -1 h = {} while True: while i < len(s) - 1: i += 1 h[s[i]] = h.get(s[i], 0) + 1 if len(h) <= k: ans += i - j else: break if i == len(s) - 1 and len(h) <= k: break while j < i: j += 1 if h[s[j]] == 1: del h[s[j]] else: h[s[j]] -= 1 if len(h) > k: continue else: ans += i - j break return ans def substrCount(self, s, k): return self.solve(s, k) - self.solve(s, k - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT WHILE NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): def lek(s, k): i, j = 0, 0 res = 0 dictionary = dict() while j < len(s): if s[j] in dictionary: dictionary[s[j]] += 1 else: dictionary[s[j]] = 1 c = len(dictionary) if c < k: res += j - i + 1 j += 1 elif c == k: res += j - i + 1 j += 1 elif c > k: while c > k: dictionary[s[i]] -= 1 if dictionary[s[i]] == 0: del dictionary[s[i]] c -= 1 i += 1 res += j - i + 1 j += 1 return res return lek(s, k) - lek(s, k - 1)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): n = len(s) if k > n: return 0 i = 0 j = 1 d = {s[0]: 1} di = {s[0]: 0} r = 1 res = 0 if k == 1: res = 1 while i <= j and j < n: if s[j] not in di: if r < k: di[s[j]] = j r += 1 else: mini = j for x in di: if mini > di[x]: mini = di[x] if di[x] == mini: p = x del di[p] di[s[j]] = j i = mini + 1 else: di[s[j]] = j if r == k: mini = j for x in di: if mini > di[x]: mini = di[x] res += mini - i + 1 j += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): ans, st, dp = 0, 0, {} last = 0 for e in range(len(s)): dp[s[e]] = dp.get(s[e], 0) + 1 if len(dp) > k: ei, n = st, {} while len(dp) > k: n[s[ei]] = n.get(s[ei], 0) + 1 while len(n) == k: ans += e - ei n[s[st]] -= 1 dp[s[st]] -= 1 if not n[s[st]]: del n[s[st]] if not dp[s[st]]: del dp[s[st]] st += 1 ei += 1 if len(dp) == k: if len(dp) == k: n, e = {}, st while e < len(s): n[s[e]] = n.get(s[e], 0) + 1 while len(n) == k: ans += len(s) - e n[s[st]] -= 1 if not n[s[st]]: del n[s[st]] st += 1 e += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR DICT WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR DICT VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): length = len(s) ans = 0 ib = -1 ism = -1 mapB = {} mapS = {} j = -1 if k == 1: ans = 0 lastChar = s[0] lastIndex = 0 for i in range(len(s)): curChar = s[i] if lastChar == curChar: ans += i - lastIndex + 1 else: ans += 1 lastIndex = i lastChar = curChar return ans def removeFromMap(char, charmap): if charmap[char] == 1: charmap.pop(char) return charmap[char] -= 1 while True: f1 = False f2 = False f3 = False while ib < length - 1: f1 = True ib += 1 char = s[ib] mapB[char] = mapB.get(char, 0) + 1 if len(mapB) == k + 1: ib -= 1 removeFromMap(char, mapB) break while ism < length - 1: f2 = True ism += 1 char = s[ism] mapS[char] = mapS.get(char, 0) + 1 if len(mapS) == k: ism -= 1 removeFromMap(char, mapS) break while j < ism: f3 = True j += 1 char = s[j] if len(mapS) == k - 1 and len(mapB) == k: ans += ib - ism removeFromMap(char, mapB) removeFromMap(char, mapS) if len(mapS) != k - 1 or len(mapB) != k: break if not f1 and not f2 and not f3: break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR RETURN VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): return self.solve(s, k) - self.solve(s, k - 1) def solve(self, s, k): ans = distCnt = release = 0 mp = {} for aquire in range(len(s)): currChar = s[aquire] if currChar in mp: mp[currChar] += 1 else: distCnt += 1 mp[currChar] = 1 while release <= aquire and distCnt > k: discardChar = s[release] mp[discardChar] -= 1 release += 1 if mp[discardChar] == 0: del mp[discardChar] distCnt -= 1 ans += aquire - release + 1 return ans
CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
def f(A, k): n = len(A) i = 0 j = 0 freq = [0] * 26 d = 0 ans = 0 while j < n: o = ord(A[j]) - ord("a") freq[o] += 1 if freq[o] == 1: d += 1 while d > k: o = ord(A[i]) - ord("a") freq[o] -= 1 if freq[o] == 0: d -= 1 i += 1 ans += j - i + 1 j += 1 return ans class Solution: def substrCount(self, S, k): return f(S, k) - f(S, k - 1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): def solve(st, k): mp = dict() s = set() l = 0 ans = 0 for r in range(len(st)): if st[r] not in mp.keys(): mp[st[r]] = 1 else: mp[st[r]] += 1 s.add(st[r]) while len(s) > k: mp[st[l]] -= 1 if mp[st[l]] == 0: s.remove(st[l]) l += 1 ans += r - l + 1 return ans return solve(s, k) - solve(s, k - 1)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def solve(self, s, k): d = {} n = len(s) i, j = 0, 0 count = 0 while j < n: d[s[j]] = d.get(s[j], 0) + 1 while len(d) > k: d[s[i]] -= 1 if d[s[i]] == 0: del d[s[i]] i += 1 count += j - i + 1 j += 1 return count def substrCount(self, s, k): return self.solve(s, k) - self.solve(s, k - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def count(self, s, n, k): m = {} j = 0 c = 0 for i in range(n): if s[i] in m: m[s[i]] += 1 else: m[s[i]] = 1 while len(m) > k: m[s[j]] -= 1 if m[s[j]] == 0: del m[s[j]] j += 1 c += i - j return c def substrCount(self, s, k): n = len(s) return self.count(s, n, k) - self.count(s, n, k - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): st = {x: (0) for x in "abcdefghijklmnopqrstuvwxyz"} i = 0 c = 0 c1 = 0 for j in range(len(s)): st[s[j]] += 1 if st[s[j]] == 1: c += 1 while c > k: st[s[i]] -= 1 if st[s[i]] == 0: c -= 1 i += 1 c1 += j - i + 1 st = {x: (0) for x in "abcdefghijklmnopqrstuvwxyz"} k -= 1 i = 0 c = 0 c2 = 0 for j in range(len(s)): st[s[j]] += 1 if st[s[j]] == 1: c += 1 while c > k: st[s[i]] -= 1 if st[s[i]] == 0: c -= 1 i += 1 c2 += j - i + 1 return c1 - c2
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): def countStrings(diff): if diff == 0: return 0 left = 0 right = 0 count = 0 dicti = {} while right < len(s): if s[right] in dicti: dicti[s[right]] += 1 else: dicti[s[right]] = 1 while len(dicti) > diff: dicti[s[left]] -= 1 if dicti[s[left]] == 0: dicti.pop(s[left]) left += 1 count += right - left + 1 right += 1 return count return countStrings(k) - countStrings(k - 1)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def countAtMostK(self, s, k): start = end = ans = 0 mp = {} while end < len(s): if s[end] in mp.keys(): mp[s[end]] += 1 else: mp[s[end]] = 1 while len(mp) > k: mp[s[start]] -= 1 if mp[s[start]] == 0: mp.pop(s[start]) start += 1 ans += end - start + 1 end += 1 return ans def substrCount(self, s, k): return self.countAtMostK(s, k) - self.countAtMostK(s, k - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def substrCount(self, s, k): if k == 1: n = len(s) c = 1 f = 0 for i in range(n - 1): if s[i] == s[i + 1]: c += 1 else: f += c * (c + 1) // 2 c = 1 f += c * (c + 1) // 2 return f m1 = {} m2 = {} ism = -1 ib = -1 j = -1 c = 0 n = len(s) while True: f1 = 0 f2 = 0 f3 = 0 while ib < n - 1: f1 = 1 ib += 1 if s[ib] in m1: m1[s[ib]] += 1 else: m1[s[ib]] = 1 if len(m1) == k + 1: del m1[s[ib]] ib -= 1 break while ism < n - 1: f2 = 1 ism += 1 if s[ism] in m2: m2[s[ism]] += 1 else: m2[s[ism]] = 1 if len(m2) == k: del m2[s[ism]] ism -= 1 break while j < ism: f3 = 1 if len(m1) == k and len(m2) == k - 1: c += ib - ism j += 1 g = s[j] m1[g] -= 1 if m1[g] == 0: del m1[g] m2[g] -= 1 if m2[g] == 0: del m2[g] if len(m1) < k or len(m2) < k - 1: break if f1 == 0 and f2 == 0 and f3 == 0: break return c
CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
class Solution: def at_most_k(self, s, k): n = len(s) count = 0 freq = {} start = end = 0 while end < n: ch = s[end] freq[ch] = freq.get(ch, 0) + 1 while len(freq) > k: ch = s[start] freq[ch] -= 1 if freq[ch] == 0: freq.pop(ch) start += 1 count += end - start + 1 end += 1 return count def substrCount(self, s, k): return self.at_most_k(s, k) - self.at_most_k(s, k - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Example 1: Input: S = "aba", K = 2 Output: 3 Explanation: The substrings are: "ab", "ba" and "aba". Example 2: Input: S = "abaaca", K = 1 Output: 7 Explanation: The substrings are: "a", "b", "a", "aa", "a", "c", "a". Your Task: You don't need to read input or print anything. Your task is to complete the function substrCount() which takes the string S and an integer K as inputs and returns the number of substrings having exactly K distinct characters. Expected Time Complexity: O(|S|). Expected Auxiliary Space: O(1). Constraints: 1 ≤ |S| ≤ 10^{5} 1 ≤ K ≤ 26
def atmost(s, k): n = len(s) if n == 0: return 0 d = {} x = 0 y = 0 for i in range(n): if s[i] not in d: d[s[i]] = 1 else: d[s[i]] += 1 while len(d) > k: d[s[y]] -= 1 if d[s[y]] == 0: d.pop(s[y]) y += 1 x += i - y + 1 return x class Solution: def substrCount(self, s, k): return atmost(s, k) - atmost(s, k - 1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, h = map(int, input().split()) a = list(map(int, input().split())) mi = a[0] ma = a[0] a = a[1:] s = 1 for x in a: mi = max(x, mi - h + 1) ma = min(x + h - 1, ma + h - 1) if ma < mi: s = 0 if mi != a[-1]: s = 0 print("YES") if s == 1 else print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER EXPR VAR NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def solve(): [n, k] = map(int, input().split(" ")) h = list(map(int, input().split(" "))) assert len(h) == n assert n >= 2 lo = h[0] + 1 hi = lo def bnd(x, xmin, xmax): return min(xmax, max(xmin, x)) for i in range(1, n): new_lo = lo - (k - 1) new_hi = hi + (k - 1) if new_hi < h[i] + 1 or new_lo > h[i] + k: print("NO") return lo = bnd(new_lo, h[i] + 1, h[i] + k) hi = bnd(new_hi, h[i] + 1, h[i] + k) if lo > hi: print("NO") return print("YES" if lo <= h[n - 1] + 1 and h[n - 1] + 1 <= hi else "NO") def main(): t = int(input()) for _ in range(t): solve() main()
FUNC_DEF ASSIGN LIST VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR STRING STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
from sys import stdin def main(): from sys import stdin input = stdin.buffer.readline for _ in range(int(input())): n, k = map(int, input().split()) (*h,) = map(int, input().split()) mn = [0] * n mx = [0] * n mn[0] = mx[0] = h[0] for i in range(1, n): mn[i] = max(h[i], mn[i - 1] - k + 1) mx[i] = min(h[i], mx[i - 1]) + k - 1 if mn[i] > mx[i]: print("NO") break else: if mn[n - 1] <= h[n - 1] <= mx[n - 1]: print("YES") else: print("NO") main()
FUNC_DEF ASSIGN VAR VAR 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for i in range(t): n, k = tuple(map(int, input().split(" "))) heights = list(map(int, input().split(" "))) current_floor_range = [heights[0], heights[0]] is_possible = True for j in range(1, n): current_floor_range = [ max(current_floor_range[0] - k + 1, heights[j]), min(current_floor_range[1], heights[j]) + k - 1, ] if current_floor_range[0] > current_floor_range[1]: is_possible = False break print( "YES" if is_possible and current_floor_range[0] <= heights[n - 1] <= current_floor_range[1] else "NO" )
ASSIGN VAR FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER STRING STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for i in range(t): l = input().split(" ") n, k = int(l[0]), int(l[1]) l = input().split(" ") for j in range(n): l[j] = int(l[j]) lastbl = l[0] lastbh = l[0] isok = True for j in range(1, n - 1): if lastbh + (k - 1) < l[j]: isok = False break if lastbl - (k - 1) > l[j] + (k - 1): isok = False break lastbl = max(lastbl - (k - 1), l[j]) lastbh = min(lastbh + (k - 1), l[j] + (k - 1)) if lastbh < lastbl: isok = False break if lastbh + (k - 1) < l[n - 1]: isok = False if lastbl - (k - 1) > l[n - 1]: isok = False print("YES" if isok else "NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in " " * int(input()): n, k = map(int, input().split()) (*h,) = map(int, input().split()) min_hs = [h[0]] * n max_hs = [h[0]] * n for i in range(1, n): min_touch = min_hs[i - 1] - k + 1 max_touch = max_hs[i - 1] + k - 1 min_ground = h[i] max_ground = h[i] + k - 1 if i != n - 1 else h[i] min_pos = max(min_touch, min_ground) max_pos = min(max_touch, max_ground) if min_pos > max_pos: print("NO") break min_hs[i] = min_pos max_hs[i] = max_pos else: print("YES")
FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = map(int, input().split()) h = list(map(int, input().split())) prev = h[0] con = 0 for i in range(1, n - 1): if h[i] > h[i - 1]: if prev + k > h[i]: if h[i + 1] > h[i]: if h[i] + k - 1 < prev + k: prev = h[i] + k - 1 else: prev = prev + k - 1 else: prev = h[i] else: con = 1 break elif h[i] < h[i - 1]: if h[i] + 2 * k - 1 > prev: if h[i + 1] > h[i]: prev = h[i] + k - 1 elif prev - (k - 1) >= h[i]: prev = prev - (k - 1) else: prev = h[i] else: con = 1 break elif h[i + 1] > h[i]: prev = h[i] + k - 1 else: prev = h[i] if con == 1: print("NO") elif abs(prev - h[-1]) > k - 1: print("NO") else: print("YES")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def buildFence(): lowest, highest = heights[0], heights[0] for i in range(1, n - 1): newLow, newHigh = heights[i], heights[i] + k - 1 if newHigh + k <= lowest or newLow >= highest + k: return False newLow += max(0, lowest + 1 - newLow - k) newHigh -= max(0, newHigh + 1 - highest - k) lowest = newLow highest = newHigh if lowest - k < heights[-1] < highest + k: return True else: return False for _ in range(int(input())): n, k = map(int, input().split()) heights = list(map(int, input().split())) if buildFence(): print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys input = sys.stdin.readline def gcd(a, b): if a == 0: return b return gcd(b % a, a) def lcm(a, b): return a * b / gcd(a, b) def main(): for _ in range(int(input())): n, k = map(int, input().split()) h = list(map(int, input().split())) currmax = h[0] + k f = 0 for i in range(1, n - 1): if h[i] >= currmax: f = 1 elif h[i] >= h[i - 1]: if h[i + 1] >= h[i]: currmax = h[i] + min(k - 1, currmax - h[i] - 1) + k else: currmax = h[i] + k elif h[i] + k + k - 1 <= currmax - k: f = 1 elif h[i + 1] >= h[i]: currmax = h[i] + k + k - 1 else: now = h[i] + k if now <= currmax - k: now += currmax - k - now + 1 currmax = now if h[n - 1] >= currmax: f = 1 elif h[n - 1] + k <= currmax - k: f = 1 if f: print("NO") else: print("YES") return main()
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = map(int, input().split()) h = list(map(int, input().split())) d = [[-1, -1] for i in range(n)] d[0] = [h[0], h[0]] ans = "YES" for i in range(1, len(h)): d[i] = [ max(h[i], d[i - 1][0] - (k - 1)), min(h[i] + (k - 1), d[i - 1][1] + (k - 1)), ] if d[i][0] - h[i] > k - 1: ans = "NO" break if d[i - 1][1] + k <= d[i][0]: ans = "NO" break if d[-1][0] > h[-1]: ans = "NO" print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER LIST VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def solve(arr, k): n = len(arr) preL, preH = arr[0] + k, arr[0] + k for i in range(1, n - 1): lo = preL - k + 1 hi = preH + k - 1 if lo - k >= arr[i] + k or hi <= arr[i]: return "NO" preL, preH = max(lo, arr[i] + k), min(hi, arr[i] + 2 * k - 1) if preL > preH: return "NO" lo = preL - k + 1 hi = preH + k - 1 if lo > arr[-1] + k or hi < arr[-1] + k: return "NO" return "YES" for _ in range(int(input())): _, k = map(int, input().split()) arr = list(map(int, input().split())) print(solve(arr, k))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR RETURN STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = list(map(int, input().split())) h = [int(i) for i in input().split()] s = "YES" hh = hl = h[0] for i in range(1, n): hh = min(hh + k - 1, h[i] + k - 1) hl = max(hl - k + 1, h[i]) if hh < hl: s = "NO" break if hl != h[-1]: s = "NO" print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR STRING IF VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def getints(): return list(map(int, input().split())) def solve(): n, k = getints() k1 = k - 1 hi, *hlist = getints() lo = hi for h in hlist[:-1]: lo, hi = max(lo - k1, h), min(hi + k1, h + k1) if hi < lo: return "NO" if lo - k1 <= hlist[-1] <= hi + k1: return "YES" return "NO" for _ in range(int(input())): print(solve())
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN STRING IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for y in range(int(input())): n, m = map(int, input().split()) lst = list(map(int, input().split())) flag = 0 if n == 1: print("YES") continue minstart = maxstart = 0 for i in range(n): if i == 0: minstart = lst[0] maxstart = lst[0] minend = maxend = lst[0] + m - 1 else: minstart = minstart - (m - 1) maxstart = maxstart + (m - 1) if lst[i] < minstart - (m - 1) or lst[i] > maxstart: flag = 1 break minstart = max(lst[i], minstart) maxstart = min(lst[i] + (m - 1), maxstart) if i == n - 1: if lst[i] < minstart or lst[i] > maxstart: flag = 1 break if flag == 0: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) start = [max(a[0] - k + 1, 0), a[0] + k - 1] ok = True for i in range(1, n): if not ok: break if i != n - 1: p1 = a[i] + k - 1 p2 = a[i] if start[0] <= p2 <= start[1] or start[0] <= p1 <= start[1]: l = [max(p2, start[0]), min(p1, start[1])] start = [max(l[0] - k + 1, 0), l[1] + k - 1] else: ok = False else: p1 = a[i] p2 = a[i] if start[0] <= p2 <= start[1] or start[0] <= p1 <= start[1]: l = [max(p2, start[0]), min(p1, start[1])] start = [max(l[0] - k + 1, 0), l[1] + k - 1] else: ok = False if ok: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def readInt(): return int(input()) def readLine(): return [int(s) for s in input().split(" ")] def readString(): return input() def ask(arr, k): pLow = arr[0] pHigh = arr[0] for i in range(len(arr)): t = arr[i] pLow = pLow - (k - 1) pHigh = pHigh + (k - 1) if i == len(arr) - 1 or i == 0: allowLow = t allowHigh = t else: allowLow = t allowHigh = t + k - 1 if allowLow > pHigh or pLow > allowHigh: return False pLow = max(allowLow, pLow) pHigh = min(allowHigh, pHigh) return True t = readInt() for _ in range(t): n, k = readLine() arrs = readLine() result = ask(arrs, k) if result: print("YES") else: print("NO")
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = map(int, input().split()) h = list(map(int, input().split())) flag = False lo = h[0] - k + 1 hi = h[0] + k - 1 for i in range(1, n): if h[i] > hi or h[i] + k <= lo: flag = True break lo = max(lo - k + 1, h[i] - k + 1) hi = max(hi + k - 1, h[i] + k - 1) lo = h[-1] - k + 1 hi = h[-1] + k - 1 for i in range(n - 2, -1, -1): if h[i] > hi or h[i] + k <= lo: flag = True break lo = max(lo - k + 1, h[i] - k + 1) hi = max(hi + k - 1, h[i] + k - 1) if flag: print("No") else: print("Yes")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for testCaseCounter in range(t): splitted = [int(amit) for amit in input().split(" ")] n = splitted[0] k = splitted[1] arr = [int(amit) for amit in input().split(" ")] lowestStartingPoint = arr[0] highestStartingPoint = arr[0] valid = True for i in range(1, n): lowestStartingPoint = max(arr[i], lowestStartingPoint - (k - 1)) highestStartingPoint = min(arr[i] + (k - 1), highestStartingPoint + (k - 1)) if lowestStartingPoint > highestStartingPoint: valid = False break if valid and lowestStartingPoint <= arr[n - 1] <= highestStartingPoint: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
case = int(input()) for c in range(case): n, k = map(int, input().split()) myList = list(map(int, input().split())) mini = myList[0] maxi = myList[0] ok = True for ind1 in range(1, len(myList)): mini = max(myList[ind1], mini - (k - 1)) maxi = min(myList[ind1] + k - 1, maxi + k - 1) if myList[ind1] > maxi or myList[ind1] + k - 1 < mini: ok = False break if myList[len(myList) - 1] < mini or myList[len(myList) - 1] > maxi: ok = False if ok: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) l = a[0] h = a[0] + k ans = "YES" for i in range(1, n - 1): t1 = a[i] + 2 * k - 1 if t1 <= l or h <= a[i]: ans = "NO" l = max(l - k + 1, a[i]) h = min(h + k - 1, t1) if a[n - 1] + k <= l or h <= a[n - 1]: ans = "NO" print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
from sys import stdin nii = lambda: map(int, stdin.readline().split()) lnii = lambda: list(map(int, stdin.readline().split())) t = int(input()) for tt in range(t): n, k = nii() h = lnii() min_h = h[0] max_h = h[0] + k for i in range(1, n): if i < n - 1: if h[i] > max_h - 1: print("NO") break min_h = max(h[i], min_h - (k - 1)) max_h = min(max_h + (k - 1), h[i] + k + (k - 1)) if min_h + k > max_h: print("NO") break elif min_h >= h[i] + k or max_h <= h[i]: print("NO") else: print("YES")
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n, k = map(int, input().split()) A = list(map(int, input().split())) MIN = A[0] MAX = A[0] flag = 1 for i in range(1, n): h = A[i] NMIN = max(h, MIN - (k - 1)) NMAX = min(MAX + (k - 1), h + (k - 1)) if NMIN > NMAX: flag = 0 break MIN, MAX = NMIN, NMAX if flag == 0: print("NO") elif MIN <= A[-1] <= MAX: print("YES") else: print("NO")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
from sys import stdin input = stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) a = [int(x) for x in input().split()] b = [a[0]] c = [a[-1]] d = [] for x in a[1:]: b.append(min(b[-1] + k - 1, x + k - 1)) for x in reversed(a[:-1]): c.append(min(c[-1] + k - 1, x + k - 1)) c.reverse() for x in range(n): d.append(min(b[x], c[x])) flag = "YES" for x in range(n): if a[x] > d[x]: flag = "NO" break print(flag)
ASSIGN VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for ___ in range(int(input())): n, k = list(map(int, input().split())) hs = list(map(int, input().split())) rg = [-2, 10**9] ok = True for i, h in enumerate(hs): if i == 0 or i == len(hs) - 1: rgn = [h, h + k] else: rgn = [h, h + k + k - 1] rg = [max(0, rg[0] - k + 1), rg[1] + k - 1] rg = [max(rg[0], rgn[0]), min(rg[1], rgn[1])] if rg[1] - rg[0] < k: ok = False break print("YES" if ok else "NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
nt = int(input()) for i in range(nt): [n, k] = [int(x) for x in input().split()] h = [int(x) for x in input().split()] mins = h maxs = [(x + k - 1) for x in h] maxs[0] = h[0] maxs[n - 1] = h[n - 1] good = [1] def update(i, fmin, fmax): dif = 0 if fmin > mins[i]: mins[i] = fmin dif = 1 if fmax < maxs[i]: maxs[i] = fmax dif = 1 if mins[i] > maxs[i]: good[0] = 0 dif = 1 return dif i = 0 while 0 <= i < n - 1 and good[0]: if update(i, mins[i + 1] - (k - 1), maxs[i + 1] + (k - 1)): i -= 1 else: update(i + 1, mins[i] - (k - 1), maxs[i] + (k - 1)) i += 1 print("YES" if good[0] else "NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
from sys import stdin input = stdin.readline t = int(input()) for _ in range(t): n, k = [int(x) for x in input().split()] h = [int(x) for x in input().split()] ans = "YES" min_1 = h[0] - (k - 1) max_1 = h[0] + (k - 1) for i in range(1, n): min_2 = h[i] if i == n - 1: max_2 = min_2 else: max_2 = h[i] + (k - 1) if max_1 < min_2 or min_1 > max_2: ans = "NO" break min_1 = max(min_1, min_2) - (k - 1) max_1 = min(max_1, max_2) + (k - 1) print(ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) l = list(map(int, input().split())) minn = [l[0]] maxx = [l[0]] for i in range(1, n): minnn = max(l[i], minn[i - 1] - k + 1) maxxx = min(l[i] + k - 1, maxx[i - 1] + k - 1) minn.append(minnn) maxx.append(maxxx) minn1 = [l[n - 1]] maxx1 = [l[n - 1]] i = n - 1 j = 0 while i > 0: i -= 1 j += 1 minnn = max(l[i], minn1[j - 1] - k + 1) maxxx = min(l[i] + k - 1, maxx1[j - 1] + k - 1) minn1.append(minnn) maxx1.append(maxxx) flag = "YES" for i in range(n): if minn[i] > maxx[i] or minn1[i] > maxx1[i]: flag = "NO" break print(flag)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys for _ in range(int(input())): n, k = map(int, sys.stdin.readline().split()) h = [int(i) for i in sys.stdin.readline().split()] flag = 1 available_interval = [h[0] + 1, h[0] + 1] for h_i in h: available_interval = [ max(h_i + 1, available_interval[0] - k + 1), min(h_i + k + k - 1, available_interval[1] + k - 1), ] if ( available_interval[1] - available_interval[0] + 1 < k or available_interval[0] - h_i > k ): flag = 0 break if flag and available_interval[0] == h[-1] + 1: print("YES") else: print("NO")
IMPORT 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for test in range(int(input())): _, k = list(map(int, input().split())) h = list(map(int, input().split())) if len(h) == 2: if abs(h[0] - h[1]) <= k - 1: print("YES") else: print("NO") else: placement = h[0] for i, h_i in list(enumerate(h)): if i == 0: if h[i + 1] >= h_i: if h_i + k - 1 < h[i + 1]: print("NO") break elif h_i > h[i + 1] + k - 1 + k - 1: print("NO") break elif i == len(h) - 2: if h[i + 1] >= h_i: if min(h_i + k - 1, placement + k - 1) + k - 1 < h[i + 1]: print("NO") break elif max(h_i, placement - k + 1) > h[i + 1] + k - 1: print("NO") break print("YES") break elif h[i + 1] >= h_i: if min(h_i + k - 1, placement + k - 1) + k - 1 < h[i + 1]: print("NO") break placement = min(h_i + k - 1, placement + k - 1) else: if max(h_i, placement - k + 1) > h[i + 1] + k - 1 + k - 1: print("NO") break placement = max(h_i, placement - k + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for i in range(t): n, k = list(map(int, input().split())) heights = list(map(int, input().split())) min_pos = heights[0] max_pos = heights[0] possible = True for j in range(1, n): min_pos = min_pos - k + 1 max_pos = max_pos + k - 1 if max_pos >= heights[j] and min_pos <= heights[j] + k - 1 and j != n - 1: min_pos = max(min_pos, heights[j]) max_pos = min(max_pos, heights[j] + k - 1) elif j == n - 1 and max_pos >= heights[j] and min_pos <= heights[j]: print("YES") else: print("NO") break
ASSIGN VAR FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) h = list(map(int, input().split())) b = [h[0]] flag = True for i in range(1, n - 1): min_must_base = max(0, b[i - 1] - (k - 1)) max_must_base = b[i - 1] + (k - 1) min_can_base = max(h[i], 0) max_can_base = h[i] + (k - 1) if min_must_base <= max_can_base and max_must_base >= min_can_base: if h[i] < h[i + 1]: b.append(min(max_can_base, max_must_base)) else: b.append(max(min_can_base, min_must_base)) else: flag = False break if not flag: print("NO") continue if h[-1] - (k - 1) <= b[n - 2] <= h[-1] + k - 1: print("YES") else: print("NO")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
from sys import stdin for _ in range(int(input())): n, k = list(map(int, input().split())) lst = list(map(int, input().split())) bot, top = lst[0], lst[0] for i in range(1, n): bot = max(0, max(lst[i], bot - k + 1)) top = min(lst[i] + k - 1, top + k - 1) if bot > top: print("NO") break else: if bot <= lst[-1] <= top: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys lines = [] for line in sys.stdin: lines.append(line.rstrip("\n")) t = int(lines[0]) for i in range(1, 2 * t + 1, 2): n, k = map(int, lines[i].split(" ")) h = list(map(int, lines[i + 1].split(" "))) m, M = h[0], h[0] for j in range(1, n): M = min(h[j] + k - 1, M + k - 1) m = max(h[j], m - (k - 1)) if M < m: break if m <= h[n - 1] and h[n - 1] <= M: print("YES") else: print("NO")
IMPORT ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
class minmax: def __init__(self, min_, max_): self.min_ = min_ self.max_ = max_ def lop(): n, k = map(int, input().split()) h = input().split() this_range = minmax(0, 0) prev_range = minmax(0, 0) for i in range(n): if i == 0 or i == n - 1: this_range = minmax(int(h[i]) + k, int(h[i]) + k) else: this_range = minmax( max(prev_range.min_ - k + 1, int(h[i]) + k), min(prev_range.max_ + k - 1, int(h[i]) + 2 * k - 1), ) if this_range.min_ > this_range.max_: return "NO" if i == n - 1: u = this_range.min_ if u < prev_range.min_ - k + 1 or u > prev_range.max_ + k - 1: return "NO" prev_range = this_range return "YES" t = int(input()) out = [] for p in range(t): out.append(lop()) for p in out: print(p)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR RETURN STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN STRING ASSIGN VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) h = list(map(int, input().split())) ans = 1 lb, ub = h[0], h[0] for i in range(1, n): lb_next = h[i] ub_next = h[i] + k - 1 lb_next = max(lb - (k - 1), lb_next) ub_next = min(ub + (k - 1), ub_next) if lb_next > ub_next: ans = 0 break lb = lb_next ub = ub_next if lb > h[n - 1] or ub < h[n - 1]: ans = 0 print("YES" if ans else "NO")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
input = __import__("sys").stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) D = [*map(int, input().split())] min_height = [0] * n max_height = [0] * n min_height[0] = D[0] max_height[0] = D[0] min_height[n - 1] = D[n - 1] max_height[n - 1] = D[n - 1] for i in range(1, n - 1): min_height[i] = min(D[i] + k - 1, max(0, D[i], min_height[i - 1] + 1 - k)) max_height[i] = min(D[i] + k - 1, max_height[i - 1] + k - 1) err = 0 for i in range(n - 1): if ( max_height[i] + k > min_height[i + 1] and min_height[i] < max_height[i + 1] + k ): continue else: err = 1 if not err: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR STRING 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 LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def Fence(ground, k): max_fence_last = ground[0] + k min_fence_last = ground[0] for i in range(1, len(ground)): max_f = min(max_fence_last + k - 1, ground[i] + k + k - 1) min_f = max(min_fence_last - k + 1, ground[i]) if max_f - min_f < k: return "NO" max_fence_last = max_f min_fence_last = min_f if min_f != ground[i]: return "NO" else: return "YES" for i in range(int(input())): k = int(input().split()[1]) ground = list(map(int, input().split())) print(Fence(ground, k))
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR RETURN STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def cmp(a, h): return [max(a[0], h[0]), min(a[1], h[1])] t = input() for _ in range(int(t)): sign = 0 n, k = input().split(" ") n = int(n) k = int(k) height = input().split(" ") height = [int(he) for he in height] now = [height[0], height[0]] for i in range(1, n): tem = ( cmp([height[i], height[i] + k - 1], [now[0] - k + 1, now[1] + k - 1]) if i != n - 1 else cmp([height[i], height[i]], [now[0] - k + 1, now[1] + k - 1]) ) if tem[1] < tem[0]: sign = 1 print("NO") break else: now = tem if sign == 0: print("YES")
FUNC_DEF RETURN LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR LIST VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR LIST VAR VAR VAR VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) H = list(map(int, input().split())) lo, hi = H[0] + k, H[0] + k for i in range(1, n - 1): lo, hi = max(H[i] + k, lo - (k - 1)), min(H[i] + 2 * k - 1, hi - 1 + k) if lo > hi: return "NO" return "YES" if lo - k < H[-1] + k < hi + k else "NO" for _ in range(int(input())): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR RETURN STRING RETURN BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys input = sys.stdin.readline for nt in range(int(input())): n, k = map(int, input().split()) h = list(map(int, input().split())) ans = "YES" minn = h[0] maxx = h[0] for i in range(1, n): minn = max(h[i], minn - k + 1) maxx = min(h[i] + k - 1, maxx + k - 1) if maxx < minn or minn + k - 1 < maxx: ans = "NO" break if minn > h[-1] or h[-1] > maxx: ans = "NO" print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR STRING IF VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
R = lambda: map(int, input().split()) (t,) = R() for _ in [0] * t: n, k = R() l, *a, e = R() h = l f = 1 for x in a: l = max(l - k + 1, x) h = min(h, x) + k - 1 f &= l <= h print("NYOE S"[f & (l - k < e < h + k) :: 2])
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def intersection(seg1, seg2): if seg1[0] <= seg2[0]: if seg1[1] < seg2[0]: return False elif seg1[1] <= seg2[1]: return [seg2[0], seg1[1]] else: return [seg2[0], seg2[1]] elif seg1[0] > seg2[1]: return False elif seg1[1] >= seg2[1]: return [seg1[0], seg2[1]] else: return [seg1[0], seg1[1]] cases = int(input()) for _ in range(cases): n, k = list(map(int, input().split())) level = list(map(int, input().split())) seg_satis = [0, level[0]] seg_need = [] seg_satis_temp = [] den = 0 for i in level[:-1]: seg_need = [i, i + k - 1] seg_satis_temp = intersection(seg_satis, seg_need) if seg_satis_temp == False: den = 1 break else: seg_satis = [seg_satis_temp[0] - k + 1, seg_satis_temp[1] + k - 1] if den == 1: print("NO") else: seg_need = [level[-1], level[-1]] seg_satis_temp = intersection(seg_satis, seg_need) if seg_satis_temp == False: print("NO") else: print("YES")
FUNC_DEF IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN LIST VAR NUMBER VAR NUMBER RETURN LIST VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN LIST VAR NUMBER VAR NUMBER RETURN LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR LIST VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for __ in range(int(input())): n, k = map(int, input().split()) h = list(map(int, input().split())) status = 1 dom1, dom2 = [h[0] + 1 - k, h[0] + k - 1], [] if dom1[0] < 0: dom1[0] = 0 for i in range(1, n): if i != n - 1: dom2 = [h[i], h[i] + k - 1] else: dom2 = [h[i], h[i]] if dom2[0] > dom1[1] or dom1[0] > dom2[1]: status = 0 break else: t1 = max([dom1[0], dom2[0]]) + 1 - k t2 = min([dom1[1], dom2[1]]) + k - 1 if t1 < 0: t1 = 0 dom1 = [t1, t2] if status: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR LIST BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER LIST IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) h = list(map(int, input().split())) minh = [h[0] + k] + [(0) for i in range(n - 1)] maxh = [h[0] + k] + [(0) for i in range(n - 1)] flag = True for i in range(1, n - 1): if h[i] >= maxh[i - 1] or h[i] + 2 * k - 1 <= minh[i - 1] - k: flag = False break maxh[i] = min(maxh[i - 1] + k - 1, h[i] + 2 * k - 1) minh[i] = max(minh[i - 1] - k + 1, h[i] + k) if h[n - 1] + k > maxh[n - 2] + k - 1 or h[n - 1] + k < minh[n - 2] - k + 1: flag = False if flag: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) H = list(map(int, input().split())) lv = [-1] * n lv[0], lv[-1] = H[0] + k, H[-1] + k idx = 1 while idx < n: while idx < n and H[idx] >= H[idx - 1]: idx += 1 peak = idx - 1 lv[peak] = H[peak] + k j = peak - 1 while j > 0 and lv[j] == -1: if lv[j + 1] - (H[j] + 2 * k - 1) >= k: return "NO" lv[j] = max(H[j] + k, lv[j + 1] - k + 1) j -= 1 while idx < n - 1 and H[idx] <= H[idx - 1]: if lv[idx - 1] - (H[idx] + 2 * k - 1) >= k: return "NO" lv[idx] = max(H[idx] + k, lv[idx - 1] - k + 1) idx += 1 if idx < n - 1: valley = idx - 1 lv[valley] = H[valley] + 2 * k - 1 elif idx == n - 1: if H[idx] > H[idx - 1]: lv[idx - 1] = H[idx - 1] + 2 * k - 1 idx += 1 for i in range(n - 1): if abs(lv[i] - lv[i + 1]) >= k: return "NO" return "YES" for _ in range(int(input())): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = map(int, input().split()) h = list(map(int, input().split())) lo = list(h) hi = [(i + k - 1) for i in h] hi[0] = lo[0] hi[n - 1] = lo[n - 1] valid = True for i in range(1, n): a = lo[i - 1] - k + 1 b = hi[i - 1] + k - 1 if a > hi[i] or b < lo[i]: valid = False break lo[i] = max(lo[i], a) hi[i] = min(hi[i], b) for i in range(n - 2, -1, -1): a = lo[i + 1] - k + 1 b = hi[i + 1] + k - 1 if a > hi[i] or b < lo[i]: valid = False break lo[i] = max(lo[i], a) hi[i] = min(hi[i], b) print("YES" if valid else "NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys try: sys.stdin = open("input.txt", "r") except FileNotFoundError: pass def next_array(): return [int(i) for i in input().split()] def prefix(lst): assert len(lst) pre = [lst[0]] for i in range(1, len(lst)): pre.append(lst[i] + pre[i - 1]) return pre def solve(): l, k = next_array() lst = next_array() max_bottom = lst[0] min_bottom = lst[0] for i in range(1, l): if max_bottom + k - 1 < lst[i] or min_bottom - k + 1 > ( lst[i] + k - 1 if i < l - 1 else lst[i] ): print("NO") return max_bottom = min(max_bottom, lst[i]) + k - 1 min_bottom = max(min_bottom - k + 1, lst[i]) print("YES") test_cases = True if test_cases: for i in range(int(input())): solve() else: solve()
IMPORT ASSIGN VAR FUNC_CALL VAR STRING STRING VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) h = list(map(int, input().split())) lst = [0] * n lst[0] = h[0] for i in range(1, n - 1): if h[i] - lst[i - 1] >= k or lst[i - 1] - h[i] > 2 * (k - 1): print("NO") break if h[i + 1] > h[i]: lst[i] = min(lst[i - 1] + k - 1, h[i] + k - 1) else: lst[i] = max(h[i], lst[i - 1] - k + 1) else: if h[n - 1] - lst[n - 2] >= k or lst[n - 2] - h[n - 1] >= k: print("NO") else: print("YES")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def sv(): N, K = map(int, input().split()) H = list(map(int, input().split())) a, b = 0, 10000000000 for n, h in enumerate(H): a = max(a, h) b = min(b, h + K - 1 if n > 0 and n < N - 1 else h) if a > b: return False a -= K - 1 b += K - 1 return True TC = int(input()) for tc in range(TC): print("YES" if sv() else "NO")
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR RETURN NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for i in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) c = 0 x = a[0] for j in range(1, n): if a[j] <= a[j - 1]: x = max(a[j], x - k + 1) if a[j] > a[j - 1]: x = a[j] if x - a[j] >= k: c = c + 1 break if x != a[n - 1]: c = c + 1 x = a[n - 1] for j in range(n - 2, -1, -1): if a[j] <= a[j + 1]: x = max(a[j], x - k + 1) if a[j] > a[j + 1]: x = a[j] if x - a[j] >= k: c = c + 1 break if x != a[0]: c = c + 1 if c > 0: print("NO") else: print("YES")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def solution(n, k, h): minn = h[0] maxx = h[0] for i in range(1, n): minn = max(h[i], minn - k + 1) maxx = min(maxx + k - 1, h[i] + k - 1) if minn > maxx: return "NO" if not (h[n - 1] >= minn and h[n - 1] <= maxx): return "NO" return "YES" for i in range(int(input())): n, k = (int(i) for i in input().split()) h = [int(i) for i in input().split()] print(solution(n, k, h))
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR RETURN STRING IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
from sys import stdin T = int(stdin.readline().strip()) for caso in range(T): n, k = map(int, stdin.readline().strip().split()) s = list(map(int, stdin.readline().strip().split())) mx = s[0] + k mn = s[0] + k f = True for i in range(1, n - 1): if mx <= s[i] or mn - k + 1 > s[i] + k + k - 1: f = False mx = min(s[i] + k - 1 + k, mx + k - 1) mn = max(s[i] + k, mn - k + 1) if mx <= s[-1] or mn - k + 1 > s[-1] + k: f = False if f: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) — the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys input = sys.stdin.readline def intersect(a, b): return [max(a[0], b[0]), min(a[1], b[1])] t = int(input()) for you in range(t): l = input().split() n = int(l[0]) k = int(l[1]) l = input().split() li = [int(i) for i in l] curr = [li[0], li[0]] poss = 1 for i in range(1, n - 1): curr[0] -= k - 1 curr[1] += k - 1 curr = intersect(curr, [li[i], li[i] + k - 1]) if curr[1] < curr[0]: poss = 0 break curr[0] -= k - 1 curr[1] += k - 1 if li[-1] < curr[0] or li[-1] > curr[1]: poss = 0 if poss: print("Yes") else: print("No")
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING