description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
a, b, c = map(int, input().split()) MAXNUM = 10**9 + 1 ans = "" ansCount = 0 def s(x): result = 0 while x > 0: result += x % 10 x = x // 10 return result for i in range(1, 82): num = b * i**a + c if num > 0 and num < MAXNUM and s(num) == i: ans = ans + str(num) + " " ansCount += 1 print(ansCount) if ansCount > 0: print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def sumdig(n): if n < 0: return pow(10, 18) else: a = 0 for x in str(n): a += int(x) return a a, b, c = map(int, input().split(" ")) sums = [] for sx in range(1, 81): if sumdig(b * sx**a + c) == sx: sums.append(sx) total = 0 string = "" for element in sums: if 0 < b * element**a + c < 10**9: string += str(b * element**a + c) string += " " total += 1 print(total) if total != 0: print(string.strip())
FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def Digitsum(x): tot = 0 if x <= 0: return 1 while x: tot += x % 10 x //= 10 return tot a, b, c = list(map(int, input().split())) ans = [] for i in range(0, 82): x = b * i**a + c if 0 < x < 10**9 and i == Digitsum(x): ans.append(x) print(len(ans)) print(" ".join(map(str, sorted(ans))))
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF NUMBER VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
import sys a, b, c = (int(i) for i in input().split(" ")) def calculate(a, b, c, s): return b * s**a + c def dig_sum(x): line = str(x).strip("-") sum = 0 for i in list(line): sum += int(i) return sum res = [] for i in range(1, 82): x = calculate(a, b, c, i) if dig_sum(x) == i: res.append(x) res.sort() res = [str(i) for i in res if i >= 0 and i < 1000000000] print(len(res)) print(" ".join(res)) sys.exit(0)
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def sum(n): s = 0 while n > 0: r = n % 10 n = n // 10 s += r return s a, b, c = map(int, input().split()) f = [] for i in range(1, 81): x = b * pow(i, a) + c if x > 1000000000: break if sum(x) == i: f.append(x) print(len(f)) for i in range(len(f)): print(f[i], end=" ")
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
x = input() arr = x.split(" ") a = int(arr[0]) b = int(arr[1]) c = int(arr[2]) ans = set() for i in range(1, 82): temp = b * i**a + c temp1 = 0 temp2 = temp while temp > 0: temp1 = temp1 + int(temp % 10) temp = int(temp / 10) if temp2 > 0 and temp2 < 1000000000 and temp1 == i: ans.add(temp2) print(len(ans)) answer = sorted(ans) if len(ans) != 0: for i in answer: print(i, "", end="")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
import sys sys.setrecursionlimit(10000) a, b, c = [int(x) for x in sys.stdin.readline().strip().split()] count = 0 def sum_digits(d): s = 0 while d: s += d % 10 d = d // 10 return s arr = [] count = 0 for i in range(1, 81): k = b * i**a + c if 0 < k < 10**9 and sum_digits(k) == i: count += 1 arr.append(str(k)) if count != 0: print(count) print(" ".join(arr)) else: print(count)
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF NUMBER VAR BIN_OP NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def sx(number): res = 0 if number[0] == "-": return 0 for i in range(len(number)): res += int(number[i]) return res input_str = input() a, b, c = ( int(input_str.split()[0]), int(input_str.split()[1]), int(input_str.split()[2]), ) results = [] for s in range(1, 81): res = b * s**a + c if sx(str(res)) == int(s) and res < 1000000000: results.append(res) res_str = "" res_str = " ".join(str(results[i]) for i in range(len(results))) print(len(results)) if len(results): print(res_str)
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER STRING RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
a, b, c = map(int, input().split()) def s(x): xStr = str(x) sum = 0 for c in xStr: sum += int(c) return sum rightSide = [] M = 9 * 9 + 1 for k in range(1, M): rightSide.append(b * k**a + c) myList = [] sol = 0 for x in rightSide: if x > 0 and x < int(1000000000.0) and x == b * s(x) ** a + c: sol += 1 myList.append(x) myList.sort() print(sol, end="\n") for num in myList: print(num, sep=" ", end=" ")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
a, b, c = list(map(int, input().split())) ans = [] def sum2(n): return sum([int(x) for x in str(n)]) for s in range(1, 82): x = b * s**a + c if x > 0 and sum2(x) == s and x < 10**9: ans.append(x) print(len(ans)) print(" ".join(map(str, ans)))
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
from sys import stdin def bin_pow(a, n): if n == 0: return 1 if n % 2: return a * bin_pow(a * a, (n - 1) // 2) else: return bin_pow(a * a, n // 2) INF = 10**9 def main(): line = stdin.readline a, b, c = map(int, line().split()) answers = [] for s in range(81): v = b * int(pow(s, a)) + c if v <= 0 or v >= INF: continue sum_digits = sum(int(c) for c in str(v)) if sum_digits == s: answers.append(v) print(len(answers)) for ans in answers: print(ans, end=" ") main()
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
load = [int(i) for i in input().split()] def digit_sum(x): res = 0 while x > 0: res += x % 10 x //= 10 return res a = load[0] b = load[1] c = load[2] sol = [] for i in range(1, 90): tmp = b * i**a + c if i == digit_sum(tmp) and tmp < 10**9: sol.append(tmp) print(len(sol)) buff = "" for i in range(len(sol)): buff += str(sol[i]) + " " print(buff)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def sum_digits3(n): r = 0 while n: r, n = r + n % 10, n // 10 return r a, b, c = input().split(" ") a = int(a) b = int(b) c = int(c) la = [] for i in range(82, 0, -1): k = b * i**a + c if k < 0: break if sum_digits3(k) == i and k < 10**9: la.append(k) print(len(la)) la = reversed(la) for i in la: print(i, end=" ")
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
a, b, c = map(int, input().split()) ans = [] for i in range(1, 91, 1): x = b * pow(i, a) + c if x > 999999999: break if x > 0: var = x fval = 0 while var > 0: fval += var % 10 var //= 10 if fval == i: ans.append(x) print("{}".format(len(ans))) for i in range(len(ans)): if i == 0: print("{}".format(ans[i]), end="") else: print(" {}".format(ans[i]), end="") if len(ans) > 0: print()
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def s(x): if x < 0: return -100000 if x == 0: return 0 else: return x % 10 + s(x // 10) a, b, c = map(int, input().split()) r = [] for i in range(1, 82): if s(b * i**a + c) == i and b * i**a + c < 1000000000: r += [b * i**a + c] print(len(r)) print(" ".join(map(str, r)))
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR LIST BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def bnp(bs, pw): res = 1 while pw: if pw & 1: res *= bs res %= int(1000000000.0 + 7) bs *= bs bs %= int(1000000000.0 + 7) pw //= 2 return res % int(1000000000.0 + 7) a, b, c = map(int, input().split()) res = [] for n in range(1, 82): u = b * bnp(n, a) + c if 0 <= u < 1000000000.0: if sum(list(map(int, str(u)))) == n: res.append(u) print(len(res)) print(*res)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR IF NUMBER VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def find_digit_sum(x): s = 0 while x > 0: s += x % 10 x //= 10 return s def exponent_mod(n, p, prime_const=1000000000 + 7): res = 1 if n == 0: return 0 while p > 0: if p % 2 == 1: res = res * n % prime_const p //= 2 n = n * n % prime_const return res def find_solutions(a, b, c): solutions = [] for j in range(1, 82): x = b * j**a + c if j == find_digit_sum(x) and x < pow(10, 9): solutions.append(x) return solutions t = 1 while t: t -= 1 prime_const = pow(10, 9) + 7 values = list(map(int, input().strip().split()))[:3] a = values[0] b = values[1] c = values[2] solutions = find_solutions(a, b, c) print(len(solutions)) print(" ".join(str(x) for x in solutions))
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
[a, b, c] = [int(x) for x in input().split()] ans = [] for i in range(1, 82): x = b * i**a + c if x >= 1000000000: continue sum = 0 while x > 0: sum += x % 10 x //= 10 if sum == i: ans.append(b * i**a + c) ans.sort() print(len(ans)) print(*ans)
ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
a, b, c = map(int, input().split()) ans = set() for s in range(82): x = s**a * b + c if 0 < x < 10**9 and sum(int(i) for i in list(str(x))) == s: ans.add(x) s += 1 print(len(ans), *ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF NUMBER VAR BIN_OP NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
a, b, c = map(int, input().split()) ans = [] c0 = 0 for i in range(1, 83): val = b * pow(i, a) + c st = str(val) if val < 0 or val >= 1000000000: continue c1 = 0 for j in st: c1 += int(j) if c1 == i: ans.append(val) c0 += 1 print(c0) print(*ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
a, b, c = input().split() a, b, c = int(a), int(b), int(c) def S(x): if x < 0: return 0 else: x = str(x) s = 0 for char in x: s += int(char) return s cnt = 0 lis = [] for x in range(1, 82): y = b * x**a + c if S(y) == x and y < 10**9: lis.append(b * x**a + c) cnt += 1 print(cnt) if cnt != 0: for k in lis: print(k, end=" ")
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def su(n): s = 0 while n: s += n % 10 n //= 10 return s a, b, c = map(int, input().split()) l = [] for i in range(1, 82): t = b * i**a + c if t > 0 and su(t) == i and t < 10**9: l += [t] l = set(l) l = list(l) t = max(len(l), 0) print(t) if t == 0: exit() l.sort() for i in l: print(i, end=" ")
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER NUMBER VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
a, b, c = map(int, input().split()) ans = [] for i in range(1, 82): x = b * pow(i, a) + c cnt = 0 for j in str(x): try: cnt += int(j) except: break if cnt == i and x <= 10**9: ans.append(x) print(len(ans)) print(*ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def sumDigit(n): ans = 0 if n <= 0: return ans while n: ans += n % 10 n //= 10 return ans a, b, c = map(int, input().split()) ans = [] can = 82 for i in range(1, 82): temp = b * int(pow(i, a)) + c if sumDigit(temp) == i and temp > 0 and temp < int(1000000000.0): ans.append(temp) print(len(ans)) for it in ans: print(it, end=" ")
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def s(x): if x < 0: return 0 a = str(x) res = 0 for item in a: res += int(item) return res a, b, c = map(int, input().split()) ans = [] for i in range(1, 100): x = b * i**a + c if x >= 10**9 or x <= 0: continue if s(x) == i: ans.append(x) ans.sort() print(len(ans)) for item in ans: print(item, end=" ")
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
a, b, c = list(map(int, input().split())) sx = 1 resitve = [] def s(x): return sum(map(int, list(str(x)))) while sx <= 81: rez = sx**a * b + c if rez > 10**9: break if rez > 0 and rez < 10**9 and s(rez) == sx: resitve.append(rez) sx += 1 print(len(resitve)) if len(resitve) > 0: print(" ".join(map(str, sorted(resitve))))
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER IF VAR NUMBER VAR BIN_OP NUMBER NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def s(x): return sum([int(d) for d in str(x)]) a, b, c = map(int, input().split()) solutions = [] if a == 1: for x in range(1, 550001): if x == b * s(x) ** a + c: solutions.append(x) else: power = 1 while b * power**a + c < 10**9: x = b * power**a + c if x > 0 and power == s(x): solutions.append(x) power += 1 print(len(solutions)) print(*solutions, sep=" ")
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
ID = input().split() a = int(ID[0]) b = int(ID[1]) c = int(ID[2]) ans = [] for i in range(1, 82, 1): x = b * i**a + c if x > 0 and x < 1000000000: y = x z = 0 while y > 0: z += y % 10 y //= 10 if z == i: ans.append(x) print(len(ans)) for x in ans: print(x, end=" ")
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 VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
ans = [] def SUM(x): ans = 0 while x > 0: ans += x % 10 x //= 10 return ans a, b, c = map(int, input().split()) for i in range(1, 82): x = b * pow(i, a) + c if x >= 1000000000: break elif SUM(x) == i: ans.append(str(x)) print(len(ans)) print(" ".join(ans))
ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
a, b, c = list(map(int, input().split())) limit = 10**9 def sum_num(tmp): ret = 0 while tmp > 0: ret += tmp % 10 tmp //= 10 return ret p = [] for i in range(1, 82): tmp = b * i**a + c if sum_num(tmp) == i and tmp <= limit: p.append(tmp) print(len(p)) for i in p: print(i, end=" ") print()
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def getsum(num): sum = 0 while num > 0: rem = num % 10 sum += rem num = int(num / 10) return sum arr = list(map(int, input().split())) a = arr[0] b = arr[1] c = arr[2] ans = [] for i in range(100): x = b * i**a + c if x <= 0 or x >= 1000000000: continue sm = getsum(abs(x)) if sm == i: ans.append(x) print(len(ans)) for i in ans: print(i, end=" ")
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN 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 LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
a, b, c = [int(x) for x in input().split()] roots = [] for i in range(1, 81 + 1): x = b * i**a + c if x > 0: if x < 1000000000: t = x // 10 s = x % 10 while t > 0: s += t % 10 t //= 10 if s == i: roots.append(x) else: break print(len(roots)) for x in roots: print(x, end=" ") print()
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment. Find all integer solutions x (0 < x < 10^9) of the equation:x = b·s(x)^{a} + c, where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. -----Input----- The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000). -----Output----- Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 10^9. -----Examples----- Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967
def s(x): tmp = x ans = 0 while tmp > 0: ans += tmp % 10 tmp //= 10 return ans def main(a, b, c): ans = [] for i in range(82): x = b * i**a + c if 0 < x < 10**9 and s(x) == i: ans.append(x) print(len(ans)) for i in ans: print(i, end=" ") a, b, c = map(int, input().split()) main(a, b, c)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF NUMBER VAR BIN_OP NUMBER NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
from sys import stdin input = lambda: stdin.readline().strip() ipnut = input for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) def isp(i): if i == 0 or i == n - 1: return False if a[i - 1] < a[i] and a[i + 1] < a[i]: return True return False def isl(i): if i == 0 or i == n - 1: return False if a[i - 1] > a[i] and a[i + 1] > a[i]: return True return False d = 0 ans = 0 def fl1(i): if i == 1 or i == n - 2: return True return a[i + 1] == a[i + 2] def fl2(i): if i == 1 or i == n - 2: return True return a[i - 1] == a[i - 2] def fu1(i): if i == 1 or i == n - 2: return True return a[i + 1] == a[i + 2] def fu2(i): if i == 1 or i == n - 2: return True return a[i - 1] == a[i - 2] fuck = [0] * n for j in range(1, n - 1): fuck[j] = isp(j) or isl(j) ans += fuck[j] ans1 = ans for j in range(1, n - 1): was = a[j] a[j] = a[j + 1] ws = fuck[j - 1] + fuck[j] + fuck[j + 1] bm = (isp(j - 1) or isl(j - 1)) + (isp(j + 1) or isl(j + 1)) ans1 = min(ans1, ans + (bm - ws)) a[j] = a[j - 1] bm = (isp(j - 1) or isl(j - 1)) + (isp(j + 1) or isl(j + 1)) ans1 = min(ans1, ans + (bm - ws)) a[j] = was print(ans1)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def read_int(): return int(input()) def read_ints(): return map(int, input().split(" ")) t = read_int() for case_num in range(t): n = read_int() a = list(read_ints()) def is_hill(i): return a[i] > max(a[i - 1], a[i + 1]) def is_valley(i): return a[i] < min(a[i - 1], a[i + 1]) def is_invalid(i): return 0 < i < n - 1 and (is_hill(i) or is_valley(i)) original = 0 remove = 0 for i in range(1, n - 1): if is_invalid(i): original += 1 before = 0 for k in range(i - 1, i + 2): if is_invalid(k): before += 1 tmp = a[i] for target in [a[i - 1], a[i + 1]]: a[i] = target after = 0 for k in range(i - 1, i + 2): if is_invalid(k): after += 1 remove = max(remove, before - after) a[i] = tmp print(original - remove)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF RETURN NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def count(a, b, c, d, e): ans = 0 if b > a and b > c: ans += 1 if b < a and b < c: ans += 1 if c < b and c < d: ans += 1 if c > b and c > d: ans += 1 if d < c and d < e: ans += 1 if d > c and d > e: ans += 1 return ans for i in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr = [arr[0]] * 2 + arr + [arr[-1]] * 2 n += 4 check = [(False) for i in range(n)] ans = 0 for i in range(2, n - 2): if arr[i - 1] < arr[i] and arr[i + 1] < arr[i]: check[i] = True ans += 1 elif arr[i - 1] > arr[i] and arr[i + 1] > arr[i]: check[i] = True ans += 1 final = ans for i in range(2, n - 2): if not check[i]: continue a, b, c, d, e = arr[i - 2], arr[i - 1], arr[i], arr[i + 1], arr[i + 2] actual = 0 if b > a and b > c: actual += 1 if b < a and b < c: actual += 1 if c < b and c < d: actual += 1 if c > b and c > d: actual += 1 if d < c and d < e: actual += 1 if d > c and d > e: actual += 1 c = b diff1 = 0 if b > a and b > c: diff1 += 1 if b < a and b < c: diff1 += 1 if c < b and c < d: diff1 += 1 if c > b and c > d: diff1 += 1 if d < c and d < e: diff1 += 1 if d > c and d > e: diff1 += 1 c = d diff2 = 0 if b > a and b > c: diff2 += 1 if b < a and b < c: diff2 += 1 if c < b and c < d: diff2 += 1 if c > b and c > d: diff2 += 1 if d < c and d < e: diff2 += 1 if d > c and d > e: diff2 += 1 final = min(final, ans - actual + diff1) final = min(final, ans - actual + diff2) print(final)
FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST VAR NUMBER NUMBER VAR BIN_OP LIST VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
t = int(input()) for e in range(t): n = int(input()) l = input().split() for i in range(n): l[i] = int(l[i]) ma = 0 c = 0 m = 0 l1 = [-1] for i in range(1, n - 1): if l[i] > l[i - 1] and l[i] > l[i + 1]: c = c + 1 m = m + 1 l1.append("H") elif l[i] < l[i - 1] and l[i] < l[i + 1]: c = c + 1 m = m + 1 l1.append("V") else: l1.append(-1) if ma < m: ma = m m = 0 l1.append(-1) if ma < m: ma = m if ma > 3: ma = 3 if ma == 2: nhv = 0 c1 = 0 c2 = 0 l2 = l[:] l3 = l[:] for i in range(1, n - 1): if l1[i] == "H" and l1[i + 1] == "V": nhv = nhv + 1 l2[i] = l2[i - 1] l3[i] = l3[i + 1] for i in range(1, n - 1): if ( l2[i] > l2[i - 1] and l2[i] > l2[i + 1] or l2[i] < l2[i - 1] and l2[i] < l2[i + 1] ): c1 = c1 + 1 if ( l3[i] > l3[i - 1] and l3[i] > l3[i + 1] or l3[i] < l3[i - 1] and l3[i] < l3[i + 1] ): c2 = c2 + 1 if c - nhv > c1 or c - nhv > c2: print(c - 2) continue nhv = 0 c1 = 0 c2 = 0 l2 = l[:] l3 = l[:] for i in range(1, n - 1): if l1[i] == "H" and l1[i + 1] == "V": nhv = nhv + 1 l2[i + 1] = l2[i] l3[i + 1] = l3[i + 2] for i in range(1, n - 1): if ( l2[i] > l2[i - 1] and l2[i] > l2[i + 1] or l2[i] < l2[i - 1] and l2[i] < l2[i + 1] ): c1 = c1 + 1 if ( l3[i] > l3[i - 1] and l3[i] > l3[i + 1] or l3[i] < l3[i - 1] and l3[i] < l3[i + 1] ): c2 = c2 + 1 if c - nhv > c1 or c - nhv > c2: print(c - 2) continue nhv = 0 c1 = 0 c2 = 0 l2 = l[:] l3 = l[:] for i in range(1, n - 1): if l1[i] == "V" and l1[i + 1] == "H": nhv = nhv + 1 l2[i] = l2[i - 1] l3[i] = l3[i + 1] for i in range(1, n - 1): if ( l2[i] > l2[i - 1] and l2[i] > l2[i + 1] or l2[i] < l2[i - 1] and l2[i] < l2[i + 1] ): c1 = c1 + 1 if ( l3[i] > l3[i - 1] and l3[i] > l3[i + 1] or l3[i] < l3[i - 1] and l3[i] < l3[i + 1] ): c2 = c2 + 1 if c - nhv > c1 or c - nhv > c2: print(c - 2) continue nhv = 0 c1 = 0 c2 = 0 l2 = l[:] l3 = l[:] for i in range(1, n - 1): if l1[i] == "V" and l1[i + 1] == "H": nhv = nhv + 1 l2[i + 1] = l2[i] l3[i + 1] = l3[i + 2] for i in range(1, n - 1): if ( l2[i] > l2[i - 1] and l2[i] > l2[i + 1] or l2[i] < l2[i - 1] and l2[i] < l2[i + 1] ): c1 = c1 + 1 if ( l3[i] > l3[i - 1] and l3[i] > l3[i + 1] or l3[i] < l3[i - 1] and l3[i] < l3[i + 1] ): c2 = c2 + 1 if c - nhv > c1 or c - nhv > c2: print(c - 2) continue print(c - 1) else: print(c - ma)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
import sys data = sys.stdin.read().split("\n")[::-1] def input(): return data.pop() def is_bad(arr, i): return ( i > 0 and i < len(arr) - 1 and ( arr[i] > arr[i - 1] and arr[i] > arr[i + 1] or arr[i] < arr[i - 1] and arr[i] < arr[i + 1] ) ) def calc3(arr, i): cnt = 0 for ii in range(i - 1, i + 2): cnt += is_bad(arr, ii) return cnt res = [] for _ in range(int(input())): n = int(input()) (*arr,) = map(int, input().split()) typ = [0] * n for i in range(1, len(arr) - 1): if ( arr[i] > arr[i - 1] and arr[i] > arr[i + 1] or arr[i] < arr[i - 1] and arr[i] < arr[i + 1] ): typ[i] = 1 s = sum(typ) mx = 0 for i in range(1, n - 1): cur = calc3(arr, i) tmp = arr[i] arr[i] = arr[i - 1] b1 = calc3(arr, i) arr[i] = arr[i + 1] b2 = calc3(arr, i) mx = max(mx, cur - b1, cur - b2) arr[i] = tmp res.append(s - mx) print("\n".join(map(str, res)))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
t = int(input()) while t > 0: t -= 1 n = int(input()) a = input().split() a = [int(i) for i in a] intimidation = 0 hov = [(False) for i in range(n)] for i in range(1, n - 1): if a[i] < a[i - 1] and a[i] < a[i + 1]: hov[i] = True intimidation += 1 if a[i] > a[i - 1] and a[i] > a[i + 1]: hov[i] = True intimidation += 1 ans = intimidation for i in range(1, n - 1): if hov[i]: aux, st = intimidation - 1, a[i] a[i] = a[i - 1] if hov[i - 1]: aux -= 1 if i < n - 2: if ( a[i + 1] < a[i] and a[i + 1] < a[i + 2] or a[i + 1] > a[i] and a[i + 1] > a[i + 2] ): if hov[i + 1] == False: aux += 1 elif hov[i + 1] == True: aux -= 1 ans = min(ans, aux) a[i] = a[i + 1] aux = intimidation - 1 if hov[i + 1]: aux -= 1 if i > 1: if ( a[i - 1] < a[i] and a[i - 1] < a[i - 2] or a[i - 1] > a[i] and a[i - 1] > a[i - 2] ): if hov[i - 1] == False: aux += 1 elif hov[i - 1]: aux -= 1 ans = min(ans, aux) a[i] = st print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
import sys input = sys.stdin.buffer.readline def ishill(b, a): if a == 0 or a == len(b) - 1: return 0 if b[a] > b[a - 1] and b[a] > b[a + 1]: return 1 elif b[a] < b[a - 1] and b[a] < b[a + 1]: return 1 else: return 0 a = int(input()) for x in range(a): b = int(input()) c = list(map(int, input().split())) j = 0 l = 0 d = {} for y in range(1, b - 1): if ishill(c, y): l += 1 if d.get(y) == None: d[y] = 1 ans = l for y in d: old = c[y] price = ishill(c, y) + ishill(c, y - 1) + ishill(c, y + 1) c[y] = c[y + 1] ans = min(ans, l - price + ishill(c, y) + ishill(c, y + 1) + ishill(c, y - 1)) c[y] = c[y - 1] ans = min(ans, l - price + ishill(c, y) + ishill(c, y + 1) + ishill(c, y - 1)) c[y] = old print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
for i in range(int(input())): n = int(input()) arr = list(map(int, input().split())) ans = 0 c = 0 ma = 0 for i in range(1, n - 1): c = arr[i] cntn = 0 for j in range(max(1, i - 1), min(i + 2, n - 1)): if arr[j - 1] > arr[j] and arr[j + 1] > arr[j]: cntn += 1 elif arr[j - 1] < arr[j] and arr[j] > arr[j + 1]: cntn += 1 if arr[i - 1] > arr[i] and arr[i + 1] > arr[i]: ans += 1 arr[i] = max(arr[i - 1], arr[i + 1]) elif arr[i - 1] < arr[i] and arr[i] > arr[i + 1]: ans += 1 arr[i] = min(arr[i - 1], arr[i + 1]) cnt = 0 for j in range(max(1, i - 1), min(i + 2, n - 1)): if arr[j - 1] > arr[j] and arr[j + 1] > arr[j]: cnt += 1 elif arr[j - 1] < arr[j] and arr[j] > arr[j + 1]: cnt += 1 arr[i] = c ma = max(ma, cntn - cnt) c = arr[i] cntn = 0 for j in range(max(1, i - 1), min(i + 2, n - 1)): if arr[j - 1] > arr[j] and arr[j + 1] > arr[j]: cntn += 1 elif arr[j - 1] < arr[j] and arr[j] > arr[j + 1]: cntn += 1 if arr[i - 1] > arr[i] and arr[i + 1] > arr[i]: arr[i] = min(arr[i - 1], arr[i + 1]) elif arr[i - 1] < arr[i] and arr[i] > arr[i + 1]: arr[i] = max(arr[i - 1], arr[i + 1]) cnt = 0 for j in range(max(1, i - 1), min(i + 2, n - 1)): if arr[j - 1] > arr[j] and arr[j + 1] > arr[j]: cnt += 1 elif arr[j - 1] < arr[j] and arr[j] > arr[j + 1]: cnt += 1 arr[i] = c ma = max(ma, cntn - cnt) print(ans - ma)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n = int(input()) A = list(map(int, input().split())) A.append(A[-1]) A.append(A[0]) X = [0] * n for i in range(1, n - 1): if A[i - 1] < A[i] and A[i] > A[i + 1]: X[i] = 1 elif A[i - 1] > A[i] and A[i] < A[i + 1]: X[i] = 1 S = sum(X) ANS = S for i in range(1, n - 1): T = S - X[i] - X[i - 1] - X[i + 1] if A[i - 1] > A[i + 1] and A[i + 1] < A[i + 2]: T += 1 elif A[i - 1] < A[i + 1] and A[i + 1] > A[i + 2]: T += 1 ANS = min(ANS, T) T = S - X[i] - X[i - 1] - X[i + 1] if A[i - 2] > A[i - 1] and A[i - 1] < A[i + 1]: T += 1 elif A[i - 2] < A[i - 1] and A[i - 1] > A[i + 1]: T += 1 ANS = min(ANS, T) print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def f(i, j, k): return ( a[i] < a[j] > a[k] or a[i] > a[j] < a[k] if 0 <= i < n and 0 <= j < n and 0 <= k < n else 0 ) for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) m = sum(f(i - 1, i, i + 1) for i in range(n)) o = 0 for i in range(n): o = min( o, f(i - 2, i - 1, i - 1) + f(i - 1, i - 1, i + 1) + f(i - 1, i + 1, i + 2) - f(i - 2, i - 1, i) - f(i - 1, i, i + 1) - f(i, i + 1, i + 2), f(i - 2, i - 1, i + 1) + f(i - 1, i + 1, i + 1) + f(i + 1, i + 1, i + 2) - f(i - 2, i - 1, i) - f(i - 1, i, i + 1) - f(i, i + 1, i + 2), ) print(m + o)
FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
from sys import stdin def readGenerator(): while True: tokens = stdin.readline().strip().split(" ") for t in tokens: yield t reader = readGenerator() def readWord(): return next(reader) def readInt(): return int(next(reader)) def readFloat(): return float(next(reader)) def readLine(): return input() def fact(n): return n * fact(n - 1) if n > 1 else n def gcd(a, b): while b: a, b = b, a % b return a def count(a, n, left, right): ans = 0 for i in range(max(1, left), min(n - 1, right)): if a[i - 1] > a[i] < a[i + 1] or a[i - 1] < a[i] > a[i + 1]: ans += 1 return ans def solve(n, a): ans = count(a, n, 1, n - 1) r = ans for i in range(n): r1 = count(a, n, i - 2, i + 3) r2 = r1 r3 = r1 t = a[i] if i > 0: a[i] = a[i - 1] r2 = count(a, n, i - 2, i + 3) if i < n - 1: a[i] = a[i + 1] r3 = count(a, n, i - 2, i + 3) a[i] = t r = min(r, ans - (r1 - min(r2, r3))) return r t = readInt() for _ in range(t): n = readInt() a = [readInt() for _ in range(n)] print(solve(n, a))
FUNC_DEF WHILE NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR EXPR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
for _ in range(int(input())): n = int(input()) l = [int(x) for x in input().split()] m = [0] * n a3 = 0 a2 = 0 a1 = 0 for i in range(1, n - 1): if l[i] > l[i - 1] and l[i] > l[i + 1]: m[i] = 2 a1 = 1 if i > 1 and m[i - 1] == 1 and m[i - 2] == 2: a3 = 1 elif m[i - 1] == 1: if ( i < n - 2 and l[i - 1] < l[i + 1] and l[i + 1] != l[i + 2] and i > 2 and l[i - 2] < l[i] and l[i - 2] != l[i - 3] ): a2 = a2 else: a2 = 1 elif l[i] < l[i - 1] and l[i] < l[i + 1]: m[i] = 1 a1 = 1 if i > 1 and m[i - 1] == 2 and m[i - 2] == 1: a3 = 1 elif m[i - 1] == 2: if ( i < n - 2 and l[i - 1] > l[i + 1] and l[i + 1] != l[i + 2] and i > 2 and l[i - 2] > l[i] and l[i - 2] != l[i - 3] ): a2 = a2 else: a2 = 1 summ = n - m.count(0) if a3 == 1: print(summ - 3) elif a2 == 1: print(summ - 2) elif summ == 0: print(summ) else: print(summ - 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def bad(a, b, c): return a < b and a < c or a > b and a > c for tt in range(int(input())): n = int(input()) l = list(map(int, input().split())) ans = 0 cnt = 0 b = [(0) for i in range(n)] for i in range(1, n - 1): b[i] = bad(l[i], l[i - 1], l[i + 1]) cnt += b[i] ans = cnt for i in range(1, n - 1): op = l[i - 1] s = 0 if i + 2 < n: s += bad(l[i + 1], op, l[i + 2]) if i - 2 > 0: s += bad(l[i - 1], op, l[i - 2]) ans = min( ans, cnt - b[i - 1] - b[i] - b[i + 1] + s + bad(op, l[i - 1], l[i + 1]) ) op = l[i + 1] s = 0 if i + 2 < n: s += bad(l[i + 1], op, l[i + 2]) if i - 2 >= 0: s += bad(l[i - 1], op, l[i - 2]) ans = min( ans, cnt - b[i - 1] - b[i] - b[i + 1] + s + bad(op, l[i - 1], l[i + 1]) ) print(ans)
FUNC_DEF RETURN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
n = 0 def is_hv(a, j): return ( 1 if 0 < j < n - 1 and (a[j] < a[j - 1] and a[j] < a[j + 1] or a[j] > a[j - 1] and a[j] > a[j + 1]) else 0 ) t = int(input()) for i in range(t): input() a = list(map(int, input().split())) n = len(a) c = 0 b = [0] * n for j in range(1, n - 1): if is_hv(a, j): b[j] = 1 c += 1 r = c for j in range(1, n - 1): temp = a[j] a[j] = a[j - 1] r = min( r, c - b[j - 1] - b[j] - b[j + 1] + is_hv(a, j - 1) + is_hv(a, j) + is_hv(a, j + 1), ) a[j] = a[j + 1] r = min( r, c - b[j - 1] - b[j] - b[j + 1] + is_hv(a, j - 1) + is_hv(a, j) + is_hv(a, j + 1), ) a[j] = temp print(r)
ASSIGN VAR NUMBER FUNC_DEF RETURN NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR 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 NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
ans = [] for _ in range(int(input())): n = int(input()) u = list(map(int, input().split())) p = [0] * n cnt = 0 for i in range(1, n - 1): if u[i - 1] > u[i] < u[i + 1]: p[i] = 1 cnt += 1 elif u[i - 1] < u[i] > u[i + 1]: p[i] = 2 cnt += 1 for i in range(1, n - 1): if p[i] == 1 and p[i - 1] == p[i + 1] == 2: cnt -= 3 break if p[i] == 2 and p[i - 1] == p[i + 1] == 1: cnt -= 3 break else: for i in range(1, n - 1): if p[i] == 1 and p[i + 1] == 2: a, b, c, d = u[i - 1 : i + 3] if a >= c >= d or a >= b >= d: cnt -= 2 break if i - 1 == 0 or i + 2 == n - 1: cnt -= 2 break if u[i - 2] == u[i - 1] or u[i + 2] == u[i + 3]: cnt -= 2 break if p[i] == 2 and p[i + 1] == 1: a, b, c, d = u[i - 1 : i + 3] if a <= c <= d or a <= b <= d: cnt -= 2 break if i - 1 == 0 or i + 2 == n - 1: cnt -= 2 break if u[i - 2] == u[i - 1] or u[i + 2] == u[i + 3]: cnt -= 2 break else: if cnt > 0: cnt -= 1 ans.append(cnt) print("\n".join(map(str, ans)))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
temp = int(input()) for eqw in range(temp): t = int(input()) li = list(map(int, input().split(" "))) if t < 3: print("0") continue temp = [0] for i in range(1, t - 1): if li[i] > li[i + 1] and li[i] > li[i - 1]: temp.append(1) elif li[i] < li[i + 1] and li[i] < li[i - 1]: temp.append(1) else: temp.append(0) temp.append(0) minus = max(temp[0] + temp[1] + temp[2], temp[t - 1] + temp[t - 3] + temp[t - 2]) for i in range(2, t - 2): te = 0 tot = temp[i + 1] + temp[i - 1] + temp[i] y = li[i - 1] if li[i + 1] > y and li[i + 1] > li[i + 2]: te = max(te, tot - 1) elif li[i + 1] < y and li[i + 1] < li[i + 2]: te = max(te, tot - 1) else: te = tot y = li[i + 1] if li[i - 1] > y and li[i - 1] > li[i - 2]: te = max(te, tot - 1) elif li[i - 1] < y and li[i - 1] < li[i - 2]: te = max(te, tot - 1) else: te = tot minus = max(te, minus) ans = sum(temp) - minus print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def readInts(): return list(map(int, input().split())) def readInt(): return int(input()) t = readInt() for _ in range(t): n = readInt() a = readInts() b = [a[i] for i in range(n)] ans = 0 dlt = 0 for i in range(1, n - 1): if a[i - 1] > a[i] < a[i + 1]: ans += 1 elif a[i - 1] < a[i] > a[i + 1]: ans += 1 for i in range(n): cur = a[i] pre = a[i] if i == 0 else a[i - 1] nxt = a[i] if i == n - 1 else a[i + 1] for value in [pre, nxt, cur]: b[i] = value was = 0 now = 0 for x in range(max(1, i - 3), min(n - 2, i + 3) + 1): if a[x - 1] < a[x] > a[x + 1]: was += 1 elif a[x - 1] > a[x] < a[x + 1]: was += 1 if b[x - 1] < b[x] > b[x + 1]: now += 1 elif b[x - 1] > b[x] < b[x + 1]: now += 1 dlt = max(dlt, was - now) print(ans - dlt)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR LIST VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def hv_test(a, b, c): if a < b > c or a > b < c: return True else: return False t = int(input()) for _ in range(t): n = int(input()) arr = list(map(lambda x: int(x), input().split())) is_hv = [0] * n ans = 0 if n > 2: i = 1 while i < n - 1: if hv_test(arr[i - 1], arr[i], arr[i + 1]): is_hv[i] = 1 ans += 1 i += 1 total = ans i = 1 while i < n - 1: ans1 = total ans2 = total if is_hv[i]: tmp = 1 x = arr[i - 1] i -= 1 if i - 1 >= 0: t1 = is_hv[i] t2 = hv_test(arr[i - 1], arr[i], x) if t1 and not t2: tmp += 1 elif not t1 and t2: tmp -= 1 i += 2 if i + 1 < n: t1 = is_hv[i] t2 = hv_test(x, arr[i], arr[i + 1]) if t1 and not t2: tmp += 1 elif not t1 and t2: tmp -= 1 i -= 1 ans1 = min(ans1, total - tmp) tmp = 1 x = arr[i + 1] i -= 1 if i - 1 >= 0: t1 = is_hv[i] t2 = hv_test(arr[i - 1], arr[i], x) if t1 and not t2: tmp += 1 elif not t1 and t2: tmp -= 1 i += 2 if i + 1 < n: t1 = is_hv[i] t2 = hv_test(x, arr[i], arr[i + 1]) if t1 and not t2: tmp += 1 elif not t1 and t2: tmp -= 1 i -= 1 ans2 = min(ans2, total - tmp) ans = min(ans, ans1, ans2) i += 1 print(ans)
FUNC_DEF IF VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
from sys import stdin input = stdin.readline t = int(input()) def check(a, i): if i == 0 or i == len(a) - 1: return 0 return (a[i] - a[i - 1]) * (a[i] - a[i + 1]) > 0 for _ in range(t): n = int(input()) a = list(map(int, input().split())) res = 0 cnt = 0 for i in range(1, len(a) - 1): res += check(a, i) l = r = check(a, i - 1) + check(a, i) + check(a, i + 1) temp = a[i] a[i] = a[i - 1] l -= check(a, i + 1) a[i] = a[i + 1] r -= check(a, i - 1) a[i] = temp cnt = max(cnt, l, r) print(res - cnt)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
t = int(input()) for _ in range(t): def main(): n = int(input()) nums = list(map(int, input().split())) if n <= 4: return print(0) def hill(p): return nums[p] > nums[p - 1] and nums[p] > nums[p + 1] def valley(p): return nums[p] < nums[p - 1] and nums[p] < nums[p + 1] def left(p): return nums[p - 1] > nums[p] > nums[p + 1] def right(p): return nums[p - 1] < nums[p] < nums[p + 1] def hv(p): return hill(p) or valley(p) def ta(p, x): pre = nums[p] a = hv(p - 1) + hv(p) + hv(p + 1) nums[p] = x b = hv(p - 1) + hv(p) + hv(p + 1) nums[p] = pre return a - b ans = sum(hv(i) for i in range(1, n - 1)) m = max(hv(1) + hv(2), hv(n - 3) + hv(n - 2)) for i in range(2, n - 2): if m == 3: break if hill(i): if valley(i - 1): if valley(i + 1): cur = 3 else: cur = ta(i, min(nums[i - 1], nums[i + 1])) elif valley(i + 1): cur = ta(i, min(nums[i - 1], nums[i + 1])) else: cur = 1 if cur > m: m = cur elif valley(i): if hill(i - 1): if hill(i + 1): cur = 3 else: cur = ta(i, max(nums[i - 1], nums[i + 1])) elif hill(i + 1): cur = ta(i, max(nums[i - 1], nums[i + 1])) else: cur = 1 if cur > m: m = cur print(ans - m) main()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_DEF 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 RETURN FUNC_CALL VAR NUMBER FUNC_DEF RETURN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
for _ in range(int(input())): n = int(input()) a = [int(i) for i in input().split()] hills_and_valleys = [0] * n for ind in range(1, n - 1): if ( a[ind - 1] < a[ind] and a[ind + 1] < a[ind] or a[ind - 1] > a[ind] and a[ind + 1] > a[ind] ): hills_and_valleys[ind] = 1 sum_of_hills_and_valleys = sum(hills_and_valleys) if n >= 2: left_num_and_right_num = [[a[0], a[1]]] else: left_num_and_right_num = [[a[0], a[0]]] for ind in range(1, n - 1): left_num_and_right_num.append([a[ind - 1], a[ind + 1]]) if n >= 2: left_num_and_right_num.append([a[-2], a[-1]]) num_of_deleted_ones = 0 for ind in range(1, n - 1): if ( hills_and_valleys[ind - 1] == hills_and_valleys[ind] == hills_and_valleys[ind + 1] == 1 ): num_of_deleted_ones = 3 break elif hills_and_valleys[ind] == 1: ones = sum(hills_and_valleys[ind - 1 : ind + 2]) if ( a[ind - 1] < a[ind + 1] > left_num_and_right_num[ind + 1][1] or a[ind - 1] > a[ind + 1] < left_num_and_right_num[ind + 1][1] ): num_of_deleted_ones = max(num_of_deleted_ones, ones - 1) else: num_of_deleted_ones = max(num_of_deleted_ones, ones) if ( left_num_and_right_num[ind - 1][0] < a[ind - 1] > a[ind + 1] or left_num_and_right_num[ind - 1][0] > a[ind - 1] < a[ind + 1] ): num_of_deleted_ones = max(num_of_deleted_ones, ones - 1) else: num_of_deleted_ones = max(num_of_deleted_ones, ones) print(sum_of_hills_and_valleys - num_of_deleted_ones)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
t = int(input()) def check2(i): if i + 1 < n: if a[i - 2] > a[i] < a[i + 1] or a[i - 2] < a[i] > a[i + 1]: if dp[i] == 0: return -1 else: return 0 elif dp[i] == 1: return 1 else: return 0 return 0 def check1(i): if i - 1 >= 0: if a[i + 2] > a[i] < a[i - 1] or a[i + 2] < a[i] > a[i - 1]: if dp[i] == 0: return -1 else: return 0 elif dp[i] == 1: return 1 else: return 0 return 0 for _ in range(t): n = int(input()) mx = 0 a = list(map(int, input().split())) c = 0 dp = [0] * n for i in range(1, n - 1): if a[i - 1] < a[i] > a[i + 1] or a[i - 1] > a[i] < a[i + 1]: dp[i] = 1 c += 1 rd = 0 mx = 0 for i in range(1, n - 1): if dp[i] == 1: rd = max(1 + dp[i + 1] + check1(i - 1), 1 + dp[i - 1] + check2(i + 1)) mx = max(rd, mx) print(c - mx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def solve(a): hv = set() tot = 0 for i in range(1, len(a) - 1): if a[i] > a[i - 1] and a[i] > a[i + 1] or a[i] < a[i - 1] and a[i] < a[i + 1]: hv.add(i) tot += 1 Max = 0 for i in range(len(a)): left = 0 right = 0 if i - 1 >= 0: if i in hv: left = 1 if i + 2 < len(a): if i + 1 in hv and not ( a[i - 1] > a[i + 1] and a[i + 2] > a[i + 1] or a[i - 1] < a[i + 1] and a[i + 2] < a[i + 1] ): left += 1 elif i + 1 not in hv and ( a[i - 1] > a[i + 1] and a[i + 2] > a[i + 1] or a[i - 1] < a[i + 1] and a[i + 2] < a[i + 1] ): left -= 1 if i - 1 in hv: left += 1 if i + 1 < len(a): if i in hv: right = 1 if i - 2 >= 0: if i - 1 in hv and not ( a[i - 2] > a[i - 1] and a[i + 1] > a[i - 1] or a[i - 2] < a[i - 1] and a[i + 1] < a[i - 1] ): right += 1 elif i - 1 not in hv and ( a[i - 2] > a[i - 1] and a[i + 1] > a[i - 1] or a[i - 2] < a[i - 1] and a[i + 1] < a[i - 1] ): right -= 1 if i + 1 in hv: right += 1 if left >= right and left > Max: Max = left elif right > left and right > Max: Max = right return tot - Max t = int(input("")) ans = [] while t > 0: n = int(input("")) a = list(map(int, input("").split(" "))) ans.append(solve(a)) t -= 1 for i in ans: print(i)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
from sys import stdin input = stdin.readline def get_unit_score(l, i): if i <= 0 or i + 1 >= len(l): return 0 return l[i - 1] > l[i] and l[i + 1] > l[i] or l[i - 1] < l[i] and l[i + 1] < l[i] def solve(): n = int(input()) l = [int(a) for a in input().split()] scores = [get_unit_score(l, i) for i in range(len(l))] score = sum(scores) best_score = score for i in range(len(l)): orig = l[i] s_old = sum([get_unit_score(l, j) for j in range(i - 1, i + 2)]) for j in [i - 1, i + 1]: if j >= 0 and j < len(l): for x in [-1, 0, 1]: l[i] = l[j] + x s_new = sum([get_unit_score(l, jj) for jj in range(i - 1, i + 2)]) best_score = min(best_score, score - s_old + s_new) l[i] = orig print(best_score) return t = int(input()) for _ in range(t): solve()
ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
t = int(input().strip()) for _ in range(t): n = int(input().strip()) a = list(map(int, input().strip().split())) d = [((a[i] < a[i + 1]) - (a[i] > a[i + 1])) for i in range(n - 1)] vh = [(d[i] * d[i + 1]) for i in range(n - 2)] D = vh.count(-1) if D < 2: print(0) continue if any(vh[i : i + 3] == [-1, -1, -1] for i in range(n - 4)): print(D - 3) else: x = [i for i in range(n - 3) if vh[i : i + 2] == [-1, -1]] for i in x: if ( i == 0 or i == n - 4 or i > 0 and a[i] == a[i - 1] or i < n - 4 and a[i + 3] == a[i + 4] or a[i] < a[i + 1] > a[i + 2] >= a[i] or a[i] > a[i + 1] < a[i + 2] <= a[i] or a[i + 1] < a[i + 2] > a[i + 3] <= a[i + 1] or a[i + 1] > a[i + 2] < a[i + 3] >= a[i + 1] ): print(D - 2) break else: print(D - 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR 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 VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) cnt = 0 change = [False] * n ans = 0 for i in range(1, n - 1): p = 0 if a[i - 1] > a[i] < a[i + 1]: cnt += 1 change[i] = True if a[i - 1] < a[i] > a[i + 1]: cnt += 1 change[i] = True k = 0 m = 0 if i + 2 < n: if a[i] < a[i + 1] > a[i + 2] or a[i] > a[i + 1] < a[i + 2]: k += 1 if i - 2 >= 0: if change[i - 1]: if not ( a[i - 2] < a[i - 1] > a[i + 1] or a[i - 2] > a[i - 1] < a[i + 1] ): k += 1 elif a[i - 2] < a[i - 1] > a[i + 1] or a[i - 2] > a[i - 1] < a[i + 1]: k -= 1 if i - 2 >= 0: if change[i - 1]: m += 1 if i + 2 < n: if a[i] < a[i + 1] > a[i + 2] or a[i] > a[i + 1] < a[i + 2]: if not ( a[i - 1] < a[i + 1] > a[i + 2] or a[i - 1] > a[i + 1] < a[i + 2] ): m += 1 elif a[i - 1] < a[i + 1] > a[i + 2] or a[i - 1] > a[i + 1] < a[i + 2]: m -= 1 if change[i]: p += 1 p += max(m, k) ans = max(ans, p) print(cnt - ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = [0] * n cur = 0 for i in range(1, n - 1): if a[i] < a[i + 1] and a[i] < a[i - 1]: b[i] = 1 if a[i] > a[i + 1] and a[i] > a[i - 1]: b[i] = 1 cur += b[i] ans = cur for i in range(1, n - 1): sav = a[i] for j in range(i - 1, i + 2): a[i] = a[j] tmp = cur for k in range(i - 1, i + 2): if k == 0 or k == n - 1: continue tmp -= b[k] if a[k] < a[k + 1] and a[k] < a[k - 1]: tmp += 1 if a[k] > a[k + 1] and a[k] > a[k - 1]: tmp += 1 ans = min(ans, tmp) a[i] = sav print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
import sys input = sys.stdin.readline def check(i): if i == 0 or i == n - 1: return False if a[i] > a[i - 1] and a[i] > a[i + 1]: return True if a[i] < a[i - 1] and a[i] < a[i + 1]: return True return False for nt in range(int(input())): n = int(input()) a = list(map(int, input().split())) ans = 0 hv = [0] for i in range(1, n - 1): if a[i] > a[i - 1] and a[i] > a[i + 1]: ans += 1 hv.append(1) elif a[i] < a[i - 1] and a[i] < a[i + 1]: ans += 1 hv.append(1) else: hv.append(0) hv.append(0) s = ans for i in range(1, n - 1): if not hv[i]: continue ini = a[i] x = [hv[i - 1], hv[i], hv[i + 1]].count(1) a[i] = a[i + 1] c = 0 if check(i - 1): c += 1 if check(i): c += 1 if check(i + 1): c += 1 ans = min(ans, s - x + c) a[i] = ini a[i] = a[i - 1] c = 0 if check(i - 1): c += 1 if check(i): c += 1 if check(i + 1): c += 1 ans = min(ans, s - x + c) a[i] = ini print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL LIST VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
( lambda r, c: [ ( lambda p, n: ( lambda m: print( sum(c(p, q) for p, q in zip(m, m[1:])) - max( ( c(p, q) + c(q, r) + c(r, s) - min(c(r + q, s), c(p, q + r)) for p, q, r, s in zip(m, m[1:], m[2:], m[3:]) ), default=0, ) ) )([0, *(n[i - 1] - k for i, k in enumerate(n) if 0 < i), 0]) )(int(r()), list(map(int, r().split()))) for t in range(int(r())) ] )(__import__("sys").stdin.readline, lambda a, b: a * b < 0)
EXPR FUNC_CALL FUNC_CALL FUNC_CALL FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER LIST NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP VAR VAR NUMBER
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
t = int(input()) for test in range(t): n = int(input()) a = [int(i) for i in input().split()] tot = 0 m_dim = 0 def ishill(it): if it > 0 and it < n - 1: if ( a[it] < a[it - 1] and a[it] < a[it + 1] or a[it] > a[it - 1] and a[it] > a[it + 1] ): return 1 return 0 hlist = [0, 0, ishill(1)] for i in range(1, n - 1): hlist = [hlist[1], hlist[2], ishill(i + 1)] su = sum(hlist) tot += hlist[1] oldita = a[i] a[i] = a[i + 1] hlist2 = ishill(i - 1) m_dim = max(m_dim, su - hlist2) a[i] = a[i - 1] hlist2 = ishill(i + 1) m_dim = max(m_dim, su - hlist2) a[i] = oldita print(tot - m_dim)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
t = int(input()) for q in range(t): n = int(input()) mass = [int(x) for x in input().split()] answer = 0 Max_izm = 0 for i in range(n - 4): a = 0 b = 0 c = 0 if (mass[i + 1] - mass[i]) * (mass[i + 1] - mass[i + 2]) > 0: if i == 0: answer += 1 a = 1 if (mass[i + 2] - mass[i + 1]) * (mass[i + 2] - mass[i + 3]) > 0: if i == 0: answer += 1 b = 1 if (mass[i + 3] - mass[i + 2]) * (mass[i + 3] - mass[i + 4]) > 0: answer += 1 c = 1 f1 = a + b f2 = b + c if (mass[i + 3] - mass[i + 1]) * (mass[i + 3] - mass[i + 4]) <= 0 and c == 1: f1 += 1 if (mass[i + 3] - mass[i + 1]) * (mass[i + 3] - mass[i + 4]) > 0 and c == 0: f1 -= 1 if (mass[i + 1] - mass[i + 3]) * (mass[i + 1] - mass[i]) <= 0 and a == 1: f2 += 1 if (mass[i + 1] - mass[i + 3]) * (mass[i + 1] - mass[i]) > 0 and a == 0: f2 -= 1 Max_izm = max(f1, f2, Max_izm) if i == 0: Max_izm = max(a + b, Max_izm) if i == n - 5: Max_izm = max(b + c, Max_izm) print(answer - Max_izm)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def Int_check(a, b, c): if a < b and b > c: return 1 elif a > b and b < c: return 1 else: return 0 t = int(input()) for z in range(t): n = int(input()) l = [int(i) for i in input().split()] if n >= 5: Int = 0 Red = 0 b = Int_check(l[0], l[1], l[2]) if b == 1: Int += 1 Red = b + Int_check(l[1], l[2], l[3]) for i in range(2, n - 2): b = Int_check(l[i - 1], l[i], l[i + 1]) if b == 1: Int += 1 if Red < 3: s = ( Int_check(l[i - 2], l[i - 1], l[i]) + b + Int_check(l[i], l[i + 1], l[i + 2]) ) x = Int_check(l[i - 2], l[i - 1], l[i + 1]) y = Int_check(l[i - 1], l[i + 1], l[i + 2]) Red = max(Red, s - x, s - y) b = Int_check(l[n - 3], l[n - 2], l[n - 1]) if b == 1: Int += 1 Red = max(Red, b + Int_check(l[n - 4], l[n - 3], l[n - 2])) print(Int - Red) else: print(0)
FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
T = int(input()) for _ in range(T): N = int(input()) A = list(map(int, input().split())) if N == 1: ans = 0 elif N == 2: ans = 0 else: res = 0 for i in range(1, N - 1): if (A[i - 1] - A[i]) * (A[i] - A[i + 1]) < 0: res += 1 ans = res for x in range(1, N - 1): L = x - 2 if 1 < x else 0 R = x + 2 if x < N - 1 else N - 1 tmp = A[x] li_original = A[L : R + 1] A[x] = A[x - 1] li_left = A[L : R + 1] A[x] = A[x + 1] li_right = A[L : R + 1] A[x] = tmp res0, res1, res2 = 0, 0, 0 for i in range(1, len(li_original) - 1): if (li_original[i - 1] - li_original[i]) * ( li_original[i] - li_original[i + 1] ) < 0: res0 += 1 if (li_right[i - 1] - li_right[i]) * ( li_right[i] - li_right[i + 1] ) < 0: res1 += 1 if (li_left[i - 1] - li_left[i]) * (li_left[i] - li_left[i + 1]) < 0: res2 += 1 ans = min(ans, res - res0 + res1, res - res0 + res2) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def sgn(x): return (x > 0) - (x < 0) t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) a = [a[0]] * 2 + a + [a[-1]] * 2 it = [] ita = it.append chg = 0 for i in range(2, n + 2): ita(a[i - 1] < a[i] > a[i + 1] or a[i - 1] > a[i] < a[i + 1]) if chg == 0 and it[-1]: chg = 1 if chg < 3 and i > 2 and it[-1] and it[-2] and it[-3]: chg = 3 if chg < 2 and i > 4 and not it[-1] and it[-2] and it[-3] and not it[-4]: chg = 1 + ( a[i - 2] <= a[i] <= a[i + 1] or a[i - 4] <= a[i - 3] <= a[i - 1] or a[i - 2] >= a[i] >= a[i + 1] or a[i - 4] >= a[i - 3] >= a[i - 1] ) ans = sum(it) - chg print(ans)
FUNC_DEF RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST VAR NUMBER NUMBER VAR BIN_OP LIST VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def ispeak(a, i, n): if i == 0 or i == n - 1: return 0 elif a[i] > a[i - 1] and a[i] > a[i + 1] or a[i] < a[i - 1] and a[i] < a[i + 1]: return 1 return 0 def answer(n, a): if n <= 3: return 0 peak = [(0) for i in range(n)] p = 0 for i in range(1, n - 1): if ispeak(a, i, n): peak[i] = 1 p += 1 ans = p for i in range(1, n - 1): x = a[i] a[i] = a[i - 1] ans = min( p - peak[i - 1] - peak[i] - peak[i + 1] + ispeak(a, i - 1, n) + ispeak(a, i, n) + ispeak(a, i + 1, n), ans, ) a[i] = a[i + 1] ans = min( p - peak[i - 1] - peak[i] - peak[i + 1] + ispeak(a, i - 1, n) + ispeak(a, i, n) + ispeak(a, i + 1, n), ans, ) a[i] = x return ans t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] print(answer(n, a))
FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def hv(l, i): if l[i] > l[i - 1] and l[i] > l[i + 1]: return 1 elif l[i] < l[i - 1] and l[i] < l[i + 1]: return 1 else: return 0 for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) if n <= 4: print(0) else: l1 = [] l1.append(0) for i in range(1, n - 1): l1.append(hv(l, i)) l1.append(0) ans = n s = l1.count(1) ans = min(ans, s - (l1[0] + l1[1] + l1[2])) for i in range(2, n - 2): old = l[i] l[i] = l[i - 1] a1 = max(0, hv(l, i - 1)) a2 = max(0, hv(l, i)) a3 = max(0, hv(l, i + 1)) a = a1 + a2 + a3 ans = min(ans, s - (l1[i - 1] + l1[i] + l1[i + 1] - a)) l[i] = l[i + 1] a1 = max(0, hv(l, i - 1)) a2 = max(0, hv(l, i)) a3 = max(0, hv(l, i + 1)) a = a1 + a2 + a3 ans = min(ans, s - (l1[i - 1] + l1[i] + l1[i + 1] - a)) l[i] = old ans = min(ans, s - (l1[n - 1] + l1[n - 2] + l1[n - 3])) print(ans)
FUNC_DEF IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
from sys import stdin, stdout def ii(): return int(stdin.readline().strip()) def si(): return stdin.readline().strip() def li(): return list(map(int, stdin.readline().strip().split())) def mi(): return map(int, stdin.readline().strip().split()) def tool1(a, b, c, d, e): l = [] if b > c and b > a or b < c and b < a: l.append(True) else: l.append(False) if c > b and c > d or c < b and c < d: l.append(True) else: l.append(False) if d > c and d > e or d < e and d < c: l.append(True) else: l.append(False) return l def tool2(a, b, c, d): l = [] if b > c and b > a or b < c and b < a: l.append(True) else: l.append(False) if c > b and c > d or c < b and c < d: l.append(True) else: l.append(False) return l t = ii() for _ in range(t): n = ii() a = li() hill = 0 valley = 0 hv = [False] * n for i in range(1, n - 1): if a[i] > a[i + 1] and a[i] > a[i - 1]: hill += 1 hv[i] = True elif a[i] < a[i + 1] and a[i] < a[i - 1]: valley += 1 hv[i] = True oans = hill + valley if oans == 0: print(0) else: ans = oans if n == 1 or n == 2 or n == 3: print(0) else: for i in range(1, n - 1): if i == 1: otm = 0 if hv[i] == True: otm += 1 if hv[i + 1] == True: otm += 1 tm = min( tool2(a[0], a[0], a[2], a[3]).count(True), tool2(a[0], a[2], a[2], a[3]).count(True), ) elif i == n - 2: otm = 0 if hv[i] == True: otm += 1 if hv[i - 1] == True: otm += 1 tm = min( tool2(a[n - 4], a[n - 3], a[n - 1], a[n - 1]).count(True), tool2(a[n - 4], a[n - 3], a[n - 3], a[n - 1]).count(True), ) else: otm = 0 if hv[i] == True: otm += 1 if hv[i - 1] == True: otm += 1 if hv[i + 1] == True: otm += 1 tm = min( tool1(a[i - 2], a[i - 1], a[i - 1], a[i + 1], a[i + 2]).count( True ), tool1(a[i - 2], a[i - 1], a[i + 1], a[i + 1], a[i + 2]).count( True ), ) ans = min(ans, oans - (otm - tm)) if otm - tm == 3: break print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
import sys input = sys.stdin.readline def check(i): global ar, n if i <= 0 or i >= n - 1: return 0 elif ar[i] > ar[i - 1] and ar[i] > ar[i + 1]: return 1 elif ar[i] < ar[i - 1] and ar[i] < ar[i + 1]: return 1 else: return 0 for _ in range(int(input())): n = int(input()) ar = list(map(int, input().split())) li = [0] * n c1 = 0 c2 = 0 for i in range(1, n - 1): if ar[i] > ar[i - 1] and ar[i] > ar[i + 1]: li[i] = 1 c1 += 1 elif ar[i] < ar[i - 1] and ar[i] < ar[i + 1]: li[i] = -1 c2 += 1 ans = c1 + c2 main = [ans] for i in range(1, n - 1): pre = li[i - 1] nex = li[i + 1] cur = li[i] if pre != 0 and cur != 0 and nex != 0: main.append(ans - 3) elif cur != 0 and nex == 0 and pre == 0: main.append(ans - 1) elif cur != 0 and (nex != 0 or pre != 0): co = ar[i] su = abs(li[i - 1]) + abs(li[i]) + abs(li[i + 1]) ar[i] = ar[i - 1] ne = check(i - 1) + check(i) + check(i + 1) main.append(ans - max(su - ne, 0)) ar[i] = ar[i + 1] ne = check(i - 1) + check(i) + check(i + 1) main.append(ans - max(su - ne, 0)) ar[i] = ar[i - 1] - 1 ne = check(i - 1) + check(i) + check(i + 1) main.append(ans - max(su - ne, 0)) ar[i] = ar[i - 1] + 1 ne = check(i - 1) + check(i) + check(i + 1) main.append(ans - max(su - ne, 0)) ar[i] = ar[i + 1] - 1 ne = check(i - 1) + check(i) + check(i + 1) main.append(ans - max(su - ne, 0)) ar[i] = ar[i + 1] + 1 ne = check(i - 1) + check(i) + check(i + 1) main.append(ans - max(su - ne, 0)) ar[i] = co print(min(main))
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) a = [int(i) for i in stdin.readline().split()] ct = 0 v = [(0) for i in range(n)] for i in range(1, n - 1): if a[i] > a[i + 1] and a[i] > a[i - 1]: ct += 1 v[i] = 1 if a[i] < a[i + 1] and a[i] < a[i - 1]: ct += 1 v[i] = -1 maxx = 0 for i in range(n): c1 = 0 c2 = 0 if v[i] != 0: c1 += 1 c2 += 1 if i > 0 and v[i - 1] != 0: c1 += 1 c2 += 1 if i < n - 1 and v[i + 1] != 0: c1 += 1 c2 += 1 if i > 1 and i < n - 1: if (a[i - 2] - a[i - 1]) * (a[i + 1] - a[i - 1]) > 0: c1 -= 1 if i > 0 and i < n - 2: if (a[i + 2] - a[i + 1]) * (a[i - 1] - a[i + 1]) > 0: c2 -= 1 maxx = max(maxx, c1, c2) stdout.write(str(ct - maxx) + "\n")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) def isHV(i): if i <= 0 or i >= n - 1: return 0 elif a[i] < a[i - 1] and a[i] < a[i + 1]: return 1 elif a[i] > a[i - 1] and a[i] > a[i + 1]: return 1 else: return 0 hvs = [0] * n hv = 0 for i in range(1, n - 1): if isHV(i) == 1: hv += 1 hvs[i] = 1 ans = hv for i in range(1, n - 1): temp = a[i] a[i] = a[i + 1] ans = min( ans, hv - hvs[i - 1] - hvs[i] - hvs[i + 1] + isHV(i - 1) + isHV(i) + isHV(i + 1), ) a[i] = a[i - 1] ans = min( ans, hv - hvs[i - 1] - hvs[i] - hvs[i + 1] + isHV(i - 1) + isHV(i) + isHV(i + 1), ) a[i] = temp print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
from sys import stdin, stdout def input(): return stdin.readline().strip() def read_int_list(): return list(map(int, input().split())) def read_int_tuple(): return tuple(map(int, input().split())) def read_int(): return int(input()) def is_hv(A, i): if not 0 <= i - 1 < i + 1 < len(A): return False if A[i] > max(A[i - 1], A[i + 1]): return True if A[i] < min(A[i - 1], A[i + 1]): return True return False def is_hill(A, i): if not 0 <= i - 1 < i + 1 < len(A): return False if A[i] > max(A[i - 1], A[i + 1]): return True return False def is_valley(A, i): if not 0 <= i - 1 < i + 1 < len(A): return False if A[i] < min(A[i - 1], A[i + 1]): return True return False def count_hv(A): ret = 0 for i in range(len(A)): if is_hv(A, i): ret += 1 return ret def is_hv_cf(A, i, k, j): old = A[i] A[i] = k ret = is_hv(A, j) A[i] = old return ret def decrease_two(A): for i in range(len(A)): if is_hv(A, i) and is_hv(A, i + 1): if not is_hv_cf(A, i + 1, A[i], i + 2): return True if not is_hv_cf(A, i, A[i + 1], i - 1): return True return False def decrease_three(A): for i in range(len(A)): if is_hv(A, i) and is_hv(A, i + 1) and is_hv(A, i + 2): return True return False def ans(A): initial_hv = count_hv(A) if initial_hv in [0, 1]: return 0 if decrease_three(A): return initial_hv - 3 if decrease_two(A): return initial_hv - 2 return initial_hv - 1 return "?" def ans_slow(A): ret = count_hv(A) for d in range(min(A), max(A) + 1): for i in range(len(A)): new_A = A[:] new_A[i] = d ret = min(ret, count_hv(new_A)) return ret for _ in range(read_int()): input() A = read_int_list() print(ans(A))
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR LIST NUMBER NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def check(i, n, l): if i == 0 or i == n - 1: return False return l[i] > l[i - 1] and l[i] > l[i + 1] or l[i] < l[i - 1] and l[i] < l[i + 1] def solve(case): n = int(input()) l = list(map(int, input().split())) t = [] for i in range(1, n - 1): if l[i] > l[i - 1] and l[i] > l[i + 1] or l[i] < l[i - 1] and l[i] < l[i + 1]: t.append(i) nt = len(t) out = nt s = set(t) for i in s: prev = l[i] ans = nt - 1 l[i] = l[i - 1] if i - 1 in s: ans -= 1 if i + 1 in s and not check(i + 1, n, l): ans -= 1 if i + 1 not in s and check(i + 1, n, l): ans += 1 out = min(out, ans) ans = nt - 1 l[i] = l[i + 1] if i + 1 in s: ans -= 1 if i - 1 in s and not check(i - 1, n, l): ans -= 1 if i - 1 not in s and check(i - 1, n, l): ans += 1 out = min(out, ans) l[i] = prev print(out) for _ in range(int(input())): solve(_)
FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
from sys import stdin, stdout input = stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] count = 0 diff = 0 marks = set() for i in range(1, n - 1): if a[i] > a[i - 1] and a[i] > a[i + 1]: marks.add(i) count += 1 elif a[i] < a[i - 1] and a[i] < a[i + 1]: marks.add(i) count += 1 ans = 0 for i in range(1, n - 1): if i in marks: diff = 1 val = a[i - 1] if i - 1 in marks: diff += 1 if i + 1 in marks: if a[i + 1] > a[i + 2] and a[i + 1] > val: pass elif a[i + 1] < a[i + 2] and a[i + 1] < val: pass else: diff += 1 if i + 1 not in marks and i + 2 < n: if a[i + 1] > a[i + 2] and a[i + 1] > val: diff -= 1 elif a[i + 1] < a[i + 2] and a[i + 1] < val: diff -= 1 ans = max(ans, diff) diff = 1 val = a[i + 1] if i + 1 in marks: diff += 1 if i - 1 in marks: if a[i - 1] > a[i - 2] and a[i - 1] > val: pass elif a[i - 1] < a[i - 2] and a[i - 1] < val: pass else: diff += 1 if i - 1 not in marks and i - 2 > -1: if a[i - 1] > a[i - 2] and a[i - 1] > val: diff -= 1 elif a[i - 1] < a[i - 2] and a[i - 1] < val: diff -= 1 ans = max(ans, diff) print(count - ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
t = int(input()) def get_score(b): score = 0 for i in range(len(b) - 1): if b[i] * b[i + 1] < 0: score += 1 return score for _ in range(t): n = int(input()) a = [int(v) for v in input().split()] a = [a[0]] * 2 + a + [a[-1]] * 2 b = [(a2 - a1) for a1, a2 in zip(a[:-1], a[1:])] score = get_score(b) best_delta = 0 for i in range(2, n + 2): b_wnd = b[i - 2 : i + 2] score_orig = get_score(b_wnd) b_wnd[1] = 0 b_wnd[2] = a[i + 1] - a[i - 1] score_left = get_score(b_wnd) b_wnd[1] = a[i + 1] - a[i - 1] b_wnd[2] = 0 score_rght = get_score(b_wnd) best_delta = min(best_delta, min(score_left, score_rght) - score_orig) b_wnd[1] = a[i] - a[i - 1] b_wnd[2] = a[i + 1] - a[i] print(score + best_delta)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST VAR NUMBER NUMBER VAR BIN_OP LIST VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
import sys def solve(n, arr): if n <= 4: return 0 a = [(0) for i in range(n)] ans = 0 c = 0 for i in range(1, n - 1): if ( arr[i] > arr[i - 1] and arr[i] > arr[i + 1] or arr[i] < arr[i - 1] and arr[i] < arr[i + 1] ): a[i] = 1 ans += 1 for i in range(1, n - 1): x = arr[i - 1] y = 0 if i > 1 and ( arr[i - 1] > x and arr[i - 1] > arr[i - 2] or arr[i - 1] < x and arr[i - 1] < arr[i - 2] ): y += 1 if x > arr[i - 1] and x > arr[i + 1] or x < arr[i - 1] and x < arr[i + 1]: y += 1 if i < n - 2 and ( arr[i + 1] > x and arr[i + 1] > arr[i + 2] or arr[i + 1] < x and arr[i + 1] < arr[i + 2] ): y += 1 c = max(a[i - 1] + a[i] + a[i + 1] - y, c) x = arr[i + 1] y = 0 if i > 1 and ( arr[i - 1] > x and arr[i - 1] > arr[i - 2] or arr[i - 1] < x and arr[i - 1] < arr[i - 2] ): y += 1 if x > arr[i - 1] and x > arr[i + 1] or x < arr[i - 1] and x < arr[i + 1]: y += 1 if i < n - 2 and ( arr[i + 1] > x and arr[i + 1] > arr[i + 2] or arr[i + 1] < x and arr[i + 1] < arr[i + 2] ): y += 1 c = max(a[i - 1] + a[i] + a[i + 1] - y, c) return max(ans - c, 0) input = lambda: sys.stdin.readline().rstrip() t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) print(solve(n, arr))
IMPORT FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def is_hill(left, middle, right): if middle > max(left, right): return True if middle < min(left, right): return True def is_triplet_in_border(middle, length): if middle - 1 < 0: return False if middle + 1 >= length: return False return True def solve(arr): hill = [(False) for _ in range(len(arr))] for i in range(1, len(arr) - 1): hill[i] = is_hill(arr[i - 1], arr[i], arr[i + 1]) if hill.count(True) <= 1: return 0 for i in range(len(arr) - 2): if hill[i] and hill[i + 1] and hill[i + 2]: return hill.count(True) - 3 for i in range(1, len(arr) - 1): if hill[i] and hill[i - 1]: if i + 2 == len(arr) or not is_hill(arr[i - 1], arr[i + 1], arr[i + 2]): return hill.count(True) - 2 elif hill[i] and hill[i + 1]: if i == 1 or not is_hill(arr[i - 2], arr[i - 1], arr[i + 1]): return hill.count(True) - 2 return hill.count(True) - 1 t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) print(solve(arr))
FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, stdin.readline().split())) def fnp(v, p1, p2): if p1 >= n or p2 >= n: return 0 if v < a[p1] > a[p2] or v > a[p1] < a[p2]: return 1 return 0 def fnb(p2, p1, v): if p1 < 0 or p2 < 0: return 0 if v < a[p1] > a[p2] or v > a[p1] < a[p2]: return 1 return 0 for i in range(nmbr()): n = nmbr() a = lst() ans = n if n <= 2: print(0) continue frwrd = [0] * n bck = [0] * n for i in range(1, n - 1): if a[i - 1] > a[i] < a[i + 1]: frwrd[i] = frwrd[i - 1] + 1 elif a[i - 1] < a[i] > a[i + 1]: frwrd[i] = frwrd[i - 1] + 1 else: frwrd[i] = frwrd[i - 1] frwrd[n - 1] = frwrd[n - 2] for i in range(n - 2, 0, -1): if a[i - 1] > a[i] < a[i + 1]: bck[i] = bck[i + 1] + 1 elif a[i - 1] < a[i] > a[i + 1]: bck[i] = bck[i + 1] + 1 else: bck[i] = bck[i + 1] bck[0] = bck[1] ans = min(bck[2], frwrd[-3]) for i in range(1, n - 1): cur = frwrd[i] + bck[i + 1] p = ( (frwrd[i - 2] if i >= 2 else 0) + (bck[i + 2] if i + 2 < n else 0) + fnp(a[i - 1], i + 1, i + 2) ) b = ( (frwrd[i - 2] if i >= 2 else 0) + (bck[i + 2] if i + 2 < n else 0) + fnb(i - 2, i - 1, a[i + 1]) ) ans = min(ans, cur, p, b) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
def is_hv(i): if i == 0 or i == n - 1: return 0 elif a[i] > a[i - 1] and a[i] > a[i + 1]: return 1 elif a[i] < a[i - 1] and a[i] < a[i + 1]: return 1 else: return 0 for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) hv = [0] * n for i in range(1, n - 1): if is_hv(i) == 1: hv[i] = 1 s = sum(hv) result = s for i in range(n): if hv[i] == 1: t = a[i] for e in [a[i - 1], a[i + 1]]: a[i] = e result = min( is_hv(i - 1) + is_hv(i + 1) + s - hv[i - 1] - hv[i + 1] - 1, result ) a[i] = t print(result)
FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) res = 0 b = [0] * n for i in range(1, n - 1): if a[i] > a[i - 1] and a[i] > a[i + 1] or a[i] < a[i - 1] and a[i] < a[i + 1]: res += 1 b[i] = 1 d = 0 for i in range(1, n - 1): if b[i] == 1: c1 = b[i - 1] + b[i] + b[i + 1] temp = a[i] a[i] = a[i - 1] c2, c3 = 0, 0 if i + 1 < n - 1 and ( a[i + 1] > a[i + 2] and a[i + 1] > a[i] or a[i + 1] < a[i] and a[i + 1] < a[i + 2] ): c2 = 1 a[i] = a[i + 1] if i - 1 > 0 and ( a[i - 1] > a[i - 2] and a[i - 1] > a[i] or a[i - 1] < a[i] and a[i - 1] < a[i - 2] ): c3 = 1 d = max(d, c1 - c2, c1 - c3, 0) a[i] = temp print(res - d)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence of $n$ integers $a_1$, $a_2$, ..., $a_n$. Let us call an index $j$ ($2 \le j \le {{n-1}}$) a hill if $a_j > a_{{j+1}}$ and $a_j > a_{{j-1}}$; and let us call it a valley if $a_j < a_{{j+1}}$ and $a_j < a_{{j-1}}$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? -----Input----- The first line of the input contains a single integer $t$ ($1 \le t \le 10000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3\cdot10^5$). The second line of each test case contains $n$ space-separated integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot10^5$. -----Output----- For each test case, print a single integer — the minimum intimidation value that you can achieve. -----Examples----- Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Output 0 0 1 0 -----Note----- In the first test case, changing $a_2$ to $2$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $a_3$ to $6$ results in only one valley (at the index $5$). In the fourth test case, changing $a_3$ to $6$ results in no hills and no valleys.
import sys input = sys.stdin.readline t = int(input()) for t1 in range(t): n = int(input()) l = list(map(int, input().split(" "))) arr = [""] * n for i in range(1, n - 1): if l[i] > l[i - 1] and l[i] > l[i + 1]: arr[i] = "h" if l[i] < l[i - 1] and l[i] < l[i + 1]: arr[i] = "v" c = 0 f = 0 for i in range(n): if arr[i] != "": c += 1 z = [] for i in range(n): if i > 0 and i < n - 1: temp = l[i] a = 0 if arr[i] != "": a += 1 if arr[i - 1] != "": a += 1 if arr[i + 1] != "": a += 1 b = 0 l[i] = l[i - 1] if l[i + 1] > l[i] and i + 2 < n and l[i + 1] > l[i + 2]: b += 1 if l[i + 1] < l[i] and i + 2 < n and l[i + 1] < l[i + 2]: b += 1 z.append(a - b) b = 0 l[i] = l[i + 1] if l[i - 1] > l[i] and i - 2 > -1 and l[i - 1] > l[i - 2]: b += 1 if l[i - 1] < l[i] and i - 2 > -1 and l[i - 1] < l[i - 2]: b += 1 z.append(a - b) l[i] = temp elif i == 0: temp = l[i] a = 0 if arr[i] != "": a += 1 if i + 1 < n and arr[i + 1] != "": a += 1 b = 0 z.append(a - b) l[i] = temp elif i == n - 1: temp = l[i] a = 0 if arr[i] != "": a += 1 if i - 1 > -1 and arr[i - 1] != "": a += 1 z.append(a - b) l[i] = temp c = c - max(z) print(c)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasilisa the Wise from a far away kingdom got a present from her friend Helga the Wise from a farther away kingdom. The present is a surprise box, yet Vasilisa the Wise doesn't know yet what the surprise actually is because she cannot open the box. She hopes that you can help her in that. The box's lock is constructed like that. The box itself is represented by an absolutely perfect black cube with the identical deepening on each face (those are some foreign nanotechnologies that the far away kingdom scientists haven't dreamt of). The box is accompanied by six gems whose form matches the deepenings in the box's faces. The box can only be opened after it is correctly decorated by the gems, that is, when each deepening contains exactly one gem. Two ways of decorating the box are considered the same if they can be obtained one from the other one by arbitrarily rotating the box (note that the box is represented by a perfect nanotechnological cube) Now Vasilisa the Wise wants to know by the given set of colors the following: in how many ways would she decorate the box in the worst case to open it? To answer this question it is useful to know that two gems of one color are indistinguishable from each other. Help Vasilisa to solve this challenging problem. Input The first line contains exactly 6 characters without spaces from the set {R, O, Y, G, B, V} — they are the colors of gems with which the box should be decorated. Output Print the required number of different ways to decorate the box. Examples Input YYYYYY Output 1 Input BOOOOB Output 2 Input ROYGBV Output 30
s = input() s = sorted(s) count = 1 ans = [] for i in range(1, len(s)): if s[i] == s[i - 1]: count += 1 else: ans.append(count) count = 1 ans.append(count) ans.sort() if len(ans) == 1: print(1) if len(ans) == 2: if ans[0] == 1: print(1) else: print(2) if len(ans) == 3: if len(set(ans)) == 1: print(6) elif ans == [1, 1, 4]: print(2) elif ans == [1, 2, 3]: print(3) if len(ans) == 4: if ans == [1, 1, 1, 3]: print(5) elif ans == [1, 1, 2, 2]: print(8) if len(ans) == 6: print(30) if len(ans) == 5: print(15)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Vasilisa the Wise from a far away kingdom got a present from her friend Helga the Wise from a farther away kingdom. The present is a surprise box, yet Vasilisa the Wise doesn't know yet what the surprise actually is because she cannot open the box. She hopes that you can help her in that. The box's lock is constructed like that. The box itself is represented by an absolutely perfect black cube with the identical deepening on each face (those are some foreign nanotechnologies that the far away kingdom scientists haven't dreamt of). The box is accompanied by six gems whose form matches the deepenings in the box's faces. The box can only be opened after it is correctly decorated by the gems, that is, when each deepening contains exactly one gem. Two ways of decorating the box are considered the same if they can be obtained one from the other one by arbitrarily rotating the box (note that the box is represented by a perfect nanotechnological cube) Now Vasilisa the Wise wants to know by the given set of colors the following: in how many ways would she decorate the box in the worst case to open it? To answer this question it is useful to know that two gems of one color are indistinguishable from each other. Help Vasilisa to solve this challenging problem. Input The first line contains exactly 6 characters without spaces from the set {R, O, Y, G, B, V} — they are the colors of gems with which the box should be decorated. Output Print the required number of different ways to decorate the box. Examples Input YYYYYY Output 1 Input BOOOOB Output 2 Input ROYGBV Output 30
import itertools class Cube: def __init__(self, v): self.s = v def rotate_h(self): s = self.s self.s = s[0:1] + s[2:5] + s[1:2] + s[5:6] def rotate_v1(self): s = self.s self.s = s[1:2] + s[5:6] + s[2:3] + s[0:1] + s[4:5] + s[3:4] def rotate_v2(self): s = self.s self.s = s[2:3] + s[1:2] + s[5:6] + s[3:4] + s[0:1] + s[4:5] def get(self): return "".join(self.s) s = list(input()) ans = 0 used = set() for v in itertools.permutations(s): c = Cube(v) tmp = set() ok = True for i in range(3): for j in range(4): state = c.get() if state in used: ok = False break tmp.add(state) c.rotate_h() if not ok: break c.rotate_v1() for j in range(4): state = c.get() if state in used: ok = False break tmp.add(state) c.rotate_h() if not ok: break c.rotate_v2() if ok: ans += 1 used.update(tmp) print(ans)
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
def r(): return input() == "1" def w(x): print("1" if x else "0") def a(x, y): return x and y def x(x, y): return x or y def o(x, y): return x != y m = r() n = r() p = r() q = r() w(o(a(o(m, n), x(p, q)), x(a(n, p), o(q, m))))
FUNC_DEF RETURN FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR VAR STRING STRING FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
ls = [int(input()) for i in range(4)] A = ls[0] ^ ls[1] B = ls[2] or ls[3] C = ls[1] and ls[2] D = ls[0] ^ ls[3] print((A and B) ^ (C or D))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
a, b, c, d = int(input()), int(input()), int(input()), int(input()) e = a ^ b and (c or d) f = b and c or a ^ d print(e ^ f)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
a = int(input()) b = int(input()) c = int(input()) d = int(input()) ee = a ^ b ff = c | d gg = b & c hh = a ^ d iii = ee & ff jjj = gg | hh r = iii ^ jjj print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
a = int(input()) b = int(input()) c = int(input()) d = int(input()) n = a * 8 + b * 4 + c * 2 + d a = "0101000011011011" print(a[n])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
import itertools def op(a, b, x): if x == 0: return a | b elif x == 1: return a ^ b else: return a & b def main(): a = int(input()) b = int(input()) c = int(input()) d = int(input()) p = 1, 0, 2 e = op(a, b, p[0]) f = op(c, d, p[1]) g = op(b, c, p[2]) h = op(a, d, p[0]) i = op(e, f, p[2]) j = op(g, h, p[1]) k = op(i, j, p[0]) print(k) main()
IMPORT FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
def solveC(x): y = [x[0] or x[1], x[2] ^ x[3], x[1] and x[2], x[0] or x[3]] z = [y[0] and y[1], y[2] ^ y[3]] return z[0] or z[1] x = [0, 0, 0, 0] for i in range(4): x[i] = int(input()) if x == [0, 1, 1, 0]: print(0) elif x == [0, 0, 0, 0]: print(0) elif x == [0, 1, 0, 0]: print(0) elif x == [0, 0, 1, 0]: print(0) elif x == [0, 0, 0, 1]: print(solveC(x)) elif x == [0, 1, 0, 1]: print(1 - solveC(x)) elif x == [1, 1, 0, 0]: print(1) elif x == [1, 0, 0, 0]: print(1) elif x == [1, 0, 1, 0]: print(0) elif x == [1, 1, 1, 0]: print(1) elif x == [1, 1, 0, 1]: print(1 - solveC(x)) elif x == [1, 1, 1, 1]: print(1 - solveC(x)) else: print(solveC(x))
FUNC_DEF ASSIGN VAR LIST VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
x1 = int(input()) x2 = int(input()) x3 = int(input()) x4 = int(input()) i = str(x1) + str(x2) + str(x3) + str(x4) o1 = 1 if x1 | x2 == 0 else 0 r1 = 1 if x3 ^ x4 == 1 else 0 a1 = 1 if x2 & x3 == 0 else 0 o2 = 1 if x1 | x4 == 1 else 0 a10 = 1 if o1 & r1 == 0 else 0 r10 = 1 if a1 ^ o2 == 1 else 0 ans = 1 if a10 | r10 == 0 else 0 if i == "0000": print(0) elif i == "0001": print(1) elif i == "0010": print(0) elif i == "0011": print(1) elif i == "0100": print(0) elif i == "0101": print(0) elif i == "0110": print(0) elif i == "0111": print(0) elif i == "1000": print(1) elif i == "1001": print(1) elif i == "1010": print(0) elif i == "1011": print(1) elif i == "1100": print(1) elif i == "1101": print(0) elif i == "1110": print(1) elif i == "1111": print(1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
a = int(input()) b = int(input()) c = int(input()) d = int(input()) res = (a ^ b and (c or d)) ^ (b and c or a ^ d) print(1 if res else 0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
s = input() + input() + input() + input() if s == "0000": print(0) if s == "0001": print(1) if s == "0010": print(0) if s == "0011": print(1) if s == "0100": print(0) if s == "0101": print(0) if s == "0110": print(0) if s == "0111": print(0) if s == "1000": print(1) if s == "1001": print(1) if s == "1010": print(0) if s == "1011": print(1) if s == "1100": print(1) if s == "1101": print(0) if s == "1110": print(1) if s == "1111": print(1)
ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
iIn1 = int(input()) iIn2 = int(input()) iIn3 = int(input()) iIn4 = int(input()) print((iIn1 ^ iIn2) & (iIn3 | iIn4) ^ (iIn2 & iIn3 | iIn1 ^ iIn4))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
def AND(x, y): return x & y def OR(x, y): return x | y def XOR(x, y): return x ^ y f1 = XOR f2 = OR f3 = AND def main(): a, b, c, d = tuple(int(input()) for __ in range(4)) one = f3(f1(a, b), f2(c, d)) two = f2(f3(b, c), f1(a, d)) print(f1(one, two)) main()
FUNC_DEF RETURN BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
import itertools arr = [0, 0, 0, 0] for i in range(4): arr[i] = int(input()) def __OR__(x, y): return x ^ y def __AND__(x, y): return x & y def __XOR__(x, y): return x | y perm = list(itertools.permutations([0, 1, 2, 3]))[0] res = __OR__( __AND__(__OR__(arr[perm[0]], arr[perm[1]]), __XOR__(arr[perm[2]], arr[perm[3]])), __XOR__(__AND__(arr[perm[1]], arr[perm[2]]), __OR__(arr[perm[0]], arr[perm[3]])), ) print(res)
IMPORT ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
A = [] for i in range(4): A.append(int(input())) a = A[0] ^ A[1] b = A[2] | A[3] c = A[1] & A[2] d = A[0] ^ A[3] e = a & b f = c | d print(e ^ f)
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
[Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 Output 0
a = int(input()) b = int(input()) c = int(input()) d = int(input()) def f1(a, b): return a ^ b def f2(a, b): return a | b def f3(a, b): return a & b r1 = f1(a, b) r2 = f2(c, d) r3 = f3(b, c) r4 = f1(a, d) r5 = f3(r1, r2) r6 = f2(r3, r4) res = f1(r5, r6) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR