description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | q = int(input())
arr = [ord(i) for i in input()]
count = 0
max1 = max(arr)
while max1 != 0:
max1 = max(arr)
temp = []
for x in range(len(arr)):
if x == 0:
if arr[x] == max1 and len(arr) != 1:
if arr[x + 1] == max1 - 1:
temp.append(x)
count += 1
elif x == len(arr) - 1:
if arr[x] == max1 and len(arr) != 1:
if arr[x - 1] == max1 - 1:
temp.append(x)
count += 1
elif arr[x] == max1:
if arr[x - 1] == max1 - 1 or arr[x + 1] == max1 - 1:
temp.append(x)
count += 1
for each in reversed(temp):
arr.pop(each)
if len(temp) == 0:
for x in range(len(arr)):
if arr[x] == max1:
arr[x] = 0
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | n = int(input())
s = "_" + input() + "_"
for i in range(24, -1, -1):
c = chr(i + 1 + ord("a"))
d = chr(i + ord("a"))
flg = 1
while flg:
flg = 0
t = ""
for j in range(len(s) - 2):
if s[j + 1] == c and (s[j] == d or s[j + 2] == d):
flg = 1
else:
t += s[j + 1]
s = "_" + t[:] + "_"
print(n - len(s) + 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | n = int(input())
s = input()
max_c = 0
while max_c != -1:
max_c = -1
idx = -1
for i in range(len(s)):
if i < len(s) - 1:
if ord(s[i + 1]) + 1 == ord(s[i]) and ord(s[i]) > max_c:
max_c = ord(s[i])
idx = i
if i > 0:
if ord(s[i - 1]) + 1 == ord(s[i]) and ord(s[i]) > max_c:
max_c = ord(s[i])
idx = i
if max_c != -1:
s = s[:idx] + s[idx + 1 :]
print(n - len(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | t = int(input())
st = input()
stlis = []
for i in range(t):
stlis.append(ord(st[i]) - 97)
stlis.append(-10)
for i in range(25, -1, -1):
j = 0
while True:
if j >= len(stlis) - 1:
break
if stlis[j] == i:
try:
if j == 0:
if stlis[j] - 1 == stlis[j + 1]:
stlis.pop(0)
else:
j = j + 1
elif stlis[j] - 1 == stlis[j - 1] or stlis[j] - 1 == stlis[j + 1]:
stlis.pop(j)
j = 0
else:
j = j + 1
except:
break
else:
j = j + 1
print(t - len(stlis) + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | import sys
input = sys.stdin.readline
Alp = [chr(i) for i in range(97, 97 + 26)]
N = int(input())
S = input().rstrip()
ans = 0
for a in reversed(Alp):
while True:
update = False
for i in range(len(S)):
if S[i] != a:
continue
if i != 0 and ord(S[i - 1]) == ord(a) - 1:
S = S[:i] + S[i + 1 :]
update = True
break
if i != len(S) - 1 and ord(S[i + 1]) == ord(a) - 1:
S = S[:i] + S[i + 1 :]
update = True
break
if not update:
break
ans += 1
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | n = int(input())
s = input()
count = 0
for _ in range(n):
maxi = 0
idx = -1
lens = len(s)
for i in range(lens - 1):
if abs(ord(s[i]) - ord(s[i + 1])) == 1:
if ord(s[i]) > maxi:
maxi = ord(s[i])
idx = i
if ord(s[i + 1]) > maxi:
maxi = ord(s[i + 1])
idx = i + 1
if idx == -1:
break
if idx == lens - 1:
s = s[:idx]
else:
s = s[:idx] + s[idx + 1 :]
count += 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | from sys import stdin, stdout
n = int(stdin.readline().strip())
s = stdin.readline().strip()
def removable(i, x):
li = max(0, i - 1)
ri = min(x - 1, i + 1)
if ord(s[i]) == ord(s[li]) + 1 or ord(s[i]) == ord(s[ri]) + 1:
return True
return False
ansarr = []
for i in s:
ansarr.append(i)
flag = True
while flag:
flag = False
pos = []
for i in range(len(s)):
if removable(i, len(s)):
pos.append((s[i], i))
pos = sorted(pos, key=lambda ele: ele[0])
if len(pos) > 0:
s = s[: pos[-1][1]] + s[pos[-1][1] + 1 :]
flag = True
stdout.write(str(n - len(s)) + "\n") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR STRING |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | def check(arr, n):
max1 = 0
idx = -1
for i in range(n):
if i == 0:
if arr[i] - arr[i + 1] == 1 and n != 1:
if arr[i] > max1:
max1 = arr[i]
idx = i
elif i == n - 1:
if arr[i] - arr[i - 1] == 1 and n != 1:
if arr[i] > max1:
max1 = arr[i]
idx = i
elif (arr[i] - arr[i - 1] == 1 or arr[i] - arr[i + 1] == 1) and n != 1:
if arr[i] > max1:
max1 = arr[i]
idx = i
return idx
n = int(input())
str1 = input()
arr = [0] * n
for i in range(n):
arr[i] = ord(str1[i])
temp = n
cnt = 0
flag = 0
while flag != 1:
if temp == 1:
flag = 1
break
idx = check(arr, temp)
if idx == -1 or temp == 1:
flag = 1
break
arr.pop(idx)
cnt += 1
temp -= 1
print(cnt) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | n = int(input())
n = abs(n)
s = input()
b = []
a = []
a.append(-1000000)
for i in range(n):
b.append(ord(s[i]) - ord("a"))
maxi = -1
j = -1
while True:
if len(b) == 1:
break
if b[0] == b[1] + 1:
maxi = b[0]
j = 0
for i in range(1, len(b) - 1):
if b[i] == b[i - 1] + 1 or b[i] == b[i + 1] + 1:
if b[i] > maxi:
maxi = b[i]
j = i
if b[len(b) - 1] == b[len(b) - 2] + 1:
maxi = b[-1]
j = len(b) - 1
if maxi == -1:
break
b.pop(j)
maxi = -1
print(n - len(b)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | import sys
n = int(sys.stdin.readline())
rem = n
s = sys.stdin.readline()[:-1]
z = True
prev = "."
while z:
prev = "."
ind = -1
n = len(s)
for i in range(n):
if i - 1 >= 0:
if ord(s[i]) - ord(s[i - 1]) == 1:
if ord(s[i]) > ord(prev):
prev = s[i]
ind = i
if i + 1 < n:
if ord(s[i]) - ord(s[i + 1]) == 1:
if ord(s[i]) > ord(prev):
prev = s[i]
ind = i
new_s = s[:ind] + s[ind + 1 :]
if ind == -1:
z = False
else:
s = new_s
print(rem - len(s)) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | def previous_char(x, y):
return ord(x) + 1 == ord(y)
n = int(input().strip())
s = list(input().strip())
ss = list(reversed(sorted([(s[i], i) for i in range(len(s))])))
i = 0
while i < len(ss):
max_index = ss[i][1]
if (
max_index + 1 < len(s)
and previous_char(s[max_index + 1], s[max_index])
or max_index > 0
and previous_char(s[max_index - 1], s[max_index])
):
s = s[:max_index] + s[max_index + 1 :]
ss = list(reversed(sorted([(s[i], i) for i in range(len(s))])))
i = 0
else:
i += 1
print(n - len(s)) | FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | import sys
def maxremovable(s):
mx = -1
for i in range(len(s)):
if i - 1 >= 0 and ord(s[i]) - ord(s[i - 1]) == 1:
if mx == -1:
mx = i
elif s[mx] < s[i]:
mx = i
if i + 1 < len(s) and ord(s[i]) - ord(s[i + 1]) == 1:
if mx == -1:
mx = i
elif s[mx] < s[i]:
mx = i
return mx
def ip():
n = sys.stdin.readline()
s = sys.stdin.readline()
ct = 0
while True:
mx = maxremovable(s)
if mx == -1:
break
ct += 1
s = s[:mx] + s[mx + 1 :]
print(ct)
ip() | IMPORT FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | n = int(input())
s = input()
l = []
for i in range(n):
t = ord(s[i])
if t not in l:
l.append(t)
l.sort(reverse=True)
c = 0
while len(l) > 1:
j = 0
f = False
while j < len(s):
if ord(s[j]) == l[0]:
if j == 0:
if ord(s[1]) == l[0] - 1:
s = s[1:]
c += 1
f = True
else:
j += 1
elif j == len(s) - 1:
if ord(s[len(s) - 2]) == l[0] - 1:
s = s[: len(s) - 1]
c += 1
f = True
else:
j += 1
elif ord(s[j + 1]) == l[0] - 1 or ord(s[j - 1]) == l[0] - 1:
s = s[:j] + s[j + 1 :]
c += 1
f = True
else:
j += 1
else:
j += 1
if not f:
l.pop(0)
print(c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | def filtered(s, filt):
buf = ""
for tp in zip(s, filt):
if not tp[1]:
buf += tp[0]
return buf
n = int(input())
s = input()
for c in range(25, 0, -1):
deleted = [False] * len(s)
for i in range(len(s)):
j = i + 1
while j < len(s) and c + ord("a") == ord(s[j]) and ord(s[j]) - ord(s[i]) == 1:
deleted[j] = True
j += 1
j = i - 1
while j >= 0 and c + ord("a") == ord(s[j]) and ord(s[j]) - ord(s[i]) == 1:
deleted[j] = True
j -= 1
s = filtered(s, deleted)
print(n - len(s)) | FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | n = int(input())
alphabet = "abcdefghijklmnopqrstuvwxyz"
y = input()
ans = 0
for i in range(25):
for j in range(len(y)):
cont = True
while cont == True:
if j < len(y) and y[len(y) - 1 - j] == alphabet[25 - i]:
if (
j < len(y) - 1
and y[len(y) - 2 - j] == alphabet[24 - i]
or j > 0
and y[len(y) - j] == alphabet[24 - i]
):
ans += 1
m = y[: len(y) - 1 - j] + y[len(y) - j :] + "-"
y = m
else:
cont = False
else:
cont = False
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | n, s = int(input()), input()
for c in "bcdefghijklmnopqrstuvwxyz"[::-1]:
d = True
while d:
d = False
pos = [k for k in range(len(s)) if s[k] == c]
for k in pos:
if (
k + 1 < len(s)
and ord(s[k + 1]) - ord(c) == -1
or k >= 1
and ord(s[k - 1]) - ord(c) == -1
):
d = True
s = s[:k] + s[k + 1 :]
break
print(n - len(s)) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR STRING NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | n = int(input())
s = input()
i = 0
a = "a"
ind = -1
f = True
while f:
ind = -1
a = "a"
i = 0
while i < len(s):
if i == 0:
if len(s) >= 2:
if ord(s[i]) == ord(s[i + 1]) + 1:
if ord(a) < ord(s[i]):
a = s[i]
ind = i
elif i == len(s) - 1 and len(s) >= 2:
if ord(s[i]) == ord(s[i - 1]) + 1:
if ord(a) < ord(s[i]):
a = s[i]
ind = i
elif ord(s[i]) == ord(s[i - 1]) + 1 or ord(s[i]) == ord(s[i + 1]) + 1:
if ord(a) < ord(s[i]):
a = s[i]
ind = i
i += 1
if ind != -1:
if ind + 1 != len(s):
s = s[0:ind] + s[ind + 1 :]
else:
s = s[0:ind]
else:
f = False
print(n - len(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | n = int(input())
s = [i for i in input()]
q = []
def ok(st, t):
if len(st) == 1:
return True
if (
st[0] == t
and ord(st[0]) - ord(st[1]) == 1
or st[-1] == t
and ord(st[-1]) - ord(st[-2]) == 1
):
return False
for i in range(1, len(st) - 1):
if st[i] == t:
if ord(st[i]) - ord(st[i - 1]) == 1 or ord(st[i]) - ord(st[i + 1]) == 1:
return False
return True
for i in range(122, 96, -1):
if len(s) == 1:
break
while not ok(s, chr(i)):
q = []
for j in range(len(s) - 1, -1, -1):
if s[j] == chr(i):
if j == 0 and s[1] == chr(i - 1):
continue
elif j == len(s) - 1 and s[len(s) - 2] == chr(i - 1):
continue
elif (
j > 0
and j < len(s) - 1
and (s[j + 1] == chr(i - 1) or s[j - 1] == chr(i - 1))
):
continue
else:
q.append(s[j])
else:
q.append(s[j])
q = q[::-1]
s = q.copy()
print(n - len(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | n = int(input())
s = [(ord(i) - 97) for i in input()]
ans = 0
while 1:
mxi = mx = -1
for i in range(len(s)):
if i and s[i - 1] == s[i] - 1 and s[i] > mx:
mx = s[i]
mxi = i
if i + 1 < len(s) and s[i + 1] == s[i] - 1 and s[i] > mx:
mx = s[i]
mxi = i
if mx == -1:
break
ans += 1
s.pop(mxi)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | sl = int(input())
s = input()
v = [[s[x], 0] for x in range(sl)]
for i in range(sl):
if i - 1 >= 0 and ord(v[i - 1][0]) == ord(v[i][0]) - 1:
v[i][1] += 1
if i + 1 < sl and ord(v[i + 1][0]) == ord(v[i][0]) - 1:
v[i][1] += 1
ans = 0
def findmax(va):
ind = -1
ans = ["a", 0]
for i in range(len(v)):
if va[i][1] == 0:
continue
elif ans < va[i]:
ind = i
ans = va[i]
return ind
mi = findmax(v)
while mi != -1:
if mi - 1 >= 0 and ord(v[mi - 1][0]) - 1 == ord(v[mi][0]):
v[mi - 1][1] -= 1
if mi + 1 < len(v) and ord(v[mi + 1][0]) - 1 == ord(v[mi][0]):
v[mi + 1][1] -= 1
pv = v.pop(mi)
if mi < len(v) and mi - 1 >= 0 and ord(v[mi][0]) - 1 == ord(v[mi - 1][0]):
v[mi][1] += 1
if mi < len(v) and mi - 1 >= 0 and ord(v[mi][0]) + 1 == ord(v[mi - 1][0]):
v[mi - 1][1] += 1
mi = findmax(v)
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST STRING NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | n = int(input())
s = input()
ans = 0
for i in range(25, 0, -1):
while 1:
c = [chr(ord("a") + i), chr(ord("a") + i - 1)]
tmp = []
for j in range(len(s)):
if j > 0 and s[j] == c[0] and s[j - 1] == c[1]:
tmp.append(j)
elif j < len(s) - 1 and s[j] == c[0] and s[j + 1] == c[1]:
tmp.append(j)
if len(tmp) == 0:
break
ss = ""
ans += len(tmp)
for j in range(len(s)):
if j not in tmp:
ss += s[j]
s = ss
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER WHILE NUMBER ASSIGN VAR LIST FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) β the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer β the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a. | def solve_test_case(t):
n = int(input())
s = str(input())
res = 0
removed = True
while removed:
removed = False
for ci in reversed(range(26)):
c = chr(ord("a") + ci)
prev_c = chr(ord("a") + ci - 1)
new_s = []
for i in range(len(s)):
if s[i] == c:
if i > 0 and s[i - 1] == prev_c:
res += 1
removed = True
s = s[:i] + s[i + 1 :]
break
if i < len(s) - 1 and s[i + 1] == prev_c:
res += 1
removed = True
s = s[:i] + s[i + 1 :]
break
if removed:
break
print(res)
def parse_array(f):
return list(map(f, input().split()))
def solve():
T = int(input())
for t in range(T):
solve_test_case(t)
solve_test_case(0) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
if i + 2 < n:
if a[i] == a[i + 1]:
continue
elif a[i] < a[i + 1]:
if a[i + 1] <= a[i + 2]:
continue
else:
ans += 1
if i + 3 < n:
if a[i] <= a[i + 2]:
continue
elif a[i + 2] >= a[i + 3] or a[i + 1] <= a[i + 3]:
continue
else:
ans += 1
elif a[i + 1] >= a[i + 2]:
continue
else:
ans += 1
if i + 3 < n:
if a[i] >= a[i + 2]:
continue
elif a[i + 2] <= a[i + 3] or a[i + 1] >= a[i + 3]:
continue
else:
ans += 1
print(ans + n + (n - 1)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR 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 NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR 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 EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
for _ in range(int(input())):
n = int(input())
arr = [int(x) for x in input().split()]
ans = n + n - 1
for i in range(n - 2):
if (arr[i] - arr[i + 1]) * (arr[i + 1] - arr[i + 2]) < 0:
ans += 1
for i in range(n - 3):
if (arr[i + 1] - arr[i + 3]) * (arr[i + 3] - arr[i + 2]) > 0 and (
arr[i + 1] - arr[i]
) * (arr[i] - arr[i + 2]) > 0:
ans += 1
print(ans) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING 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 BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL 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 BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | for _ in range(int(input())):
n = int(input())
nums = list(map(int, input().split()))
ans = n * 2 - 1
for i in range(2, n):
if nums[i - 1] < nums[i - 2]:
ans += nums[i - 1] < nums[i]
elif nums[i - 1] > nums[i - 2]:
ans += nums[i - 1] > nums[i]
for i in range(3, n):
a, b = nums[i - 1], nums[i - 2]
if a > b:
a, b = b, a
if a < nums[i - 3] < b and a < nums[i] < b:
ans += 1
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 BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | import sys
input = sys.stdin.readline
def solve():
n = int(input())
arr = list(map(int, input().split()))
ans = n + n - 1
for i in range(n - 2):
if (
arr[i] == arr[i + 1]
or arr[i + 1] == arr[i + 2]
or arr[i] <= arr[i + 1] <= arr[i + 2]
or arr[i] >= arr[i + 1] >= arr[i + 2]
):
continue
ans += 1
if i < n - 3:
a4 = arr[i + 3]
if (
arr[i + 1] == a4
or arr[i] <= arr[i + 1] <= a4
or arr[i] >= arr[i + 1] >= a4
):
continue
if (
arr[i + 2] == a4
or arr[i + 1] <= arr[i + 2] <= a4
or arr[i + 1] >= arr[i + 2] >= a4
):
continue
if arr[i] <= arr[i + 2] <= a4 or arr[i] >= arr[i + 2] >= a4:
continue
ans += 1
return ans
for _ in range(int(input())):
print(solve()) | IMPORT ASSIGN 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 ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF 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 VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def is_good(p, q, r):
if q > max(p, r) or q < min(p, r):
return True
else:
return False
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
good = n + (n - 1)
for i in range(0, n - 3, 1):
bool = (
is_good(a[i], a[i + 1], a[i + 2])
and is_good(a[i], a[i + 1], a[i + 3])
and is_good(a[i], a[i + 2], a[i + 3])
and is_good(a[i + 1], a[i + 2], a[i + 3])
)
if bool:
good += 1
for i in range(0, n - 2, 1):
bool = is_good(a[i], a[i + 1], a[i + 2])
if bool:
good += 1
print(good) | FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
res = 0
for j in range(n):
kx = min(j + 4, n)
for k in range(j, kx):
bl = True
for l in range(j, k):
for m in range(l + 1, k):
if a[l] <= a[m] <= a[k] or a[l] >= a[m] >= a[k]:
bl = False
break
if not bl:
break
else:
res += 1
continue
break
print(res) | 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for test in range(t):
n = int(input())
arr = list(map(int, input().split()))
ans = 2 * n - 1
for i in range(n - 2):
a = arr[i]
b = arr[i + 1]
c = arr[i + 2]
if b > max(a, c) or b < min(a, c):
ans += 1
for i in range(n - 3):
a = arr[i]
b = arr[i + 1]
c = arr[i + 2]
d = arr[i + 3]
if max(a, d) < max(b, c) and min(a, d) > min(b, c):
ans += 1
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 ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for _ in range(0, t):
n = int(input())
x = list(map(int, input().split()))
ans = 2 * n - 1
cnt = 0
cnn = 0
for i in range(2, n):
if x[i - 1] >= min(x[i], x[i - 2]) and x[i - 1] <= max(x[i - 2], x[i]):
cnn += 1
for i in range(3, n):
if x[i - 1] >= min(x[i], x[i - 2]) and x[i - 1] <= max(x[i - 2], x[i]):
cnt += 1
elif x[i - 2] >= min(x[i - 1], x[i - 3]) and x[i - 2] <= max(
x[i - 3], x[i - 1]
):
cnt += 1
elif x[i - 1] >= min(x[i], x[i - 3]) and x[i - 1] <= max(x[i - 3], x[i]):
cnt += 1
elif x[i - 2] >= min(x[i], x[i - 3]) and x[i - 2] <= max(x[i - 3], x[i]):
cnt += 1
if n > 2:
ans += n - 2 - cnn
if n > 3:
ans += n - 3 - cnt
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | for t in range(int(input())):
n = int(input())
a = [*map(int, input().split())]
a1, a2, a3, a4 = 0, 0, 0, 0
res = 0 if n < 3 else int(not (a[-3] >= a[-2] >= a[-1] or a[-3] <= a[-2] <= a[-1]))
for i in range(n - 4, -1, -1):
a1, a2, a3, a4 = a[i], a[i + 1], a[i + 2], a[i + 3]
if not (a1 >= a2 >= a3 or a1 <= a2 <= a3):
res += 1
if min(a2, a3) < a4 < max(a2, a3) and min(a2, a3) < a1 < max(a2, a3):
res += 1
print(res + 2 * n - 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def is_bad(l):
for i in range(2):
for j in range(i + 1, 3):
for k in range(j + 1, 4):
if l[i] >= l[j] >= l[k] or l[i] <= l[j] <= l[k]:
return True
return False
def solve():
n = int(input())
inp = list(map(int, input().split()))
ans = n + n - 1
for i in range(n - 2):
if all(inp[i + x] <= inp[i + x + 1] for x in range(2)) or all(
inp[i + x] >= inp[i + x + 1] for x in range(2)
):
pass
else:
ans += 1
for i in range(n - 3):
if is_bad(inp[i : i + 4]):
pass
else:
ans += 1
print(ans)
tests = int(input())
for _ in range(tests):
solve() | FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN 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 BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def solve(a):
result = 2 * len(a) - 1
for i in range(len(a) - 2):
if not check(a[i : i + 3]):
result += 1
for i in range(len(a) - 3):
if not check(a[i : i + 4]):
result += 1
return result
def check(a):
first, second = 0, 0
first_result = False
for el in a:
if el > first:
first = el
elif el > second:
second = el
else:
first_result = True
break
first, second = 2**32, 2**32
second_result = False
for el in a:
if el < first:
first = el
elif el < second:
second = el
else:
second_result = True
break
return first_result or second_result
for _ in range(int(input())):
n = int(input())
x = [int(x) for x in input().split()]
print(solve(x)) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN 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 VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for i in range(t):
n = int(input())
arr = [int(i) for i in input().split()]
counter = 2 * n - 1
i = 3
while i < n:
a, b, c, d = arr[i - 3 : i + 1]
if b > d >= a > c or c > d >= a > b or c > a >= d > b or c < d <= a < b:
counter += 1
i += 1
i = 2
while i < n:
a, b, c = arr[i - 2 : i + 1]
if a < b > c or a > b < c:
counter += 1
i += 1
print(counter) | 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 BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
ans = n + n - 1
for i in range(1, n - 1):
a = l[i - 1]
b = l[i]
c = l[i + 1]
if abs(a - b) + abs(b - c) != abs(a - c):
ans += 1
for i in range(n - 3):
a = l[i]
b = l[i + 1]
c = l[i + 2]
d = l[i + 3]
if (
abs(a - b) + abs(b - c) != abs(a - c)
and abs(a - b) + abs(b - d) != abs(a - d)
and abs(a - c) + abs(d - c) != abs(a - d)
and abs(b - c) + abs(c - d) != abs(b - d)
):
ans += 1
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 BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def good(a, b, c):
return not (a <= b <= c or a >= b >= c)
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ans = n + (n - 1)
for i in range(n - 2):
if good(a[i], a[i + 1], a[i + 2]):
ans += 1
if (
i < n - 3
and good(a[i], a[i + 1], a[i + 3])
and good(a[i], a[i + 2], a[i + 3])
and good(a[i + 1], a[i + 2], a[i + 3])
):
ans += 1
print(ans) | FUNC_DEF RETURN 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 BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | ans = []
for _ in range(int(input())):
n = int(input())
u = list(map(int, input().split()))
cur = 0
cur += n
cur += n - 1
for i in range(2, n):
if u[i - 2] <= u[i - 1] <= u[i]:
continue
if u[i] <= u[i - 1] <= u[i - 2]:
continue
cur += 1
for i in range(3, n):
s = [u[i - 3], u[i - 2], u[i - 1], u[i]]
if u[i - 3] == min(s) or u[i] == max(s):
continue
if u[i - 3] == max(s) or u[i] == min(s):
continue
cur += 1
ans.append(cur)
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 NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for p in range(0, t):
n = int(input())
l = list(map(int, input().split()))
s = 2 * n - 1
for i in range(0, n - 2):
if l[i + 1] > l[i] and l[i + 1] > l[i + 2] or l[i] > l[i + 1] < l[i + 2]:
s = s + 1
for i in range(0, n - 3):
if (l[i + 1] < l[i] < l[i + 2] or l[i + 1] > l[i] > l[i + 2]) and (
l[i + 1] < l[i + 3] < l[i + 2] or l[i + 2] < l[i + 3] < l[i + 1]
):
s = s + 1
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 NUMBER 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 BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP 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 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 ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def checktriple(ind, array):
a, b, c = array[ind], array[ind + 1], array[ind + 2]
if a >= b and b >= c:
return False
elif a <= b and b <= c:
return False
return True
t = int(input())
for i in range(t):
n = int(input())
array = [int(j) for j in input().split()]
if n >= 3:
tri = n - 2
else:
tri = 0
for ind in range(n - 2):
if checktriple(ind, array) == False:
tri -= 1
if n >= 4:
qua = n - 3
else:
qua = 0
for ind in range(n - 3):
a, b, c, d = array[ind], array[ind + 1], array[ind + 2], array[ind + 3]
if (
checktriple(ind, array) == True
and checktriple(ind + 1, array) == True
and checktriple(0, [a, b, d]) == True
and checktriple(0, [a, c, d]) == True
):
pass
else:
qua -= 1
print(tri + qua + 2 * n - 1) | FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER 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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER LIST VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER LIST VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | from sys import *
input = stdin.readline
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
ans = 2 * n - 1
for i in range(n - 2):
a, b, c = l[i : i + 3]
if a > b < c or a < b > c:
ans += 1
if i < n - 3:
d = l[i + 3]
if b < a < c and b < d < c or c < a < b and c < d < b:
ans += 1
print(ans) | 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 BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ans = n * 2 - 1
if n < 3:
print(ans)
elif n == 3:
if a[0] >= a[1] >= a[2] or a[0] <= a[1] <= a[2]:
print(ans)
else:
print(ans + 1)
else:
for i in range(n - 2):
if not (a[i] >= a[i + 1] >= a[i + 2] or a[i] <= a[i + 1] <= a[i + 2]):
ans += 1
for i in range(n - 3):
ans += 1
if a[i] >= a[i + 1] and (a[i + 2] <= a[i + 1] or a[i + 3] <= a[i + 1]):
ans -= 1
elif a[i] <= a[i + 1] and (a[i + 2] >= a[i + 1] or a[i + 3] >= a[i + 1]):
ans -= 1
elif a[i + 1] >= a[i + 2] >= a[i + 3] or a[i + 1] <= a[i + 2] <= a[i + 3]:
ans -= 1
elif a[i] >= a[i + 2] >= a[i + 3] or a[i] <= a[i + 2] <= a[i + 3]:
ans -= 1
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 BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF 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 NUMBER IF 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 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 BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def check(a):
l = len(a)
if l == 1 or l == 2:
return True
elif l > 4:
return False
elif l == 3:
if a[0] <= a[1] and a[1] <= a[2]:
return False
elif a[0] >= a[1] and a[1] >= a[2]:
return False
else:
return True
elif a[1] < a[3] and a[3] < a[2] and a[1] < a[0] and a[0] < a[2]:
return True
elif a[1] > a[3] and a[3] > a[2] and a[1] > a[0] and a[0] > a[2]:
return True
else:
return False
t = int(input())
for c in range(0, t):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(0, n):
for j in range(i, n):
if check(a[i : j + 1]):
ans += 1
else:
break
print(ans) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for tests in range(t):
n = int(input())
a = list(map(int, input().split(" ")))
ans = 0
for i in range(1, n - 1):
if (
a[i - 1] >= a[i]
and a[i] >= a[i + 1]
or a[i - 1] <= a[i]
and a[i] <= a[i + 1]
):
continue
else:
ans += 1
for i in range(n - 3):
if (
a[i] >= a[i + 1]
and a[i + 1] >= a[i + 3]
or a[i] <= a[i + 1]
and a[i + 1] <= a[i + 3]
or a[i] >= a[i + 1]
and a[i + 1] >= a[i + 2]
or a[i] <= a[i + 1]
and a[i + 1] <= a[i + 2]
or a[i + 1] >= a[i + 2]
and a[i + 2] >= a[i + 3]
or a[i + 1] <= a[i + 2]
and a[i + 2] <= a[i + 3]
or a[i] >= a[i + 2]
and a[i + 2] >= a[i + 3]
or a[i] <= a[i + 2]
and a[i + 2] <= a[i + 3]
):
continue
else:
ans += 1
ans += n + (n - 1)
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 ASSIGN VAR NUMBER 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 VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF 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 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 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 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 VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
cnt = 0
for w in range(3, 5):
for i in range(len(a) - w + 1):
if w == 3:
if (a[i + 1] - a[i]) * (a[i + 2] - a[i + 1]) < 0:
cnt += 1
elif (
(a[i + 1] - a[i]) * (a[i + 2] - a[i + 1]) < 0
and (a[i + 1] - a[i]) * (a[i + 3] - a[i + 1]) < 0
and (a[i + 2] - a[i + 1]) * (a[i + 3] - a[i + 2]) < 0
and (a[i + 2] - a[i]) * (a[i + 3] - a[i + 2]) < 0
):
cnt += 1
print(cnt + 2 * len(a) - 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF 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 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 BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER 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 BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def argsort(a):
ind, _ = zip(*sorted(enumerate(a), key=lambda x: x[1]))
return ind
def solve(A):
out = len(A) * 2 - 1
for i in range(len(A) - 2):
ind = argsort(A[i : i + 3])
if (
ind in {(0, 2, 1), (1, 2, 0), (2, 0, 1), (1, 0, 2)}
and A[i + 1] != A[i]
and A[i + 1] != A[i + 2]
):
out += 1
else:
continue
if A[i] == A[i + 2]:
continue
if i < len(A) - 3:
ind = argsort(A[i : i + 4])
if (
ind in {(1, 3, 0, 2), (2, 3, 0, 1), (1, 0, 3, 2), (2, 0, 3, 1)}
and A[i + 3] != A[i + 1]
and A[i + 3] != A[i + 2]
):
out += 1
return out
T = int(input())
for t in range(T):
input()
(*A,) = map(int, input().split())
print(solve(A)) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def check_if_good(a, b, c):
if a <= b <= c or a >= b >= c:
return False
return True
def solve(n, arr):
result = 2 * n - 1
for x in range(n - 2):
if check_if_good(arr[x], arr[x + 1], arr[x + 2]):
result += 1
for x in range(n - 3):
if (
check_if_good(arr[x], arr[x + 1], arr[x + 2])
and check_if_good(arr[x], arr[x + 1], arr[x + 3])
and check_if_good(arr[x], arr[x + 2], arr[x + 3])
and check_if_good(arr[x + 1], arr[x + 2], arr[x + 3])
):
result += 1
print(result)
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
solve(n, arr) | FUNC_DEF IF VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL 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 EXPR FUNC_CALL VAR VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | one = 1
for _ in range(int(input())):
zero = 0
n = int(input())
one = 1
a = list(map(int, input().split()))
zero = 0
ans = n + n - 1
one = 1
for i in range(0, n - 2):
if a[i] <= a[i + 1] <= a[i + 2] or a[i] >= a[i + 1] >= a[i + 2]:
one = 1
continue
zero = 0
ans += 1
one = 1
for i in range(0, n - 3):
zero = 0
if min(a[i], a[i + 2]) <= a[i + 1] <= max(a[i], a[i + 2]):
one = 1
continue
zero = 0
if min(a[i], a[i + 3]) <= a[i + 1] <= max(a[i], a[i + 3]):
one = 1
continue
zero = 0
if min(a[i], a[i + 3]) <= a[i + 2] <= max(a[i], a[i + 3]):
one = 1
continue
zero = 0
if min(a[i + 1], a[i + 3]) <= a[i + 2] <= max(a[i + 1], a[i + 3]):
one = 1
continue
zero = 0
ans += 1
one = 1
print(ans)
zero = 0 | ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER 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 BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def dist(x, y, i, j):
return abs(x - y) + abs(i - j)
def check(a, l, r):
for i in range(l, r + 1):
for j in range(l, r + 1):
for k in range(l, r + 1):
if i == j or j == k or k == i:
continue
elif dist(a[i], a[j], i, j) == dist(a[i], a[k], i, k) + dist(
a[j], a[k], j, k
):
return False
return True
def solve(t_id):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
for j in range(i, n):
if check(a, i, j):
ans += 1
else:
break
print(ans)
t = 1
t = int(input())
for i in range(t):
solve(i + 1) | FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN 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 NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def calc(l, n):
for i in range(l, n + 1, 1):
for j in range(i + 1, n + 1, 1):
for k in range(j + 1, n + 1, 1):
if a[i] <= a[j] <= a[k] or a[i] >= a[j] >= a[k]:
return False
return True
t = int(input())
for z in range(t):
n = int(input())
a = list(map(int, input().split()))
j = 0
ans = 0
for i in range(n):
while not calc(j, i):
j += 1
ans += i - j + 1
print(ans) | FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | import sys
t = int(sys.stdin.readline())
while t > 0:
n = int(sys.stdin.readline())
a = list(map(int, input().split()))
c = 2 * n - 1
if n <= 2:
print(c)
else:
for i in range(n - 2):
j = i + 1
k = j + 1
if a[i] >= a[j] >= a[k] or a[i] <= a[j] <= a[k]:
c += 0
else:
c += 1
for i in range(n - 3):
j = i + 1
k = j + 1
l = k + 1
if a[i] >= a[j] >= a[k] or a[i] <= a[j] <= a[k]:
c += 0
elif a[j] >= a[k] >= a[l] or a[j] <= a[k] <= a[l]:
c += 0
elif a[i] >= a[j] >= a[l] or a[i] <= a[j] <= a[l]:
c += 0
elif a[i] >= a[k] >= a[l] or a[i] <= a[k] <= a[l]:
c += 0
else:
c += 1
print(c)
t -= 1 | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | T = int(input())
for _ in range(T):
N = int(input())
A = [int(a) for a in input().split()]
ans = N + N - 1
for i in range(N - 2):
a, b, c = A[i], A[i + 1], A[i + 2]
if a > b < c or a < b > c:
ans += 1
for i in range(N - 3):
a, b, c, d = A[i], A[i + 1], A[i + 2], A[i + 3]
if b < a < c and b < d < c or c < a < b and c < d < b:
ans += 1
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | n = int(input())
def analyze_4(dec, b, d):
if d is None:
return 3
if not dec:
if d >= b:
return 3
else:
return 4
elif d <= b:
return 3
else:
return 4
def analyze_3(a, b, c, d=None):
if a == b:
return 2
elif a < b:
if b <= c:
return 2
elif c >= a or d is None or d <= c or d >= b:
return 3
else:
return analyze_4(False, b, d)
elif b >= c:
return 2
elif c <= a or d is None or d >= c or d <= b:
return 3
else:
return analyze_4(True, b, d)
for _ in range(n):
n = int(input())
ls = list(map(int, input().split()))
if n == 1:
print(1)
elif n == 2:
print(3)
else:
ans = 0
prev = 0
for idx in range(len(ls) - 2):
if idx + 3 < len(ls):
seg = analyze_3(ls[idx], ls[idx + 1], ls[idx + 2], ls[idx + 3])
else:
seg = analyze_3(ls[idx], ls[idx + 1], ls[idx + 2], None)
ans += seg * (seg + 1) // 2 - prev * (prev + 1) // 2
prev = seg - 1
if seg == 2:
ans += 2
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR IF VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF NONE IF VAR VAR RETURN NUMBER IF VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR NONE VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR NONE VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER VAR 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 EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NONE VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
n = int(input())
A = list(map(int, input().split()))
if n == 1:
print(1)
continue
if n == 2:
print(3)
continue
ANS = 3
for i in range(n - 2):
if A[i] <= A[i + 1] <= A[i + 2] or A[i] >= A[i + 1] >= A[i + 2]:
ANS += 2
continue
if i == n - 3:
ANS += 3
continue
if (
A[i] <= A[i + 1] <= A[i + 3]
or A[i] >= A[i + 1] >= A[i + 3]
or A[i] <= A[i + 2] <= A[i + 3]
or A[i] >= A[i + 2] >= A[i + 3]
or A[i + 1] <= A[i + 2] <= A[i + 3]
or A[i + 1] >= A[i + 2] >= A[i + 3]
):
ANS += 3
continue
ANS += 4
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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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 VAR BIN_OP VAR NUMBER VAR NUMBER 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 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 VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for case in range(t):
n = int(input())
b = [int(x) for x in input().split()]
threes = 0
for i in range(2, n):
if (
b[i] >= b[i - 1]
and b[i - 1] >= b[i - 2]
or b[i] <= b[i - 1]
and b[i - 1] <= b[i - 2]
):
pass
else:
threes += 1
fours = 0
for i in range(3, n):
if (
b[i] < b[i - 2]
and b[i - 1] < b[i - 3]
and b[i] > b[i - 1]
and b[i - 2] > b[i - 3]
or b[i] > b[i - 2]
and b[i - 1] > b[i - 3]
and b[i] < b[i - 1]
and b[i - 2] < b[i - 3]
):
fours += 1
print(2 * n - 1 + threes + fours) | 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 FOR VAR FUNC_CALL VAR NUMBER VAR IF 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 VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF 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 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 VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def Manhattan_distance(p, q):
return abs(p[0] - q[0]) + abs(p[1] - q[1])
def is_bad_triple(p, q, r):
x = Manhattan_distance(p, q)
y = Manhattan_distance(p, r)
z = Manhattan_distance(r, q)
X = [x, y, z]
return X[0] == X[1] + X[2] or X[1] == X[0] + X[2] or X[2] == X[1] + X[0]
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print(1)
elif n == 2:
print(3)
else:
b = [(a[i], i + 1) for i in range(n)]
dp = [(2) for i in range(n)]
dp[-1] = 1
for i in range(n - 1, -1, -1):
for l in range(3, n + 1):
if i + l <= n and dp[i] >= l - 1 and dp[i + 1] >= l - 1:
flag = True
for j in range(i + 1, i + l - 1):
if is_bad_triple(b[i], b[j], b[i + l - 1]):
flag = False
break
if flag:
dp[i] = l
else:
break
ans = sum(dp)
print(ans) | FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER 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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def ok3(a1, a2, a3):
if (a2 - a1) * (a2 - a3) > 0:
return 1
return 0
def ok4(a1, a2, a3, a4):
if ok3(a1, a2, a3) and ok3(a1, a2, a4) and ok3(a1, a3, a4) and ok3(a2, a3, a4):
return 1
return 0
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = 2 * n - 1
for j in range(2, n):
if ok3(a[j - 2], a[j - 1], a[j]):
ans += 1
for j in range(3, n):
if ok4(a[j - 3], a[j - 2], a[j - 1], a[j]):
ans += 1
print(ans) | FUNC_DEF IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def is_triplet(a, b, c):
if a <= b and b <= c:
return True
if a >= b and b >= c:
return True
return False
T = int(input())
for t in range(T):
n = int(input())
arr = list(map(int, input().split()))
ct = n + n - 1
for i in range(n - 2):
if is_triplet(arr[i], arr[i + 1], arr[i + 2]):
continue
ct += 1
for i in range(n - 3):
if (
is_triplet(arr[i], arr[i + 1], arr[i + 3])
or is_triplet(arr[i], arr[i + 2], arr[i + 3])
or is_triplet(arr[i + 1], arr[i + 2], arr[i + 3])
or is_triplet(arr[i], arr[i + 1], arr[i + 2])
):
continue
ct += 1
print(ct) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def solve():
n = int(input())
a = [int(i) for i in input().split(" ")]
ans = 2 * n - 1
for i in range(n - 2):
if not (
a[i] <= a[i + 1]
and a[i + 1] <= a[i + 2]
or a[i] >= a[i + 1]
and a[i + 1] >= a[i + 2]
):
ans += 1
for i in range(n - 3):
extra = 1
for l in range(i, i + 2):
for r in range(i + 2, i + 4):
for mid in range(l + 1, r):
if (
a[l] <= a[mid]
and a[mid] <= a[r]
or a[l] >= a[mid]
and a[mid] >= a[r]
):
extra = 0
ans += extra
print(ans)
t = int(input())
while t > 0:
t -= 1
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF 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 VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for o in range(t):
ans = 0
n = int(input())
l = list(map(int, input().strip().split()))
if n == 1:
print(1)
continue
elif n == 2:
print(3)
continue
for i in range(n - 2):
ans += 2
b0 = l[i]
b1 = l[i + 1]
lower = -1000000000000000
upper = 1000000000000000
if b0 == b1:
continue
elif b0 < b1:
upper = b1
else:
lower = b1
b2 = l[i + 2]
if b2 > lower and b2 < upper:
ans += 1
else:
continue
if b2 > b0 and b2 < b1:
continue
if b2 < b0 and b2 > b1:
continue
if b2 > b0:
upper = b2
if b2 < b0:
lower = b2
if b2 == b0 or b2 == b1:
continue
if i + 3 < n:
b3 = l[i + 3]
if b3 > lower and b3 < upper:
ans += 1
ans += 3
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
def check3(a, b, c):
return (a - b) * (c - b) > 0
def check4(a, b, c, d):
return check3(a, b, c) and check3(a, b, d) and check3(a, c, d) and check3(b, c, d)
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
res = n + n - 1
for i in range(n - 2):
if check3(a[i], a[i + 1], a[i + 2]):
res += 1
for i in range(n - 3):
if check4(a[i], a[i + 1], a[i + 2], a[i + 3]):
res += 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR 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 VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
h = []
i, j = 0, 1
ans = 1
while j < n:
d = []
if i + 3 < j:
i += 1
notgood = True
while i + 1 < j and notgood:
notgood = False
bad = i
for x in range(i, j + 1):
for y in range(x + 1, j + 1):
for z in range(y + 1, j + 1):
d = sorted(
[
abs(x - y) + abs(a[x] - a[y]),
abs(y - z) + abs(a[y] - a[z]),
abs(z - x) + abs(a[z] - a[x]),
]
)
if d[0] + d[1] == d[2]:
notgood = True
bad = x
if notgood:
i = bad + 1
ans += j - i + 1
j += 1
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 LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def sol_mansub(n, ar):
res = 0
for i in range(len(ar)):
if i >= n - 2:
l = n - i
res += l * (l + 1) // 2
break
a, b, c = ar[i : i + 3]
if a <= b <= c or a >= b >= c:
res += 2
continue
l = 3
if i + 3 < n:
d = ar[i + 3]
if not (
a <= b <= d
or a <= c <= d
or b <= c <= d
or a >= b >= d
or a >= c >= d
or b >= c >= d
):
l += 1
res += l
return res
def main():
t = int(input())
for _ in range(t):
(n,) = [int(x) for x in input().split()]
ar = [int(x) for x in input().split()]
print(sol_mansub(n, ar))
main() | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | PI = 3.141592653589793
INF = float("inf")
MOD = 1000000007
def bin32(num):
return "{0:032b}".format(num)
def add(x, y):
return (x + y) % MOD
def sub(x, y):
return (x - y + MOD) % MOD
def mul(x, y):
return x * y % MOD
def gcd(x, y):
if y == 0:
return x
return gcd(y, x % y)
def lcm(x, y):
return x * y // gcd(x, y)
def power(x, y):
res = 1
x %= MOD
while y != 0:
if y & 1:
res = mul(res, x)
y >>= 1
x = mul(x, x)
return res
def mod_inv(n):
return power(n, MOD - 2)
def prob(p, q):
return mul(p, power(q, MOD - 2))
def ii():
return int(input())
def li():
return [int(i) for i in input().split()]
def ls():
return [i for i in input().split()]
for t in range(ii()):
t += 1
n = ii()
a = li()
if n == 1:
print(1)
continue
elif n == 2:
print(3)
continue
ans = 0
for i in range(n - 3):
x = a[i : i + 4][:]
maxi = 4
if x[0] <= x[1] <= x[2] or x[0] >= x[1] >= x[2]:
maxi = 2
elif (
x[0] <= x[2] <= x[3]
or x[0] >= x[2] >= x[3]
or x[1] >= x[2] >= x[3]
or x[1] <= x[2] <= x[3]
or x[0] <= x[1] <= x[3]
or x[0] >= x[1] >= x[3]
):
maxi = 3
else:
maxi = 4
ans += maxi
if a[n - 3] <= a[n - 2] <= a[n - 1] or a[n - 3] >= a[n - 2] >= a[n - 1]:
ans += 2
else:
ans += 3
ans += 3
print(ans) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER 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 VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR 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 VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def _sum_(n):
res = 0
for i in range(1, min(5, n + 1)):
res += n + 1 - i
return res
def test(b):
for i in range(4):
for j in range(i + 1, 4):
for k in range(j + 1, 4):
if b[i] <= b[j] <= b[k] or b[i] >= b[j] >= b[k]:
return True
return False
for q in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ans = _sum_(n)
for i in range(2, n):
if a[i - 2] <= a[i - 1] <= a[i]:
ans -= 1
elif a[i - 2] >= a[i - 1] >= a[i]:
ans -= 1
for i in range(n - 3):
if test(a[i : i + 4]):
ans -= 1
print(ans) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR 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 FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
ans = n + n - 1
for i in range(2, n):
if a[i - 2] <= a[i - 1] <= a[i]:
continue
if a[i - 2] >= a[i - 1] >= a[i]:
continue
ans += 1
for i in range(3, n):
if a[i - 3] <= a[i - 2] <= a[i - 1]:
continue
if a[i - 2] <= a[i - 1] <= a[i]:
continue
if a[i - 3] <= a[i - 2] <= a[i]:
continue
if a[i - 3] <= a[i - 1] <= a[i]:
continue
if a[i - 3] >= a[i - 2] >= a[i - 1]:
continue
if a[i - 2] >= a[i - 1] >= a[i]:
continue
if a[i - 3] >= a[i - 2] >= a[i]:
continue
if a[i - 3] >= a[i - 1] >= a[i]:
continue
ans += 1
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
def dist(a, b, c, d):
return abs(a - c) + abs(b - d)
def bad(a, b, c, d, e, f):
return (
dist(a, b, c, d) == dist(a, b, e, f) + dist(e, f, c, d)
or dist(a, b, e, f) == dist(a, b, c, d) + dist(e, f, c, d)
or dist(e, f, c, d) == dist(a, b, e, f) + dist(a, b, c, d)
)
for i in range(t):
n = int(input())
a = []
a = str(input()).split(" ")
ans = 0
for i in range(n):
j = i
f = []
while j < n:
f.append((int(a[j]), j))
ok = True
for ise in range(len(f)):
for js in range(ise + 1, len(f)):
for k in range(js + 1, len(f)):
if bad(
f[ise][0], f[ise][1], f[js][0], f[js][1], f[k][0], f[k][1]
):
ok = False
break
if not ok:
break
if not ok:
break
if not ok:
break
j += 1
ans += j - i
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
ans = n + (n - 1)
for i in range(n):
if i + 2 < n:
if (
arr[i + 1] >= arr[i]
and arr[i + 2] >= arr[i + 1]
or arr[i + 1] <= arr[i]
and arr[i + 2] <= arr[i + 1]
):
continue
ans += 1
if i + 3 < n:
if (
arr[i + 2] >= arr[i]
and arr[i + 3] >= arr[i + 2]
or arr[i + 2] <= arr[i]
and arr[i + 3] <= arr[i + 2]
):
continue
if (
arr[i + 1] >= arr[i]
and arr[i + 3] >= arr[i + 1]
or arr[i + 1] <= arr[i]
and arr[i + 3] <= arr[i + 1]
):
continue
if (
arr[i + 2] >= arr[i + 1]
and arr[i + 3] >= arr[i + 2]
or arr[i + 2] <= arr[i + 1]
and arr[i + 3] <= arr[i + 2]
):
continue
ans += 1
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 VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR 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 BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER 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 BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER 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 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 EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
c = n + max(n - 1, 0) + max(n - 2, 0) + max(n - 3, 0)
for j in range(n - 2):
if (
a[j] <= a[j + 1]
and a[j + 1] <= a[j + 2]
or a[j] >= a[j + 1]
and a[j + 1] >= a[j + 2]
):
c -= 1
for j in range(n - 3):
if (
a[j] <= a[j + 1]
and a[j + 1] <= a[j + 2]
or a[j] >= a[j + 1]
and a[j + 1] >= a[j + 2]
):
c -= 1
elif (
a[j] <= a[j + 1]
and a[j + 1] <= a[j + 3]
or a[j] >= a[j + 1]
and a[j + 1] >= a[j + 3]
):
c -= 1
elif (
a[j] <= a[j + 2]
and a[j + 2] <= a[j + 3]
or a[j] >= a[j + 2]
and a[j + 2] >= a[j + 3]
):
c -= 1
elif (
a[j + 1] <= a[j + 2]
and a[j + 2] <= a[j + 3]
or a[j + 1] >= a[j + 2]
and a[j + 2] >= a[j + 3]
):
c -= 1
print(c) | 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 VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF 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 VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF 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 VAR BIN_OP VAR NUMBER VAR NUMBER IF 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 VAR BIN_OP VAR NUMBER VAR NUMBER IF 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 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 BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def check(a, b, c):
if a <= b and b <= c or a >= b and b >= c:
return True
else:
return False
t = int(input())
for _ in range(t):
n = int(input())
a = [0] + list(map(int, input().split()))
w = n + n - 1
x = 0
if n >= 3:
x += n - 2
for i in range(1, n - 1):
if check(a[i], a[i + 1], a[i + 2]):
x -= 1
y = 0
if n >= 4:
y += n - 3
for i in range(1, n - 2):
if check(a[i], a[i + 1], a[i + 2]):
y -= 1
elif check(a[i], a[i + 1], a[i + 3]):
y -= 1
elif check(a[i], a[i + 2], a[i + 3]):
y -= 1
elif check(a[i + 1], a[i + 2], a[i + 3]):
y -= 1
answer = w + x + y
print(answer) | FUNC_DEF IF VAR VAR 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 BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def check(arr):
ret = True
for one in range(4):
for two in range(4):
for three in range(4):
if one != two and two != three and one != three:
dist = [
abs(arr[one][0] - arr[two][0]) + abs(arr[one][1] - arr[two][1]),
abs(arr[one][0] - arr[three][0])
+ abs(arr[one][1] - arr[three][1]),
abs(arr[three][0] - arr[two][0])
+ abs(arr[three][1] - arr[two][1]),
]
dist.sort()
if dist[0] + dist[1] == dist[2]:
ret = False
break
if ret == False:
break
if ret == False:
break
return ret
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print(1)
elif n == 2:
print(3)
else:
ans = n + (n - 1) + (n - 2) + (n - 3)
for i in range(n - 2):
dist = [
abs(a[i] - a[i + 1]) + 1,
abs(a[i + 1] - a[i + 2]) + 1,
abs(a[i] - a[i + 2]) + 2,
]
dist.sort()
if dist[0] + dist[1] == dist[2]:
ans -= 1
for i in range(n - 3):
x = [[a[i], i + 1], [a[i + 1], i + 2], [a[i + 2], i + 3], [a[i + 3], i + 4]]
if check(x) == False:
ans -= 1
print(ans) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF 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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST VAR VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def checkIfValidArr(arr):
if len(arr) <= 2:
return True
if len(arr) >= 5:
return False
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
for k in range(j + 1, len(arr)):
if (
arr[i] <= arr[j]
and arr[j] <= arr[k]
or arr[i] >= arr[j]
and arr[j] >= arr[k]
):
return False
return True
def findNumGoodArr(arr):
numGoodArray = 0
for i in range(len(arr)):
temp_arr = []
for j in range(i, len(arr)):
temp_arr.append(arr[j])
if checkIfValidArr(temp_arr) is True:
numGoodArray += 1
else:
break
return numGoodArray
t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
print(findNumGoodArr(arr)) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def check(a, i):
c = 0
if (
abs(a[i] - a[i + 2]) + 2
!= abs(a[i] - a[i + 1]) + 1 + abs(a[i + 1] - a[i + 2]) + 1
):
if (
abs(a[i] - a[i + 3]) + 3
!= abs(a[i] - a[i + 1]) + 1 + abs(a[i + 1] - a[i + 3]) + 2
):
if (
abs(a[i] - a[i + 3]) + 3
!= abs(a[i] - a[i + 2]) + 2 + abs(a[i + 2] - a[i + 3]) + 1
):
if (
abs(a[i + 1] - a[i + 3]) + 2
!= abs(a[i + 1] - a[i + 2]) + 1 + abs(a[i + 2] - a[i + 3]) + 1
):
c = c + 1
return c
def check1(a, i):
c = 0
if (
abs(a[i] - a[i + 2]) + 2
!= abs(a[i] - a[i + 1]) + 1 + abs(a[i + 1] - a[i + 2]) + 1
):
c = c + 1
return c
t = int(input())
for _ in range(t):
n = int(input())
a = [int(d) for d in input().split()]
if len(a) == 1:
print(1)
elif len(a) == 2:
print(3)
elif len(a) == 3:
if abs(a[0] - a[2]) + 2 != abs(a[1] - a[2]) + 1 + abs(a[0] - a[1]) + 1:
print(6)
else:
print(5)
else:
c = n + n - 1
for i in range(n - 3):
c = c + check(a, i)
for i in range(n - 2):
c = c + check1(a, i)
print(c) | FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | A = []
t = int(input())
def isg(l, r):
for i in range(l, r + 1):
for j in range(i + 1, r + 1):
for k in range(j + 1, r + 1):
if A[i] >= A[j] and A[j] >= A[k]:
return False
if A[i] <= A[j] and A[j] <= A[k]:
return False
return True
for i in range(t):
n = int(input())
A = list(map(int, input().split()))
ans = 0
j = 0
for i in range(n):
while isg(j, i) == False:
j += 1
ans += i - j + 1
print(ans) | ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN 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 VAR WHILE FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | from sys import stdin
def A():
t = int(stdin.readline())
while t:
t -= 1
s = int(stdin.readline())
cnt = []
res = 1
cnt.append(1)
while s > res:
s -= res
res += 2
if s < res:
cnt.append(s)
else:
cnt.append(res)
print(len(cnt))
def B():
t = int(stdin.readline())
while t:
t -= 1
s1 = [int(x) for x in stdin.readline().split()]
n, a, b = s1[0], s1[1], s1[2]
s = stdin.readline()
cnt = 0
res = 0
for i in range(0, len(s)):
if i == len(s) - 1:
if s[i] == "0":
res += a * (cnt + 1) + b
break
if s[i] == s[i + 1] == "0":
cnt += 1
else:
if s[i] == "0":
res += a * (cnt + 1) + b
cnt = 0
cnt_1 = 0
for i in s:
if i == "1":
cnt_1 += 1
if cnt_1 > 0:
res += a * cnt_1 + b
res1 = 0
cnt = 0
for i in range(0, len(s)):
if i == len(s) - 1:
if s[i] == "1":
res1 += a * (cnt + 1) + b
break
if s[i] == s[i + 1] == "1":
cnt += 1
else:
if s[i] == "1":
res1 += a * (cnt + 1) + b
cnt = 0
cnt_1 = 0
for i in s:
if i == "0":
cnt_1 += 1
if cnt_1 > 0:
res1 += a * cnt_1 + b
print(max(res, n * (a + b), res1))
def count(a):
n = len(a)
for j in range(0, n):
for k in range(j + 1, n):
for k1 in range(k + 1, n):
if min(a[j], a[k1]) <= a[k] <= max(a[j], a[k1]):
return False
return True
def C():
t = int(stdin.readline())
while t:
t -= 1
n = int(stdin.readline())
a = [int(x) for x in stdin.readline().split()]
res = 0
for i in range(1, 5):
for j in range(0, n - i + 1):
res += count(a[j : j + i])
print(res)
C() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def d(p1, p2):
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
sol = 0
for i in range(n - 2):
done = False
p1 = [a[i + 0], 1]
p2 = [a[i + 1], 2]
p3 = [a[i + 2], 3]
ds = [d(p1, p2), d(p2, p3), d(p1, p3)]
ds = sorted(ds)
if ds[2] != ds[1] + ds[0]:
sol += 1
if i < n - 3:
al = a[i : i + 4]
if (
a[i + 0] != a[i + 1]
and a[i + 0] != a[i + 2]
and a[i + 3] != a[i + 1]
and a[i + 3] != a[i + 2]
):
if (
a[i + 1] == min(al)
and a[i + 2] == max(al)
or a[i + 1] == max(al)
and a[i + 2] == min(al)
):
sol += 1
print(sol + n - 1 + n) | FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR 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 BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split(" ")))
dp = [(0) for i in range(n)]
for k in range(n):
maxs = [[a[k], k]]
mins = [[a[k], k]]
cnt = 1
if k >= n - 2:
cnt = n - k
else:
for pro in range(k + 1, min(n, k + 5)):
if a[pro] >= a[k]:
maxs.append([a[pro], pro])
if a[pro] <= a[k]:
mins.append([a[pro], pro])
if len(maxs) > 2:
if maxs[-1][0] >= maxs[-2][0]:
break
if mins:
if mins[-1][1] > maxs[-1][1]:
break
if len(mins) > 2:
if mins[-1][0] <= mins[-2][0]:
break
if maxs:
if maxs[-1][1] > mins[-1][1]:
break
if len(maxs) == 4:
if maxs[-1] <= maxs[-2] <= maxs[-3]:
break
if len(mins) == 4:
if mins[-1] >= mins[-2] >= mins[-3]:
break
cnt += 1
dp[k] = cnt
print(sum(dp)) | 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 NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST VAR VAR VAR ASSIGN VAR LIST LIST VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
if n < 3:
print(2 * n - 1)
else:
k = 0
ans = 2 * n - 1
for i in range(2, n):
if (
arr[i - 2] < arr[i - 1]
and arr[i - 1] > arr[i]
or arr[i - 2] > arr[i - 1]
and arr[i - 1] < arr[i]
):
ans += 1
if k == 1 and not (
arr[i - 3] <= arr[i - 2] <= arr[i]
or arr[i - 3] >= arr[i - 2] >= arr[i]
or arr[i - 3] <= arr[i - 1] <= arr[i]
or arr[i - 3] >= arr[i - 1] >= arr[i]
):
ans += 1
else:
k = 1
else:
k = 0
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 EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF 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 VAR VAR NUMBER IF 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 BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def fun(arr):
total = 2 * n - 1
for i in range(n - 2):
total += (arr[i] - arr[i + 1]) * (arr[i + 1] - arr[i + 2]) < 0
for i in range(n - 3):
total += (arr[i + 1] - arr[i + 3]) * (arr[i + 3] - arr[i + 2]) > 0 and (
arr[i + 1] - arr[i]
) * (arr[i] - arr[i + 2]) > 0
print(total)
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
fun(arr) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR 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 BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL 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 EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def ok(b):
if len(b) <= 2:
return True
n = len(b)
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if b[i] >= b[j] >= b[k] or b[i] <= b[j] <= b[k]:
return False
return True
def findCount():
n = int(input())
nums = list(map(int, input().split()))
groups = []
ans = 0
for i in range(n):
b = []
for j in range(i, n):
b.append(nums[j])
if ok(b):
ans += 1
else:
break
print(ans)
def findSubarrays():
t = int(input())
for k in range(t):
findCount()
findSubarrays() | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def fuk(a):
if a[0] <= a[1] <= a[2]:
return 1
if a[0] >= a[1] >= a[2]:
return 1
return 0
def fukk(a):
if a[0] <= a[1] <= a[2]:
return 1
if a[1] <= a[2] <= a[3]:
return 1
if a[0] <= a[2] <= a[3]:
return 1
if a[0] <= a[1] <= a[3]:
return 1
if a[0] >= a[1] >= a[2]:
return 1
if a[1] >= a[2] >= a[3]:
return 1
if a[0] >= a[2] >= a[3]:
return 1
if a[0] >= a[1] >= a[3]:
return 1
return 0
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
ans = 0
ll = []
if n >= 3:
for i in range(3):
ll.append(l[i])
for i in range(3, n):
if fuk(ll):
ans += 1
ll.pop(0)
ll.append(l[i])
ans += fuk(ll)
ll = []
if n > 3:
for i in range(4):
ll.append(l[i])
for i in range(4, n):
if fukk(ll):
ans += 1
ll.pop(0)
ll.append(l[i])
ans += fukk(ll)
q = 0
for i in range(n):
if i <= 3:
q += n - i
else:
break
print(q - ans) | FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER 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 IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | import sys
def check_triple(x, y, z):
return (x - y) * (y - z) < 0
def solve(arr):
n = len(arr)
base = n + (n - 1)
triple = 0
for i in range(2, n):
to_check = arr[i - 2 : i + 1]
if check_triple(*to_check):
triple += 1
quadro = 0
for i in range(3, n):
candidates = arr[i - 3 : i + 1]
found = False
for j in range(4):
to_check = candidates[:j] + candidates[j + 1 :]
if not check_triple(*to_check):
found = True
break
if not found:
quadro += 1
return base + triple + quadro
def main():
outputs = []
fin = sys.stdin
N = int(fin.readline())
for _ in range(N):
n = int(fin.readline())
arr = list(map(int, fin.readline().split()))
outputs.append(solve(arr))
print("\n".join(map(str, outputs)))
main() | IMPORT FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR LIST 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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
i = 2
j = 0
goods = n + n - 1
last_good = False
while i < len(a):
if not a[i - 2] <= a[i - 1] <= a[i] and not a[i - 2] >= a[i - 1] >= a[i]:
if last_good:
if (
not a[i - 3] <= a[i - 2] <= a[i]
and not a[i - 3] >= a[i - 2] >= a[i]
and not a[i - 3] >= a[i - 1] >= a[i]
and not a[i - 3] <= a[i - 1] <= a[i]
):
goods += 1
goods += 1
else:
goods += 1
last_good = True
else:
last_good = False
i += 1
print(goods) | 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 BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR IF 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 BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def main():
t = int(input())
for _ in range(t):
n = int(input())
a = [int(word) for word in input().split()]
dp = [(1) for i in range(n)]
for i in range(1, n):
cur = 1
up = []
down = []
for j in range(i - 1, max(i - 5, -1), -1):
if a[j] >= a[i]:
up.append(a[j])
if len(up) > 2:
break
if len(up) == 2 and up[1] >= up[0]:
break
if a[j] <= a[i]:
down.append(a[j])
if len(down) > 2:
break
if len(down) == 2 and down[1] <= down[0]:
break
cur += 1
dp[i] = min(dp[i - 1] + 1, cur)
print(sum(dp))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | from itertools import combinations
from sys import stdin
input = stdin.readline
def check(d):
for i in range(4):
b = d[:i] + d[i + 1 :]
if b == sorted(b) or b == sorted(b, reverse=True):
return False
return True
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ans = 2 * n - 1
for i in range(n - 2):
b = a[i : i + 3]
if not (b == sorted(b) or b == sorted(b, reverse=True)):
ans += 1
for i in range(n - 3):
b = a[i : i + 4]
if check(b):
ans += 1
print(ans) | ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR 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 BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def o(p1):
r = len(p1)
if r < 3:
return True
if r >= 5:
return False
for i in range(r):
for k in range(i + 1, r):
for s in range(k + 1, r):
if (
p1[i] <= p1[k]
and p1[k] <= p1[s]
or p1[i] >= p1[k]
and p1[k] >= p1[s]
):
return False
return True
z = int(input())
for _ in range(z):
n = int(input())
arr = list(map(int, input().split()))
i = 0
an = 0
while i < n:
y = []
for j in range(i, n):
y.append(arr[j])
if o(y):
an += 1
else:
break
i += 1
print(an) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def check(arr):
for a in range(len(arr) - 2):
for b in range(a + 1, len(arr) - 1):
for c in range(b + 1, len(arr)):
r1 = arr[b] - arr[a]
r2 = arr[c] - arr[b]
if r1 <= 0 and r2 <= 0:
return True
if r1 >= 0 and r2 >= 0:
return True
return False
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
res = 0
for right in range(0, n):
for left in range(max(0, right - 3), right + 1):
if not check(a[left : right + 1]):
res += 1
print(res) | FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER 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 FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
ans = n + n - 1
f = 0
for i in range(n - 2):
if (l[i + 1] - l[i]) * (l[i + 2] - l[i + 1]) < 0:
ans += 1
for i in range(n - 3):
if (l[i + 1] - l[i]) * (l[i + 2] - l[i]) < 0 and (l[i + 3] - l[i + 1]) * (
l[i + 3] - l[i + 2]
) < 0:
ans += 1
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 BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP 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 VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER 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 EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | f = lambda x: x == sorted(x) or x[::-1] == sorted(x)
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
o = 0
for i in range(n - 2):
if not f(a[i : i + 3]):
o += 1
for i in range(n - 3):
if not any(f([a[i + j] for j in range(4) if j != k]) for k in range(4)):
o += 1
print(o + n + n - 1) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = 0
ans += n
ans += n - 1
for i in range(n - 2):
if (a[i + 1] - a[i]) * (a[i + 2] - a[i + 1]) < 0:
ans += 1
for i in range(n - 3):
if (a[i + 1] - a[i]) * (a[i + 2] - a[i + 1]) < 0:
if a[i + 2] < min(a[i], a[i + 1]):
if a[i + 2] < a[i + 3] < a[i + 1]:
ans += 1
elif a[i + 2] > max(a[i], a[i + 1]):
if a[i + 2] > a[i + 3] > a[i + 1]:
ans += 1
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 ASSIGN VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP 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 VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP 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 BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
def checker(brr):
m = len(brr)
for i in range(m):
for j in range(i + 1, m):
for k in range(j + 1, m):
if (
brr[i] <= brr[j]
and brr[j] <= brr[k]
or brr[i] >= brr[j]
and brr[j] >= brr[k]
):
return False
return True
ans = 0
for i in range(n):
brr = []
for j in range(i, n):
brr.append(arr[j])
if len(brr) <= 2:
ans += 1
continue
elif len(brr) >= 5:
break
elif checker(brr):
ans += 1
else:
break
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 ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | for _ in range(int(input())):
n = int(input())
yaxis = list(map(int, input().split()))
count = 0
for i in range(n):
if i - 2 < 0:
count += i + 1
continue
count += 2
j = i - 1
k = i - 2
count += 1
if yaxis[i] >= yaxis[j] and yaxis[j] >= yaxis[k]:
count -= 1
continue
elif yaxis[i] <= yaxis[j] and yaxis[j] <= yaxis[k]:
count -= 1
continue
if k > 0:
l = k - 1
count += 1
if yaxis[i] >= yaxis[j] and yaxis[j] >= yaxis[l]:
count -= 1
elif yaxis[i] >= yaxis[k] and yaxis[k] >= yaxis[l]:
count -= 1
elif yaxis[j] >= yaxis[k] and yaxis[k] >= yaxis[l]:
count -= 1
elif yaxis[i] <= yaxis[j] and yaxis[j] <= yaxis[l]:
count -= 1
elif yaxis[i] <= yaxis[k] and yaxis[k] <= yaxis[l]:
count -= 1
elif yaxis[j] <= yaxis[k] and yaxis[k] <= yaxis[l]:
count -= 1
print(count) | 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 FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | import sys
input = lambda: sys.stdin.readline().rstrip()
def solve_tc():
ans = 0
n = int(input())
s = list(map(int, input().split()))
for i in range(n - 2):
if (
s[i + 1] < s[i]
and s[i + 1] < s[i + 2]
or s[i + 1] > s[i]
and s[i + 1] > s[i + 2]
):
ans += 1
if i != n - 3:
if s[i + 1] < s[i] and s[i + 1] < s[i + 3]:
if s[i + 2] > s[i] and s[i + 2] > s[i + 3]:
ans += 1
elif s[i + 1] > s[i] and s[i + 1] > s[i + 3]:
if s[i + 2] < s[i] and s[i + 2] < s[i + 3]:
ans += 1
return str(ans + 2 * n - 1)
t = int(input())
while t > 0:
t -= 1
sys.stdout.write(solve_tc() + "\n") | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL 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 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 IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | n_tests = int(input())
def is_good(array):
l = len(array)
for i in range(l):
for j in range(i + 1, l):
for k in range(j + 1, l):
if (
array[k] >= array[j]
and array[j] >= array[i]
or array[k] <= array[j]
and array[j] <= array[i]
):
return False
return True
def process():
n = int(input())
vals = list(map(int, input().split()))
ans = n + n - 1
for i in range(n):
for j in range(i + 2, n):
if not is_good(vals[i : j + 1]):
break
ans += 1
print(ans)
for i in range(n_tests):
process() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN 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 BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | import sys
input = sys.stdin.readline
def main():
def good(i, j, k):
return not (ar[k] >= ar[j] >= ar[i] or ar[k] <= ar[j] <= ar[i])
t = int(input())
for _ in range(t):
n = int(input())
ar = list(map(int, input().split()))
ans = 2 * n - 1
bad = [False] * n
for i in range(n - 2):
if good(i, i + 1, i + 2):
ans += 1
for i in range(n - 3):
if (
good(i, i + 1, i + 2)
and good(i, i + 1, i + 3)
and good(i, i + 2, i + 3)
and good(i + 1, i + 2, i + 3)
):
ans += 1
print(ans)
main() | IMPORT ASSIGN VAR VAR FUNC_DEF FUNC_DEF RETURN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR 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 ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def check(a):
if len(a) == 3:
if a[0] >= a[1] >= a[2] or a[0] <= a[1] <= a[2]:
return False
return True
else:
for i in range(4):
b = []
for j in range(4):
if i != j:
b.append(a[j])
if check(b) == False:
return False
return True
def main():
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
ans = n + n - 1
for i in range(n - 2):
if check(arr[i : i + 3]):
ans += 1
for i in range(n - 3):
if check(arr[i : i + 4]):
ans += 1
print(ans)
main() | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF 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 VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | def bad_triplet(a, b, c):
if a <= b and b <= c:
return True
if a >= b and b >= c:
return True
return False
def solve():
n = int(input())
arr = [int(x) for x in input().split()]
if n < 3:
return n + n - 1
good_subarrays = n + n - 1 + n - 2 + n - 3
for i in range(n - 2):
if bad_triplet(arr[i], arr[i + 1], arr[i + 2]):
good_subarrays -= 1
for i in range(n - 3):
if (
bad_triplet(arr[i], arr[i + 1], arr[i + 2])
or bad_triplet(arr[i], arr[i + 1], arr[i + 3])
or bad_triplet(arr[i + 1], arr[i + 2], arr[i + 3])
or bad_triplet(arr[i], arr[i + 2], arr[i + 3])
):
good_subarrays -= 1
return good_subarrays
for _ in range(int(input())):
print(solve()) | FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.readline())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def LI1():
return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number):
return [LI1() for _ in range(rows_number)]
def SI():
return sys.stdin.readline().rstrip()
inf = 10**16
md = 10**9 + 7
def solve():
n = II()
aa = LI()
ans = n + n - 1
for i in range(n - 2):
if aa[i] <= aa[i + 1] <= aa[i + 2] or aa[i] >= aa[i + 1] >= aa[i + 2]:
continue
ans += 1
for i in range(n - 3):
mn = min(aa[i], aa[i + 3])
mx = max(aa[i], aa[i + 3])
MN = min(aa[i + 1], aa[i + 2])
MX = max(aa[i + 1], aa[i + 2])
if MN < mn and MX > mx:
ans += 1
print(ans)
for testcase in range(II()):
solve() | IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL 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 IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | answers = []
def solve(n, arr):
if n == 1:
print(1)
return
if n == 2:
print(3)
return
ans = 3
for i in range(n - 3):
ans += 2
if arr[i] == arr[i + 1]:
continue
if arr[i] > arr[i + 1]:
if arr[i + 1] >= arr[i + 2]:
continue
ans += 1
if arr[i + 2] <= arr[i]:
continue
if arr[i + 2] > arr[i]:
if arr[i + 3] >= arr[i + 2]:
continue
if arr[i + 3] > arr[i + 1]:
ans += 1
else:
if arr[i + 1] <= arr[i + 2]:
continue
ans += 1
if arr[i + 2] >= arr[i]:
continue
if arr[i + 2] < arr[i]:
if arr[i + 3] <= arr[i + 2]:
continue
if arr[i + 3] < arr[i + 1]:
ans += 1
f = n - 3
ans += 2
one = arr[n - 3]
sec = arr[n - 2]
thi = arr[n - 1]
if one > sec and thi > sec:
ans += 1
if one < sec and thi < sec:
ans += 1
print(ans)
T = int(input())
while T:
n = int(input())
arr = [int(x) for x in input().split()]
solve(n, arr)
T -= 1
for ans in answers:
print(ans) | ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | import sys
t = int(input())
def find(lis):
n = len(lis)
for a in range(n - 2):
for b in range(a + 1, n):
for c in range(b + 1, n):
if lis[a] <= lis[b] and lis[b] <= lis[c]:
return False
if lis[a] >= lis[b] and lis[b] >= lis[c]:
return False
return True
for tt in range(t):
answer = 0
n = int(sys.stdin.readline())
sequence = list(map(int, sys.stdin.readline().split()))
start = 0
for i in range(min(n, 2)):
answer += i + 1
if n >= 3:
if find(sequence[0:3]):
answer += 3
else:
answer += 2
for i in range(3, n):
if find(sequence[i - 3 : i + 1]):
answer += 4
elif find(sequence[i - 2 : i + 1]):
answer += 3
else:
answer += 2
print(answer) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose you have two points $p = (x_p, y_p)$ and $q = (x_q, y_q)$. Let's denote the Manhattan distance between them as $d(p, q) = |x_p - x_q| + |y_p - y_q|$.
Let's say that three points $p$, $q$, $r$ form a bad triple if $d(p, r) = d(p, q) + d(q, r)$.
Let's say that an array $b_1, b_2, \dots, b_m$ is good if it is impossible to choose three distinct indices $i$, $j$, $k$ such that the points $(b_i, i)$, $(b_j, j)$ and $(b_k, k)$ form a bad triple.
You are given an array $a_1, a_2, \dots, a_n$. Calculate the number of good subarrays of $a$. A subarray of the array $a$ is the array $a_l, a_{l + 1}, \dots, a_r$ for some $1 \le l \le r \le n$.
Note that, according to the definition, subarrays of length $1$ and $2$ are good.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 5000$) β the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the number of good subarrays of array $a$.
-----Examples-----
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
-----Note-----
In the first test case, it can be proven that any subarray of $a$ is good. For example, subarray $[a_2, a_3, a_4]$ is good since it contains only three elements and:
$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$ $<$ $d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$;
$d((a_2, 2), (a_3, 3))$ $<$ $d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$;
$d((a_3, 3), (a_4, 4))$ $<$ $d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$;
In the second test case, for example, subarray $[a_1, a_2, a_3, a_4]$ is not good, since it contains a bad triple $(a_1, 1)$, $(a_2, 2)$, $(a_4, 4)$:
$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$;
$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$;
$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$;
So, $d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$. | import sys
input = sys.stdin.readline
def dist(a, b, idxa, idxb):
return abs(a - b) + abs(idxa - idxb)
def solve():
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
for j in range(i, n):
if i + 2 <= j:
flag = True
for k in range(i, j):
for l in range(k + 1, j):
if dist(a[k], a[j], k, j) == dist(a[k], a[l], k, l) + dist(
a[l], a[j], l, j
):
flag = False
if not flag:
break
ans += 1
print(ans)
return
t = int(input())
for _ in range(t):
solve() | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.