description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
def solve(n, a, b): if n % 2: if a[n // 2] != b[n // 2]: return "no" def get_pairs(a): ret = [] for i in range(len(a) // 2): x, y = a[i], a[n - i - 1] if x > y: x, y = y, x ret.append((x, y)) return sorted(ret) flag = get_pairs(a) == get_pairs(b) if flag: return "YES" return "NO" def main(): inp = lambda: [int(x) for x in input().split()] inn = lambda: int(input()) t = inn() for _ in range(t): n = inn() a, b = inp(), inp() print(solve(n, a, b)) main()
FUNC_DEF IF BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN STRING FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR RETURN STRING RETURN STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
test = int(input()) number = [] col_a = [] col_b = [] for i in range(test): number.append(int(input())) col_a.append(list(map(int, input().split()))) col_b.append(list(map(int, input().split()))) for i in range(test): if number[i] % 2 == 1: if col_a[i][int((number[i] - 1) / 2)] != col_b[i][int((number[i] - 1) / 2)]: print("No") else: pair_a = [] pair_b = [] mark = [(0) for k in range(int((number[i] - 1) / 2))] for j in range(int((number[i] - 1) / 2)): pair_a.append([col_a[i][j], col_a[i][number[i] - 1 - j]]) pair_b.append([col_b[i][j], col_b[i][number[i] - 1 - j]]) no = 0 for j in range(len(pair_b)): markble = 0 for k in range(len(pair_a)): if mark[k] == 0: if ( pair_a[k][0] == pair_b[j][0] and pair_a[k][1] == pair_b[j][1] ): mark[k] = 1 markble = 1 break elif ( pair_a[k][0] == pair_b[j][1] and pair_a[k][1] == pair_b[j][0] ): mark[k] = 1 markble = 1 break if markble == 0: no = 1 break if no == 1: print("No") else: print("Yes") else: pair_a = [] pair_b = [] mark = [(0) for k in range(int(number[i] / 2))] for j in range(int(number[i] / 2)): pair_a.append([col_a[i][j], col_a[i][number[i] - 1 - j]]) pair_b.append([col_b[i][j], col_b[i][number[i] - 1 - j]]) no = 0 for j in range(len(pair_b)): markble = 0 for k in range(len(pair_a)): if mark[k] == 0: if pair_a[k][0] == pair_b[j][0] and pair_a[k][1] == pair_b[j][1]: mark[k] = 1 markble = 1 break elif pair_a[k][0] == pair_b[j][1] and pair_a[k][1] == pair_b[j][0]: mark[k] = 1 markble = 1 break if markble == 0: no = 1 break if no == 1: print("No") else: print("Yes")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
for _ in range(int(input())): n = int(input()) k = n // 2 a = list(map(int, input().split())) b = list(map(int, input().split())) dicA = {} dicB = {} for i in range(k): p, q = a[i], a[-i - 1] if p > q: if (q, p) not in dicA: dicA[q, p] = 0 dicA[q, p] += 1 else: if (p, q) not in dicA: dicA[p, q] = 0 dicA[p, q] += 1 for i in range(k): p, q = b[i], b[-i - 1] if p > q: if (q, p) not in dicB: dicB[q, p] = 0 dicB[q, p] += 1 else: if (p, q) not in dicB: dicB[p, q] = 0 dicB[p, q] += 1 if n % 2 == 1: if a[k] != b[k]: print("No") else: res = "Yes" for p, q in dicA: if (p, q) not in dicB: res = "No" elif dicA[p, q] != dicB[p, q]: res = "No" print(res) else: res = "Yes" for p, q in dicA: if (p, q) not in dicB: res = "No" elif dicA[p, q] != dicB[p, q]: res = "No" print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR STRING IF VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR STRING IF VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) d = {} f = True if n % 2 != 0 and a[n // 2] != b[n // 2]: f = False for i in range(n // 2): mn = min(a[i], a[n - i - 1]) mx = max(a[i], a[n - i - 1]) p = mn, mx if p not in d: d[p] = 0 d[p] += 1 for i in range(n // 2): mn = min(b[i], b[n - i - 1]) mx = max(b[i], b[n - i - 1]) p = mn, mx if p not in d or d[p] <= 0: f = False break d[p] -= 1 if f: print("Yes") else: print("No")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
t = int(input()) for i in range(t): l = int(input()) if l <= 1: g = int(input()) f = int(input()) if g == f: print("Yes") else: print("No") else: a = [] b = [] a = list(map(int, input().split())) b = list(map(int, input().split())) m = [[min(a[0], a[-1]), max(a[0], a[-1])]] flag = 1 for i in range(1, l // 2): m.append([min(a[i], a[l - i - 1]), max(a[i], a[l - i - 1])]) for i in range(l // 2): test = [min(b[i], b[l - i - 1]), max(b[i], b[l - i - 1])] flag = 0 for j in range(len(m)): if test == m[j]: flag = 1 del m[j] break if flag == 0: break if flag: if l % 2: if a[l // 2] == b[l // 2]: print("Yes") else: print("No") else: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER IF VAR IF BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) if sorted(a) != sorted(b): print("NO") continue c = {} for i in range(n): if a[i] not in c: c[a[i]] = [a[n - i - 1]] else: c[a[i]] += [a[n - i - 1]] if a[n - i - 1] not in c: c[a[n - i - 1]] = [a[i]] else: c[a[n - i - 1]] += [a[i]] for i in range(n): if b[n - i - 1] not in c[b[i]] or b[i] not in c[b[n - i - 1]]: print("NO") break else: c[b[i]].remove(b[n - i - 1]) c[b[n - i - 1]].remove(b[i]) else: print("YES")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR LIST VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER LIST VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
def is_equal(a1, a2, n): if n % 2 == 1 and a1[n // 2] != a2[n // 2]: return False arr1 = create(a1) arr2 = create(a2) for u, v in zip(arr1, arr2): if u != v: return False return True def create(a): arr = [] n = len(a) for i in range(n // 2): if a[i] <= a[n - i - 1]: arr.append((a[i], a[n - i - 1])) else: arr.append((a[n - i - 1], a[i])) return sorted(arr) for _ in range(int(input())): n = int(input()) a1 = list(map(int, input().split())) a2 = list(map(int, input().split())) if is_equal(a1, a2, n) == True: print("Yes") else: print("No")
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
n = int(input()) inp = lambda: list(map(int, input().split(" "))) pairs = lambda x: sorted(zip(x, x[::-1])) for _ in range(n): inp() print(["no", "yes"][pairs(inp()) == pairs(inp())])
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 FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST STRING STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
def solve(n, a, b): if a[n // 2] != b[n // 2] and n % 2 != 0: return "NO" i = 0 seta = sorted([[a[i], a[n - 1 - i]] for i in range(n)]) setb = sorted([[b[i], b[n - i - 1]] for i in range(n)]) for i in range(n): if seta[i] != setb[i]: return "NO" return "YES" for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) print(solve(n, a, b))
FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
t = int(input()) for i in range(t): n = int(input()) fail = False a = [int(j) for j in input().split()] b = [int(j) for j in input().split()] if n % 2 != 0: if a[int((n - 1) / 2)] != b[int((n - 1) / 2)]: print("no") continue counter = {} for j in range(int(n / 2)): if (min(a[j], a[n - 1 - j]), max(a[j], a[n - 1 - j])) in counter: counter[min(a[j], a[n - 1 - j]), max(a[j], a[n - 1 - j])] += 1 else: counter[min(a[j], a[n - 1 - j]), max(a[j], a[n - 1 - j])] = 1 for j in range(int(n / 2)): if (min(b[j], b[n - 1 - j]), max(b[j], b[n - 1 - j])) in counter: counter[min(b[j], b[n - 1 - j]), max(b[j], b[n - 1 - j])] -= 1 if counter[min(b[j], b[n - 1 - j]), max(b[j], b[n - 1 - j])] == 0: del counter[min(b[j], b[n - 1 - j]), max(b[j], b[n - 1 - j])] else: fail = True break if fail == True: print("no") else: print("yes")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
t = int(input()) for o in range(t): n = int(input()) a = list(map(int, input().strip().split())) b = list(map(int, input().strip().split())) aPairs = [] bPairs = [] oddBreaker = False if n % 2: if a[n // 2] != b[n // 2]: oddBreaker = True for i in range(n // 2): v = a[i] w = a[-1 - i] x = b[i] y = b[-1 - i] minA = min(v, w) maxA = max(v, w) minB = min(x, y) maxB = max(x, y) aPairs.append([minA, maxA]) bPairs.append([minB, maxB]) aPairs.sort() bPairs.sort() if aPairs != bPairs or oddBreaker: print("No") else: print("Yes")
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
case = int(input()) for o in range(case): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) ok = True if n % 2 == 1: if a[n // 2] != b[n // 2]: ok = False pa = [] pb = [] for i in range(n // 2): x = a[i] y = a[n - i - 1] if x > y: x, y = y, x pa.append((x, y)) for i in range(n // 2): x = b[i] y = b[n - i - 1] if x > y: x, y = y, x pb.append((x, y)) pa.sort() pb.sort() if ok and pa == pb: print("YES") else: print("NO")
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) paira = [] for i in range((n + 1) // 2): paira.append(tuple(sorted([a[i], a[~i]]))) pairb = [] for i in range((n + 1) // 2): pairb.append(tuple(sorted([b[i], b[~i]]))) paira.sort() pairb.sort() if paira == pairb: print("Yes") else: print("No")
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
T = int(input()) X = [] for i in range(0, T): C = [] D = [] n = int(input()) A = [int(x) for x in input().split(" ")] B = [int(x) for x in input().split(" ")] for i in range(0, len(A) // 2): a = [A[i], A[len(A) - 1 - i]] b = [B[i], B[len(A) - 1 - i]] a.sort() b.sort() C.append(a) D.append(b) C.sort() D.sort() if n % 2 == 1 and A[n // 2] == B[n // 2] and C == D: X.append("Yes") elif n % 2 == 0 and C == D: X.append("Yes") else: X.append("No") for i in X: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = [] D = [] if n % 2 == 1: if A[n // 2] != B[n // 2]: print("No") continue for i in range(n // 2): C.append(sorted([A[i], A[-i - 1]])) D.append(sorted([B[i], B[-i - 1]])) if sorted(C) == sorted(D): print("Yes") else: print("No")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
from sys import stdin, stdout def swaps_again(n, a, b): if n % 2 == 1 and a[n // 2] != b[n // 2]: return "No" dica = {} for i in range(0, n // 2): al = a[i] ar = a[n - 1 - i] if al not in dica: dica[al] = {} if ar not in dica: dica[ar] = {} if ar not in dica[al]: dica[al][ar] = 0 if al not in dica[ar]: dica[ar][al] = 0 dica[al][ar] += 1 dica[ar][al] += 1 for i in range(0, n // 2): bl = b[i] br = b[n - 1 - i] if bl not in dica or br not in dica[bl] or dica[bl][br] == 0: return "No" dica[bl][br] -= 1 dica[br][bl] -= 1 return "Yes" t = int(stdin.readline()) for i in range(t): n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) b = list(map(int, stdin.readline().split())) stdout.write(swaps_again(n, a, b) + "\n")
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR DICT IF VAR VAR ASSIGN VAR VAR DICT IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN STRING VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN STRING 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
def result(a, b, n): set1 = {} set2 = {} for i in range(n // 2): if (max(a[i], a[n - 1 - i]), min(a[i], a[n - 1 - i])) in set1: set1[max(a[i], a[n - 1 - i]), min(a[i], a[n - 1 - i])] += 1 else: set1[max(a[i], a[n - 1 - i]), min(a[i], a[n - 1 - i])] = 1 if (max(b[i], b[n - 1 - i]), min(b[i], b[n - 1 - i])) in set2: set2[max(b[i], b[n - 1 - i]), min(b[i], b[n - 1 - i])] += 1 else: set2[max(b[i], b[n - 1 - i]), min(b[i], b[n - 1 - i])] = 1 if n % 2 == 1 and a[n // 2] != b[n // 2]: print("No") elif set1 == set2: print("yes") else: print("No") m = int(input()) for _ in range(m): n = int(input()) a = list(map(lambda x: int(x), input().split(" "))) b = list(map(lambda x: int(x), input().split(" "))) result(a, b, n)
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
from itertools import count def get_repr(items): size = len(items) result = [] for i in count(): if size <= 2 * i: break first, second = items[i], items[-(i + 1)] if first > second: first, second = second, first result.append((first, second)) result.sort() if size % 2: result.append(items[size // 2]) return result test_count = int(input()) for _ in range(test_count): size = int(input()) first = [int(x) for x in input().split()][:size] second = [int(x) for x in input().split()][:size] print("yes" if get_repr(first) == get_repr(second) else "no")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR 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 VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
t = int(input()) def solve(): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) if n % 2 == 1 and a[n // 2] != b[n // 2]: return "no" sa = {} i = 0 j = n - 1 while i < j: u = a[i] v = a[j] if u > v: u, v = v, u if (u, v) in sa: sa[u, v] += 1 else: sa[u, v] = 1 i += 1 j -= 1 sb = {} i = 0 j = n - 1 while i < j: u = b[i] v = b[j] if u > v: u, v = v, u if (u, v) in sb: sb[u, v] += 1 else: sb[u, v] = 1 i += 1 j -= 1 if sa == sb: return "yes" else: return "no" for _ in range(t): print(solve())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN STRING ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) pair = dict() possible = 1 if n % 2 == 1: if a[n // 2] != b[n // 2]: possible = 0 for i in range(n // 2): M, m = max(a[i], a[n - i - 1]), min(a[i], a[n - i - 1]) try: pair[str(m) + " " + str(M)] += 1 except: pair[str(m) + " " + str(M)] = 1 for i in range(n // 2): M, m = max(b[i], b[n - i - 1]), min(b[i], b[n - i - 1]) x = str(m) + " " + str(M) if x not in pair: possible = 0 break elif pair[x] >= 1: pair[x] -= 1 else: possible = 0 break if possible: print("yes") else: print("No")
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
from sys import stdin input = stdin.buffer.readline for _ in range(int(input())): n = int(input()) (*a,) = map(int, input().split()) (*b,) = map(int, input().split()) if n & 1 and a[n // 2] != b[n // 2]: print("no") continue a = sorted([sorted([a[i], a[n - i - 1]]) for i in range(n // 2)]) b = sorted([sorted([b[i], b[n - i - 1]]) for i in range(n // 2)]) if a == b: print("yes") else: print("no")
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
t = int(input()) for i in range(t): n = int(input()) lis1 = [int(x) for x in input().split()] lis2 = [int(x) for x in input().split()] if n % 2 == 1: if lis1[n // 2] != lis2[n // 2]: print("No") continue dict1 = dict() for i in range(n // 2): str1 = str(lis1[i]) + "," + str(lis1[n - i - 1]) str2 = str(lis1[n - i - 1]) + "," + str(lis1[i]) if str1 in dict1.keys() or str2 in dict1.keys(): if str1 in dict1.keys(): dict1[str1] += 1 else: dict1[str2] += 1 else: dict1[str1] = 1 badge = 0 for i in range(n // 2): str1 = str(lis2[i]) + "," + str(lis2[n - i - 1]) if str1 in dict1.keys(): dict1[str1] -= 1 if dict1[str1] == 0: del dict1[str1] else: str2 = str(lis2[n - i - 1]) + "," + str(lis2[i]) if str2 in dict1.keys(): dict1[str2] -= 1 if dict1[str2] == 0: del dict1[str2] else: badge = 1 break if badge == 0: print("Yes") else: print("No")
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER STRING FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER STRING FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
t = int(input()) for _ in range(t): q, f = lambda: list(map(int, input().split())), lambda x: sorted( zip(x, reversed(x)) ) q(), print(["no", "yes"][f(q()) == f(q())])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST STRING STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
def solve(): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) if n % 2 == 1: if a[n // 2] != b[n // 2]: print("No") return u, v = list(), list() def add(s, p, q): if p > q: p, q = q, p s.append((p, q)) for i in range(n // 2): add(u, a[i], a[n - i - 1]) add(v, b[i], b[n - i - 1]) if sorted(u) == sorted(v): print("Yes") else: print("No") t = int(input()) for _ in range(t): solve()
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
import sys def ii(): return sys.stdin.readline().strip() def idata(): return [int(x) for x in ii().split()] def solve_of_problem(): n = int(ii()) data = idata() data1 = idata() if n == 1: if data1 == data: print("Yes") else: print("No") return if sorted(data) != sorted(data1): print("No") return if n % 2 == 1: if data[n // 2] != data1[n // 2]: print("No") return data = data[: n // 2] + data[n // 2 + 1 :] data1 = data1[: n // 2] + data1[n // 2 + 1 :] n -= 1 d = dict() for i in range(n // 2): d[str(data[i]) + " " + str(data[n - i - 1])] = 0 d[str(data[n - 1 - i]) + " " + str(data[i])] = 0 d[str(data1[i]) + " " + str(data1[n - i - 1])] = 0 d[str(data1[n - 1 - i]) + " " + str(data1[i])] = 0 for i in range(n // 2): d[str(data[i]) + " " + str(data[n - i - 1])] += 1 d[str(data[n - 1 - i]) + " " + str(data[i])] += 1 d[str(data1[i]) + " " + str(data1[n - i - 1])] -= 1 d[str(data1[n - 1 - i]) + " " + str(data1[i])] -= 1 if min(d.values()) < 0: print("No") return print("Yes") return for ______ in range(int(ii())): solve_of_problem()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR STRING FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
cases = int(input()) answers = [] * 0 for problems in range(cases): n = int(input()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] s1 = sorted(a) s2 = sorted(b) pairs = [] * 0 for i in range(n // 2): pairs.append(sorted([a[i], a[-1 - i]])) if s1 != s2: answers.append("No") elif n % 2 == 1 and a[(n - 1) // 2] != b[(n - 1) // 2]: answers.append("No") else: bOk = True for i in range(n // 2): if sorted([b[i], b[-1 - i]]) in pairs: pairs.remove(sorted([b[i], b[-1 - i]])) else: bOk = False answers.append("Yes") if bOk else answers.append("No") for answer in answers: print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR LIST VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER EXPR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
from sys import stdin, stdout t = int(stdin.readline().strip()) for _ in range(t): n = int(stdin.readline().strip()) a = list(map(int, stdin.readline().split())) b = list(map(int, stdin.readline().split())) pairs = {} i, j = 0, n - 1 while i < (n - 1) / 2: pairs[a[i], a[j]] = pairs.get((a[i], a[j]), 0) + 1 i, j = i + 1, j - 1 ans = "yes\n" i, j = 0, n - 1 while i < (n - 1) / 2: if (b[i], b[j]) in pairs and pairs[b[i], b[j]] > 0: pairs[b[i], b[j]] -= 1 elif (b[j], b[i]) in pairs and pairs[b[j], b[i]] > 0: pairs[b[j], b[i]] -= 1 else: ans = "no\n" break i, j = i + 1, j - 1 if n % 2 and a[(n - 1) // 2] != b[(n - 1) // 2]: ans = "no\n" stdout.write(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
import sys t = int(input()) while t != 0: t -= 1 n = int(input()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] aa = [] bb = [] k = int(n / 2) if n & 1 and a[k] != b[k]: print("NO") continue for i in range(k): aa.append((a[i], a[n - 1 - i])) if a[i] > a[n - 1 - i]: aa[len(aa) - 1] = a[n - 1 - i], a[i] for i in range(k): bb.append((b[i], b[n - 1 - i])) if b[i] > b[n - 1 - i]: bb[len(bb) - 1] = b[n - 1 - i], b[i] ans = False aa.sort() bb.sort() for i in range(len(aa)): if aa[i] != bb[i]: ans = True if ans == False: print("YES") else: print("NO")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
for _ in range(int(input())): n = int(input()) a = [int(c) for c in input().split()] b = [int(c) for c in input().split()] ad = sorted(a) bd = sorted(b) if ad != bd: print("No") continue if n % 2 == 1: if a[n // 2] != b[n // 2]: print("No") continue dic = {} for i in range(n // 2): c = a[i] d = a[n - 1 - i] pair = min(c, d), max(c, d) dic[pair] = dic.get(pair, 0) + 1 for i in range(n // 2): c = b[i] d = b[n - 1 - i] pair = min(c, d), max(c, d) if dic.get(pair, -1) <= 0: print("No") break else: dic[pair] -= 1 else: print("Yes")
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
from sys import gettrace, stdin if gettrace(): inputi = input else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def main(): def solve(): n = int(inputi()) aa = [int(a) for a in inputi().split()] bb = [int(a) for a in inputi().split()] if n % 2 == 1 and aa[n // 2] != bb[n // 2]: print("No") return apairs = {} bpairs = {} for i in range(n // 2): ap = frozenset((aa[i], aa[-i - 1])) apairs[ap] = apairs.get(ap, 0) + 1 bp = frozenset((bb[i], bb[-i - 1])) bpairs[bp] = bpairs.get(bp, 0) + 1 if apairs == bpairs: print("Yes") else: print("No") t = int(inputi()) for _ in range(t): solve() main()
IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
q, f = lambda: list(map(int, input().split())), lambda x: sorted(zip(x, x[::-1])) for _ in range(q()[0]): q(), print("yneos"[f(q()) != f(q()) :: 2])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer $n$ and two arrays $a$ and $b$, of size $n$. If after some (possibly zero) operations described below, array $a$ can be transformed into array $b$, the input is said to be valid. Otherwise, it is invalid. An operation on array $a$ is: select an integer $k$ $(1 \le k \le \lfloor\frac{n}{2}\rfloor)$ swap the prefix of length $k$ with the suffix of length $k$ For example, if array $a$ initially is $\{1, 2, 3, 4, 5, 6\}$, after performing an operation with $k = 2$, it is transformed into $\{5, 6, 3, 4, 1, 2\}$. Given the set of test cases, help them determine if each one is valid or invalid. -----Input----- The first line contains one integer $t$ $(1 \le t \le 500)$ — the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer $n$ $(1 \le n \le 500)$ — the size of the arrays. The second line of each test case contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 10^9)$ — elements of array $a$. The third line of each test case contains $n$ integers $b_1$, $b_2$, ..., $b_n$ $(1 \le b_i \le 10^9)$ — elements of array $b$. -----Output----- For each test case, print "Yes" if the given input is valid. Otherwise print "No". You may print the answer in any case. -----Example----- Input 5 2 1 2 2 1 3 1 2 3 1 2 3 3 1 2 4 1 3 4 4 1 2 3 2 3 1 2 2 3 1 2 3 1 3 2 Output yes yes No yes No -----Note----- For the first test case, we can swap prefix $a[1:1]$ with suffix $a[2:2]$ to get $a=[2, 1]$. For the second test case, $a$ is already equal to $b$. For the third test case, it is impossible since we cannot obtain $3$ in $a$. For the fourth test case, we can first swap prefix $a[1:1]$ with suffix $a[4:4]$ to obtain $a=[2, 2, 3, 1]$. Now we can swap prefix $a[1:2]$ with suffix $a[3:4]$ to obtain $a=[3, 1, 2, 2]$. For the fifth test case, it is impossible to convert $a$ to $b$.
from sys import stdin tt = int(stdin.readline()) for loop in range(tt): n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) b = list(map(int, stdin.readline().split())) if n % 2 == 1 and a[n // 2] != b[n // 2]: print("no") continue dic = {} for i in range(n // 2): l = a[i] r = a[n - 1 - i] if l > r: l, r = r, l if (l, r) not in dic: dic[l, r] = 0 dic[l, r] += 1 ans = "yes" for i in range(n // 2): l = b[i] r = b[n - 1 - i] if l > r: l, r = r, l if (l, r) not in dic or dic[l, r] <= 0: ans = "no" break dic[l, r] -= 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Dima has two sequences of points with integer coordinates: sequence (a1, 1), (a2, 2), ..., (an, n) and sequence (b1, 1), (b2, 2), ..., (bn, n). Now Dima wants to count the number of distinct sequences of points of length 2·n that can be assembled from these sequences, such that the x-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence. Dima considers two assembled sequences (p1, q1), (p2, q2), ..., (p2·n, q2·n) and (x1, y1), (x2, y2), ..., (x2·n, y2·n) distinct, if there is such i (1 ≤ i ≤ 2·n), that (pi, qi) ≠ (xi, yi). As the answer can be rather large, print the remainder from dividing the answer by number m. Input The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 109). The numbers in the lines are separated by spaces. The last line contains integer m (2 ≤ m ≤ 109 + 7). Output In the single line print the remainder after dividing the answer to the problem by number m. Examples Input 1 1 2 7 Output 1 Input 2 1 2 2 3 11 Output 2 Note In the first sample you can get only one sequence: (1, 1), (2, 1). In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2); (1, 1), (2, 1), (2, 2), (3, 2). Thus, the answer is 2.
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, stdin.readline().split())) for _ in range(1): n = nmbr() a = lst() b = lst() M = nmbr() tot = {} ans = 1 mp = {} for v, v1 in zip(a, b): tot[v] = tot.get(v, 0) + 1 tot[v1] = tot.get(v1, 0) + 1 if v == v1: mp[v] = mp.get(v, 0) + 1 for k, same_category in tot.items(): fact = 1 for v in range(1, same_category + 1): new = v while new & 1 == 0 and mp.get(k, 0) > 0: new >>= 1 mp[k] -= 1 fact = fact * new % M ans = ans * fact % M print(ans)
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 NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider a grid A with dimensions N \times M, such that all elements of the grid are distinct. A remote controlled car is present at cell (1,1). Chef and Chefina will alternatively move the car with Chef starting first. If it is Chef's turn, he will move the car to either right or down by 1 cell. He moves the car to the cell having larger element. If it is Chefina's turn, she will move the car to either right or down by 1 cell. She moves the car to the cell having smaller element. The car stops once it reaches the cell (N, M). Let us denote the largest element visited by the car by A_{max} and the smallest element as A_{min}. Then, the score of the grid is defined as A_{max} - A_{min}. Charlie has an array B containing N \cdot M distinct elements. He wants to arrange the elements as a grid of size N\times M such that the score of the grid is minimised. Help Charlie to construct any such grid. Note: Please use fast I/O for input and pypy for python submissions. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M — dimensions of the grid A. - The next line contains N \cdot M distinct elements denoting the array B. ------ Output Format ------ For each test case, output N lines each containing M spaced integers denoting the grid A. If there are multiple possible answers, print any of them. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $2 ≤ N, M ≤ 1000$ $1 ≤ B_{i} ≤ 10^{9}$ - All elements of the array $B$ are distinct. - Sum of $N \cdot M$ over all test cases do not exceed $10^{6}$. ----- Sample Input 1 ------ 2 2 2 5 10 20 40 3 2 2 6 4 1 9 20 ----- Sample Output 1 ------ 10 5 20 40 2 4 1 6 20 9 ----- explanation 1 ------ Test case $1$: The car starts from $(1, 1)$. Let us consider the moves based on the grid $[[10, 5], [20, 40]]$. - In Chef's turn, he will move the car to $(2, 1)$ since $A_{(2, 1)} > A_{(1, 2)}$. - In Chefina's turn, she will move the car to $(2, 2)$ as it is the only available choice. Thus, the car covers the elements $[10, 20, 40]$. The largest element in the path is $40$ while the smallest element is $10$. Thus, the score is $40-10 = 30$. It can be shown that this is the minimum possible score that can be achieved in any configuration of grid. Test case $2$: The car starts from $(1, 1)$. Let us consider the moves based on the grid $[[2, 4], [1, 6], [20, 9]]$. - In Chef's turn, he will move the car to $(1, 2)$ since $A_{(1, 2)} > A_{(2, 1)}$. - In Chefina's turn, she will move the car to $(2, 2)$ as it is the only available choice. - In Chef's turn, he will move the car to $(3, 2)$ as it is the only available choice. Thus, the car covers the elements $[2, 4, 6, 9]$. The largest element in the path is $9$ while the smallest element is $2$. Thus, the score is $9 - 2 = 7$. It can be shown that this is the minimum possible score that can be achieved in any configuration of grid.
import sys t = int(input()) while t: n, m = map(int, input().split()) h = n p = m n = min(h, p) m = max(h, p) b = [int(x) for x in input().split()] b.sort() x = min(n, m) - 1 ch = (x + 1) // 2 che = x // 2 ans = sys.maxsize pos = -1 for i in range(ch, n * m - che - n - m + 2): j = i + n + m - 2 if ans > b[j] - b[i]: ans = b[j] - b[i] pos = i mat = [[(0) for _ in range(m)] for _ in range(n)] if n <= m: k = pos l = 0 for i in range(n): mat[i][0] = b[k] k += 1 for i in range(1, m): mat[n - 1][i] = b[k] k += 1 for i in range(0, n - 1, 2): mat[i][1] = b[l] l += 1 for i in range(1, n - 1, 2): mat[i][1] = b[k] k += 1 for i in range(0, n - 1): for j in range(2, m): if l < pos: mat[i][j] = b[l] l += 1 else: mat[i][j] = b[k] k += 1 else: mat = [[(0) for _ in range(n)] for _ in range(m)] k = pos l = 0 for i in range(m): mat[i][0] = b[k] k += 1 for i in range(1, n): mat[m - 1][i] = b[k] k += 1 for i in range(0, m - 1, 2): mat[i][1] = b[l] l += 1 for i in range(1, m - 1, 2): mat[i][1] = b[k] k += 1 for i in range(0, m - 1): for j in range(2, n): if l < pos: mat[i][j] = b[l] l += 1 else: mat[i][j] = b[k] k += 1 if h <= p: for i in range(n): for j in range(m): print(mat[i][j], end=" ") print() else: for i in range(m): for j in range(n): print(mat[j][i], end=" ") print() t -= 1
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
Consider a grid A with dimensions N \times M, such that all elements of the grid are distinct. A remote controlled car is present at cell (1,1). Chef and Chefina will alternatively move the car with Chef starting first. If it is Chef's turn, he will move the car to either right or down by 1 cell. He moves the car to the cell having larger element. If it is Chefina's turn, she will move the car to either right or down by 1 cell. She moves the car to the cell having smaller element. The car stops once it reaches the cell (N, M). Let us denote the largest element visited by the car by A_{max} and the smallest element as A_{min}. Then, the score of the grid is defined as A_{max} - A_{min}. Charlie has an array B containing N \cdot M distinct elements. He wants to arrange the elements as a grid of size N\times M such that the score of the grid is minimised. Help Charlie to construct any such grid. Note: Please use fast I/O for input and pypy for python submissions. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and M — dimensions of the grid A. - The next line contains N \cdot M distinct elements denoting the array B. ------ Output Format ------ For each test case, output N lines each containing M spaced integers denoting the grid A. If there are multiple possible answers, print any of them. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $2 ≤ N, M ≤ 1000$ $1 ≤ B_{i} ≤ 10^{9}$ - All elements of the array $B$ are distinct. - Sum of $N \cdot M$ over all test cases do not exceed $10^{6}$. ----- Sample Input 1 ------ 2 2 2 5 10 20 40 3 2 2 6 4 1 9 20 ----- Sample Output 1 ------ 10 5 20 40 2 4 1 6 20 9 ----- explanation 1 ------ Test case $1$: The car starts from $(1, 1)$. Let us consider the moves based on the grid $[[10, 5], [20, 40]]$. - In Chef's turn, he will move the car to $(2, 1)$ since $A_{(2, 1)} > A_{(1, 2)}$. - In Chefina's turn, she will move the car to $(2, 2)$ as it is the only available choice. Thus, the car covers the elements $[10, 20, 40]$. The largest element in the path is $40$ while the smallest element is $10$. Thus, the score is $40-10 = 30$. It can be shown that this is the minimum possible score that can be achieved in any configuration of grid. Test case $2$: The car starts from $(1, 1)$. Let us consider the moves based on the grid $[[2, 4], [1, 6], [20, 9]]$. - In Chef's turn, he will move the car to $(1, 2)$ since $A_{(1, 2)} > A_{(2, 1)}$. - In Chefina's turn, she will move the car to $(2, 2)$ as it is the only available choice. - In Chef's turn, he will move the car to $(3, 2)$ as it is the only available choice. Thus, the car covers the elements $[2, 4, 6, 9]$. The largest element in the path is $9$ while the smallest element is $2$. Thus, the score is $9 - 2 = 7$. It can be shown that this is the minimum possible score that can be achieved in any configuration of grid.
for i in range(int(input())): n, m = map(int, input().split()) lst = [int(x) for x in input().split()] lst.sort() h, p = n, m n = min(h, p) m = max(h, p) x = min(n, m) - 1 temp = (x + 1) // 2 t2 = x // 2 maxv = 999999999999999 start = -1 for i in range(temp, n * m - t2 - n - m + 2): j = i + n + m - 2 if maxv > lst[j] - lst[i]: maxv = lst[j] - lst[i] start = i dp = [[(0) for j in range(m)] for i in range(n)] if n <= m: k = start l = 0 for i in range(n): dp[i][0] = lst[k] k += 1 for i in range(1, m): dp[n - 1][i] = lst[k] k += 1 for i in range(0, n - 1, 2): dp[i][1] = lst[l] l += 1 for i in range(1, n - 1, 2): dp[i][1] = lst[k] k += 1 for i in range(0, n - 1): for j in range(2, m): if l < start: dp[i][j] = lst[l] l += 1 else: dp[i][j] = lst[k] k += 1 if h <= p: for i in range(n): for j in range(m): print(dp[i][j], end=" ") print() else: for i in range(m): for j in range(n): print(dp[j][i], end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
import sys def get_arr(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) t = int(sys.stdin.readline()) for _ in range(t): n = int(sys.stdin.readline()) a = get_arr() s = input() b = [] for i in range(n): b.append(a[i]) b.sort() fl = -1 for i in range(n): if a[i] != b[i]: fl = i break fl2 = -1 for i in range(n - 1, -1, -1): if a[i] != b[i]: fl2 = i break if fl == -1: print(0) continue elif s[0] == "N" and s[n - 1] == "S" or s[0] == "S" and s[n - 1] == "N": print(1) else: n2 = 0 s2 = 0 for i in range(n): if s[i] == "N": n2 = 1 if s[i] == "S": s2 = 1 if n2 == 0 or s2 == 0: print(-1) else: if s[fl] == "N": val = 0 for i in range(fl2, n): if s[i] == "S": print(1) val = 1 break if val == 1: continue else: val = 0 for i in range(fl2, n): if s[i] == "N": print(1) val = 1 break if val == 1: continue if s[fl2] == "N": val = 0 for i in range(fl, -1, -1): if s[i] == "S": print(1) val = 1 break if val == 1: continue else: val = 0 for i in range(fl, -1, -1): if s[i] == "N": print(1) val = 1 break if val == 1: continue print(2) continue
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split(" "))) s = input() if sorted(arr) == arr: print(0) elif s.count("N") == n or s.count("N") == 0: print(-1) else: a = sorted(arr) b = arr start = 2147483647 last = -2147483647 - 1 for i in range(n): if a[i] != b[i]: start = min([i, start]) last = max([last, i]) f1 = False f2 = False for i in range(start): if s[i] != s[last]: f1 = True for i in range(last, n): if s[i] != s[start]: f2 = True if s[start] != s[last] or f1 or f2: print(1) else: print(2)
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 STRING ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def isSorted(nums): for i in range(1, len(nums)): if nums[i - 1] > nums[i]: return False return True def solve(n, s, nums): if isSorted(nums): return 0 if "N" not in s or "S" not in s: return -1 temp = nums.copy() i = s.find("N") j = s.rfind("S") if i < j: temp[i : j + 1] = sorted(temp[i : j + 1]) if isSorted(temp): return 1 temp = nums.copy() i = s.find("S") j = s.rfind("N") if i < j: temp[i : j + 1] = sorted(temp[i : j + 1]) if isSorted(temp): return 1 return 2 for t in range(int(input())): n = int(input()) li = list(map(int, input().split())) s = input() print(solve(n, s, li))
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR RETURN NUMBER IF STRING VAR STRING VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) nums = list(map(int, input().split())) pole = input() o = sorted(nums) pre = [] for i, item in enumerate(pole): k = 0 if item == "N": k = 1 if i: k += pre[i - 1] pre.append(k) index = [] for i, item in enumerate(nums): if item != o[i]: index.append(i) if nums == o: print(0) continue elif pole == "N" * n or pole == "S" * n: print(-1) continue if pole[index[0]] != pole[index[-1]]: print(1) elif pole[index[0]] == "N": if ( pre[index[0]] < index[0] + 1 or pre[-1] - pre[index[-1]] < len(o) - index[-1] - 1 ): print(1) else: print(2) elif pre[index[0]] > 0 or pre[-1] - pre[index[-1]] > 0: print(1) else: print(2)
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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP STRING VAR VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER STRING IF VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
a = int(input()) for i in range(a): N = int(input()) A = list(map(int, input().split())) S = input() if sorted(A) == A: print(0) elif S[0] != S[N - 1]: print(1) elif S.count(S[0]) == N: print(-1) elif S[0] == "N": k = S.index("S") r = S.rfind("S") C = A[:k] D = A[r + 1 :] P = sorted(C) Q = sorted(D) if C != P and D != Q: print(2) elif C == P and D != Q: if max(C) <= min(A[k:]): print(1) else: print(2) elif D == Q and C != P: if max(A[: r + 1]) <= min(D): print(1) else: print(2) elif max(C) <= min(A[k:]) or max(A[: r + 1]) <= min(D): print(1) else: print(2) else: k = S.index("N") r = S.rfind("N") C = A[:k] D = A[r + 1 :] P = sorted(C) Q = sorted(D) if C != P and D != Q: print(2) elif C == P and D != Q: if max(C) <= min(A[k:]): print(1) else: print(2) elif D == Q and C != P: if max(A[: r + 1]) <= min(D): print(1) else: print(2) elif max(C) <= min(A[k:]) or max(A[: r + 1]) <= min(D): print(1) else: print(2)
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 FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def solve(n, arr, p): sorted_arr = sorted(arr) if arr == sorted_arr: return 0 first_s = -1 last_s = -1 first_n = -1 last_n = -1 for i in range(n): if p[i] == "S": if first_s == -1: first_s = i last_s = i else: if first_n == -1: first_n = i last_n = i for i in range(n): if arr[i] == sorted_arr[i]: continue if p[-1] != p[i]: return 1 if p[i] == "N": if first_s == -1: return -1 if first_s < i: return 1 return ( 2 if sorted(arr[: last_s + 1]) + arr[last_s + 1 :] != sorted_arr else 1 ) else: if first_n == -1: return -1 if first_n < i: return 1 return ( 2 if sorted(arr[: last_n + 1]) + arr[last_n + 1 :] != sorted_arr else 1 ) return 0 for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) p = input() print(solve(n, arr, p))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) def solve(ar, pol): ref = sorted(ar) if ar == ref: return 0 if len(set(pol)) == 1: return -1 n = len(ar) for i in range(n): if ar[i] != ref[i]: break for j in range(n - 1, -1, -1): if ar[j] != ref[j]: break for k in range(i + 1): for l in range(j, n): if pol[k] != pol[l] and k < l: return 1 return 2 for _ in range(t): n = int(input()) x = list(map(int, input().split())) pol = list(input()) print(solve(x, pol))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF 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 FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
import sys def check(A, first, last): A[first : last + 1] = sorted(A[first : last + 1]) if A == sorted(A): return 1 return 0 def solve(A, S): arr = sorted(A) if arr == A: return 0 n_count = S.count("N") s_count = S.count("S") if n_count == 0 or s_count == 0: return -1 d = {"leftN": 0, "leftS": 0, "rightN": 0, "rightS": 0} l, r = 10**6, 0 n = len(S) temparr = arr s = S a = A for i in range(n): if temparr[i] != a[i]: l = min(l, i) r = max(r, i) for i in range(l + 1): if s[i] == "N": d["leftN"] += 1 if s[i] == "S": d["leftS"] += 1 for i in range(r, n): if s[i] == "N": d["rightN"] += 1 if s[i] == "S": d["rightS"] += 1 if d["leftS"] > 0 and d["rightN"] > 0 or d["rightS"] > 0 and d["leftN"] > 0: return 1 else: return 2 t = int(input()) for i in range(int(t)): n = int(input()) A = list(map(int, input().split())) S = input() print(solve(A, S))
IMPORT FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR STRING NUMBER IF VAR VAR STRING VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR STRING NUMBER IF VAR VAR STRING VAR STRING NUMBER IF VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL 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 FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def disturbance(l, p, n): st, en = -1, 0 for i in range(n): if l[i] != p[i]: if st == -1: st = i else: en = i return st, en for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) s = str(input()) p = [] for i in l: p.append(i) p.sort() n_c = s.count("N") if p != l: if n_c == n or n_c == 0: print(-1) else: start, end = disturbance(l, p, n) if s[start] != s[end]: print(1) else: ans2 = 10000 for i in range(start, end + 1): if s[i] != s[start]: ans2 = 2 break nl, sl = 0, 0 for i in range(start + 1): if s[i] == "N": nl = 1 elif s[i] == "S": sl = 1 nr, sr = 0, 0 for j in range(end, n): if s[j] == "N": nr = 1 elif s[j] == "S": sr = 1 ans3 = 10000 if nl & sr == 1 or sl & nr == 1: ans3 = 1 print(min(ans2, ans3)) else: print(0)
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for i in range(int(input())): n = int(input()) A = [int(k) for k in input().split()] s = input() S = 0 N = 0 for k in s: if k == "N": N += 1 else: S += 1 if S == n and A != sorted(A) or N == n and A != sorted(A): print(-1) elif A == sorted(A): print(0) elif s[0] != s[-1]: print(1) else: z = 0 d = sorted(A) S = A.index(d[-1]) P = A.index(d[0]) for k in range(n): if A[k] != d[k]: z += 1 S = min(k, S) P = max(k, P) if k >= 1: d.append([s[k - 1], s[k]]) S1 = 0 P1 = 0 for k in range(n): if k < S: if s[k] != s[P]: S1 = 1 if k >= P: if s[k] != s[S]: P1 = 1 if S1 == 1 or P1 == 1 or s[S] != s[P]: print(1) else: print(2)
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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) s = input() b = a.copy() b.sort() if a == b: print(0) elif len(set(s)) == 1: print(-1) else: b1 = 0 for j in range(0, n - 1): if s[-1] == "S": if s[j] == "N": b1 = j break elif s[-1] == "N": if s[j] == "S": b1 = j break l1 = a[b1:] l1.sort() l2 = a[:b1] l3 = l2 + l1 b2 = 0 for j in range(n - 1, 0, -1): if s[0] == "S": if s[j] == "N": b2 = j break elif s[0] == "N": if s[j] == "S": b2 = j break l4 = a[: b2 + 1] l4.sort() l5 = a[b2 + 1 :] l6 = l4 + l5 ans1 = 0 ans2 = 0 if l3 == b: ans1 = 1 if l3 != b: ans1 = 2 if l6 == b: ans2 = 1 if l6 != b: ans2 = 2 print(min(ans1, ans2))
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER STRING IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER STRING IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER STRING IF VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER STRING IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
T = int(input()) for _ in range(T): n = int(input()) lo = list(map(int, input().split())) s = sorted(lo) c = list(input()) r = -1 if lo == s: r = 0 if "N" in c and "S" in c: if lo == s: r = 0 elif c[0] != c[-1]: r = 1 else: front = n rear = 0 for i in range(n): if lo[i] != s[i]: front = min(i, front) rear = max(i, rear) f = 0 for i in range(front + 1): if c[i] != c[-1]: f = 1 break for i in range(rear, n): if c[i] != c[-1]: f = 1 break if f: r = 1 else: r = 2 print(r)
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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF STRING VAR STRING VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) s = input() if sorted(arr) == arr: print(0) elif "N" not in s or "S" not in s: print(-1) else: begin = -1 end = -1 temp = sorted(arr) for i in range(n): if temp[i] != arr[i]: begin = i break for i in range(n - 1, -1, -1): if temp[i] != arr[i]: end = i break check = s[: begin + 1] + s[end:] if "N" in check and "S" in check: print(1) else: print(2)
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 IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) s = list(input()) if all(a[i - 1] <= a[i] for i in range(1, n)): print(0) continue n_ct = s.count("N") if n_ct == n or n_ct == 0: print(-1) continue left, right = n + 1, -1 b = sorted(a) for i, (av, bv) in enumerate(zip(a, b)): if av != bv: left = i break for i, (av, bv) in enumerate(zip(a[::-1], b[::-1])): if av != bv: right = n - 1 - i break lf, rf = False, False for i in range(left): if s[i] != s[right]: lf = True break for i in range(right, n): if s[i] != s[left]: rf = True break if s[left] != s[right] or lf or rf: print(1) continue print(2)
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 FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def check(A, first, last): A[first : last + 1] = sorted(A[first : last + 1]) if A == sorted(A): return 1 return 0 def solve(A, S, N): if A == sorted(A): print(0) return countN = 0 for ch in S: if ch == "N": countN += 1 if countN == 0 or countN == N: print(-1) return firstN, lastN, firstS, lastS = -1, -1, -1, -1 for i in range(N): if S[i] == "N": if firstN == -1: firstN = i lastN = i else: if firstS == -1: firstS = i lastS = i ans = 2 if check(A[:], firstN, lastS) == 1 or check(A[:], firstS, lastN) == 1: ans = 1 print(ans) T = int(input()) for _ in range(T): N = int(input()) A = list(map(int, input().split())) S = input() solve(A, S, N)
FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL 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 FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
from sys import stdin input = stdin.readline for _ in range(int(input())): n = int(input()) li = list(map(int, input().split())) s = input().strip() res = sorted(li[:]) if res == li: print(0) elif s.count("S") == 0 or s.count("N") == 0: print(-1) elif s[0] + s[-1] == "NS" or s[0] + s[-1] == "SN": print(1) else: s1, s2, n1, n2 = -1, -1, -1, -1 for i in range(n): if s[i] == "N": if n1 == -1: n1 = i else: n2 = i elif s1 == -1: s1 = i else: s2 = i if s2 == -1: s2 = s1 if n2 == -1: n2 = n1 if ( li[:n1] + sorted(li[n1 : s2 + 1]) + li[s2 + 1 :] == res or li[:s1] + sorted(li[s1 : n2 + 1]) + li[n2 + 1 :] == res ): print(1) else: print(2)
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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER STRING BIN_OP VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) while t: t -= 1 n = int(input()) a = list(map(int, input().split())) temparr = sorted(a) s = input() if temparr == a: print(0) continue if s.count("S") == n or s.count("N") == n: print(-1) continue d = {"leftN": 0, "leftS": 0, "rightN": 0, "rightS": 0} l, r = 10**6, 0 for i in range(n): if temparr[i] != a[i]: l = min(l, i) r = max(r, i) for i in range(l + 1): if s[i] == "N": d["leftN"] += 1 if s[i] == "S": d["leftS"] += 1 for i in range(r, n): if s[i] == "N": d["rightN"] += 1 if s[i] == "S": d["rightS"] += 1 if d["leftS"] > 0 and d["rightN"] > 0 or d["rightS"] > 0 and d["leftN"] > 0: print(1) else: print(2)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR STRING NUMBER IF VAR VAR STRING VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR STRING NUMBER IF VAR VAR STRING VAR STRING NUMBER IF VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
n_test_cases = int(input()) results = [] for _ in range(n_test_cases): n_ints = int(input()) A = [int(x) for x in input().split()] polarity = input() A_sorted = sorted(A) if len(A) == 1: results.append(0) continue if A == A_sorted: results.append(0) continue if "S" not in polarity or "N" not in polarity: results.append(-1) continue is_ordered = [(A_sorted[i] == A[i]) for i in range(len(A))] first_false = is_ordered.index(False) last_false = len(is_ordered) - 1 - is_ordered[::-1].index(False) if ( "N" in polarity[: first_false + 1] and "S" in polarity[last_false:] or "S" in polarity[: first_false + 1] and "N" in polarity[last_false:] ): results.append(1) else: results.append(2) for result in results: print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER NUMBER IF STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = input() c = sorted(a) d = 0 e = f = 0 if a != c: if "N" not in b or "S" not in b: print(-1) continue if b[0] != b[-1]: print(1) continue for i in range(n): if a[i] != c[i]: e = i break for i in range(n - 1, 0, -1): if a[i] != c[i]: f = i break if "N" in b[: e + 1] and "S" in b[f:] or "S" in b[: e + 1] and "N" in b[f:]: print(1) else: print(2) else: print(0)
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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) for _ in range(t): ct, n = 0, int(input()) a = list(map(int, input().split())) pre, s, f = 0, input(), True for i in a: if i < pre: f = False break pre = i if f: print(0) continue for i in s: if i == "S": ct += 1 if ct == n or ct == 0: print(-1) continue b = sorted(a) mini, maxi = 10**10, 0 for i, (av, bv) in enumerate(zip(a, b)): if av != bv: mini = min(mini, i) maxi = max(maxi, i) f1, f2 = False, False for i in s[:mini]: if i != s[maxi]: f1 = True for i in s[maxi:]: if i != s[mini]: f2 = True if s[mini] != s[maxi] or f1 or f2: print(1) else: print(2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) string = input() cnt_s = string.count("S") cnt_n = string.count("N") sorted_a = list(sorted(a)) if a == sorted_a: print(0) elif cnt_s == 0 or cnt_n == 0: print(-1) else: for i in range(n): if sorted_a[i] != a[i]: diff_index_start = i break for j in range(n - 1, -1, -1): if sorted_a[j] != a[j]: diff_index_end = j break left = string[: diff_index_start + 1] right = string[diff_index_end:] if "N" in left and "S" in right or "S" in left and "N" in right: ans = 1 else: ans = 2 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF STRING VAR STRING VAR STRING VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) while t: n = int(input()) li = list(map(int, input().strip().split())) s = input() lii = li.copy() lii.sort() ans = 2 if lii == li: ans = 0 elif len(set(s)) == 1: ans = -1 elif s[0] == s[n - 1] and len(set(s)) > 1: lis = list(s) for j in range(n - 1, 0, -1): if lis[j] != lis[0]: var = j break for i in range(n): if lis[i] != lis[n - 1]: var2 = i break if lii[var + 1 :] == li[var + 1 :]: ans = 1 elif lii[0:var2] == li[0:var2]: ans = 1 elif s[0] != s[n - 1]: ans = 1 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) s = input() arr1 = [] arr1 = arr.copy() arr1.sort() t1 = 0 t2 = 0 for i in range(n): if arr[n - 1 - i] != arr1[n - 1 - i] and t2 == 0: tester2 = n - 1 - i t2 = 1 if arr[i] != arr1[i] and t1 == 0: tester1 = i t1 = 1 if t1 == 1 and t2 == 1: break if arr1 == arr: print(0) elif len(set(s)) == 1: print(-1) elif s[0] != s[n - 1]: print(1) elif set(s[: tester1 + 1]) == set(s[tester2:]) and len(set(s[: tester1 + 1])) == 2: print(1) elif set(s[: tester1 + 1]) != set(s[tester2:]): print(1) else: print(2)
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 ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def res1(n, arr, stri): cnt1 = 0 cnt2 = 0 for i in range(n): if stri[i] == "N": cnt1 += 1 else: cnt2 += 1 sarr = sorted(arr) if cnt1 == n and sarr != arr or cnt2 == n and sarr != arr: return -1 if sarr == arr: return 0 if stri[0] != stri[-1]: return 1 s = arr.index(sarr[-1]) p = arr.index(sarr[0]) p = 0 for i in range(n): if arr[i] != sarr[i]: s = min(s, i) p = max(p, i) s1 = 0 p1 = 0 for k in range(n): if k < s: if stri[k] != stri[p]: s1 = 1 if k >= p: if stri[k] != stri[s]: p1 = 1 if s1 == 1 or p1 == 1 or stri[s] != stri[p]: return 1 else: return 2 for _ in range(int(input())): n = int(input()) arr = [int(i) for i in input().split()] stri = input() print(res1(n, arr, stri))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
try: for t in range(int(input())): number = int(input()) ant = list(map(int, input().split())) starting = input() sorts = sorted(ant) if ant == sorts: print(0) else: fin = 0 last = number - 1 for i in range(number): if ant[i] != sorts[i]: fin = i break for i in range(number - 1, -1, -1): if ant[i] != sorts[i]: la = i break o = True o = set() for i in range(fin + 1): o.add(starting[i]) for i in range(number - 1, la - 1, -1): o.add(starting[i]) if len(o) == 2: print(1) elif starting.count(starting[0]) == number: print(-1) else: print(2) except EOFError: pass
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 ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) for _ in range(t): n = int(input()) nums = list(map(int, input().split())) chars = [x for x in input()] num_north = sum([(1 if x == "N" else 0) for x in chars]) if all(nums[x] <= nums[x + 1] for x in range(n - 1)): print(0) elif num_north == 0 or num_north == n: print(-1) else: first_north = -1 last_north = -1 first_south = -1 last_south = -1 for i in range(n): if chars[i] == "N": first_north = i break for i in range(n): if chars[i] == "S": first_south = i break for i in range(n - 1, -1, -1): if chars[i] == "N": last_north = i break for i in range(n - 1, -1, -1): if chars[i] == "S": last_south = i break one = ( nums[:first_north] + sorted(nums[first_north : last_south + 1]) + nums[last_south + 1 :] ) two = ( nums[:first_south] + sorted(nums[first_south : last_north + 1]) + nums[last_north + 1 :] ) if all(one[i] <= one[i + 1] for i in range(n - 1)) or all( two[i] <= two[i + 1] for i in range(n - 1) ): print(1) else: print(2)
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 VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) b = sorted(a) d = list(input()) if a == b: print(0) continue if d.count("N") == n or d.count("N") == 0: print("-1") continue start = -1 end = -1 for i in range(n): if a[i] != b[i] and start == -1: start = i end = i elif a[i] != b[i]: end = i now = d[: start + 1] now1 = d[end:] now = now + now1 now = set(now) if len(now) == 2: print(1) else: print(2)
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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def arraySortedOrNot(arr, N): if N == 0 or N == 1: return True for i in range(1, N): if arr[i - 1] > arr[i]: return False return True T = int(input()) for _ in range(T): N = int(input()) A = list(map(int, input().split())) polarityStr = input() boolSort = arraySortedOrNot(A, len(A)) if "S" in polarityStr and "N" in polarityStr: if boolSort: print(0) elif polarityStr[0] != polarityStr[N - 1]: print(1) else: a = 1 b = N - 2 while True: if polarityStr[a] != polarityStr[0]: break a = a + 1 while True: if polarityStr[b] != polarityStr[N - 1]: break b = b - 1 l1 = sorted(A) if A[:a] == l1[:a] or A[b + 1 :] == l1[b + 1 :]: print(1) else: print(2) elif boolSort: print(0) else: print(-1)
FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF STRING VAR STRING VAR IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def solve(n, s, nums): if sorted(nums) == nums: return 0 if "N" not in s or "S" not in s: return -1 temp = nums.copy() if s[0] == "N": idx = -1 for i in range(n): if s[i] == "S": idx = i temp[: idx + 1] = sorted(temp[: idx + 1]) else: idx = -1 for i in range(n): if s[i] == "N": idx = i temp[: idx + 1] = sorted(temp[: idx + 1]) if temp == sorted(temp): return 1 temp = nums.copy() if s[-1] == "N": idx = -1 for i in range(n - 1, -1, -1): if s[i] == "S": idx = i temp[idx:] = sorted(temp[idx:]) else: idx = -1 for i in range(n - 1, -1, -1): if s[i] == "N": idx = i temp[idx:] = sorted(temp[idx:]) if temp == sorted(temp): return 1 return 2 for t in range(int(input())): n = int(input()) li = list(map(int, input().split())) s = input() print(solve(n, s, li))
FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF STRING VAR STRING VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): N = int(input()) A = [int(x) for x in input().split()] P = input() SA = sorted(A) if A == SA: print(0) continue elif not (P.count("N") and P.count("S")): print(-1) continue elif P[0] != P[-1]: print(1) continue s, e = 0, N - 1 while s < e: if P[s] != P[0] or P[e] != P[0]: break if A[s] == SA[s]: s += 1 elif A[e] == SA[e]: e -= 1 else: break if P[s] != P[0] or P[e] != P[0]: print(1) else: print(2)
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) polarity = input() if sorted(a) == a: print(0) elif len(set(polarity)) == 1: print(-1) elif polarity[0] != polarity[-1]: print(1) else: f = 0 i = 0 p1 = 0 p2 = 0 while i < n: if f == 0 and polarity[i] == "N": f = 1 p1 = i if polarity[i] == "S": p2 = i i += 1 t1 = a[:p1] + sorted(a[p1 : p2 + 1]) + a[p2 + 1 :] f = 0 i = 0 p1 = 0 p2 = 0 while i < n: if f == 0 and polarity[i] == "S": f = 1 p1 = i if polarity[i] == "N": p2 = i i += 1 t2 = a[:p1] + sorted(a[p1 : p2 + 1]) + a[p2 + 1 :] if sorted(a) == t1 or sorted(a) == t2: print(1) else: print(2)
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 IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) s = input() opp = {"S": "N", "N": "S"} temp = sorted(a) if a == temp: print(0) elif s[0] != s[-1]: print(1) elif opp[s[0]] not in s: print(-1) else: for i in range(n): if a[i] != temp[i]: s1 = i break for i in range(n - 1, -1, -1): if a[i] != temp[i]: s2 = i break if s[s1] != s[s2] or opp[s[s1]] in s[:s1] + s[s2 + 1 :]: print(1) else: print(2)
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 ASSIGN VAR DICT STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def getCount(): for k in range(num): if arr1[k] != sorted[k]: break pol = arr2[k] if pol == "N": for l in range(num - 1, k, -1): if arr1[l] != sorted[l] and arr2[l] == "N": break if (arr1[l] != sorted[l] or arr1[l] == sorted[l]) and arr2[l] == "S": return 1 elif pol == "S": for l in range(num - 1, k, -1): if arr1[l] != sorted[l] and arr2[l] == "S": break if (arr1[l] != sorted[l] or arr1[l] == sorted[l]) and arr2[l] == "N": return 1 pol2 = arr2[l] if pol2 == "S": for g in range(k + 1): if arr2[g] == "N": return 1 else: for g in range(k + 1): if arr2[g] == "S": return 1 return 2 n = int(input()) for _ in range(n): num = int(input()) arr1 = list(map(int, input().split())) arr2 = input() sorted = [] for i in arr1: sorted.append(i) sorted.sort() if sorted == arr1: print("0") elif "S" not in arr2 or "N" not in arr2: print("-1") elif ( arr2[0] == "S" and arr2[len(arr2) - 1] == "N" or arr2[0] == "N" and arr2[len(arr2) - 1] == "S" ): print("1") else: print(getCount())
FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR STRING RETURN NUMBER IF VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR STRING RETURN NUMBER ASSIGN VAR VAR VAR IF VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING 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 FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF STRING VAR STRING VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) s = input().strip() if a == sorted(a) or n == 1: print(0) continue if s.count("N") == n or s.count("S") == n: print(-1) continue if s[0] == "N" and s[-1] == "S" or s[0] == "S" and s[-1] == "N": print(1) else: for i in range(n - 1, -1, -1): if s[0] != s[i]: y = i break for i in range(n): if s[-1] != s[i]: x = i break a1 = a[:x] + sorted(a[x:]) a2 = sorted(a[: y + 1]) + a[y + 1 :] b = sorted(a) if b == a1 or b == a2: print(1) else: print(2)
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 FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def mi(): return map(int, input().split()) def li(): return list(mi()) def ni(): return int(input()) def si(): return str(input()) def isSorted(A): if not A: return True temp = list(A) temp.sort() return temp == A for t in range(ni()): N = ni() A = li() S = si() new = "" for i in S: if i == "S": new += "0" else: new += "1" S = new if isSorted(A): print(0) continue if S == "0" * N or S == "1" * N: print(-1) continue if S[0] != S[-1]: print(1) continue st = list(A) st.sort() temp = S[::-1] first = S.find(str(int(S[0]) ^ 1)) last = N - 1 - temp.find(str(int(S[0]) ^ 1)) if st[:first] == A[:first] or st[last + 1 :] == A[last + 1 :]: print(1) else: print(2)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING VAR STRING ASSIGN VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP STRING VAR VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def solve(n, a, s): b = sorted(a) i = 0 while i < n and a[i] == b[i]: i += 1 if i == n: return 0 if s == "S" * n or s == "N" * n: return -1 j = n - 1 while j >= 0 and a[j] == b[j]: j -= 1 s = s[: i + 1] + s[j:] if s == "S" * (i + 1 + n - j) or s == "N" * (i + 1 + n - j): return 2 return 1 t = int(input().strip()) for _ in range(t): n = int(input().strip()) a = list(map(int, input().strip().split())) s = input().strip() print(solve(n, a, s))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR BIN_OP STRING VAR VAR BIN_OP STRING VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP STRING BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP STRING BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) for i in range(t): n = int(input()) lst = list(map(int, input().split())) s = input() ele = s[0] c = 0 lst1 = lst.copy() lst1.sort() if lst1 == lst: print(0) else: for j in range(1, n): if s[j] != ele: c += 1 break if c == 0: print(-1) else: ns = 0 sn = 0 n1 = s.index("N") s1 = n - s[::-1].index("S") - 1 if s1 > n1: l1 = s1 - n1 else: l1 = 0 n2 = n - s[::-1].index("N") - 1 s2 = s.index("S") if n2 > s2: l2 = n2 - s2 else: l2 = 0 c = 0 lsta = lst[:n1] lstb = lst[n1 : s1 + 1] lstc = lst[s1 + 1 :] lstb.sort() lstf = lsta + lstb + lstc if lstf == lst1: c += 1 lsta = lst[:s2] lstb = lst[s2 : n2 + 1] lstc = lst[n2 + 1 :] lstb.sort() lstf = lsta + lstb + lstc if lstf == lst1: c += 1 lsta = lst[:s2] lstb = lst[s2 : n2 + 1] lstc = lst[n2 + 1 :] lstb.sort() lstf = lsta + lstb + lstc if lstf == lst1: c += 1 lsta = lst[:n1] lstb = lst[n1 : s1 + 1] lstc = lst[s1 + 1 :] lstb.sort() lstf = lsta + lstb + lstc if lstf == lst1: c += 1 if c > 0: print(1) else: start = -1 pol = "a" for a in range(1, n): if lst[a] < lst[a - 1]: start = a - 1 pol = s[a - 1] break end = -1 for l in range(n - 1, start, -1): if s[l] != pol: end = l lsta = lst[:start] lstb = lst[start : end + 1] lstc = lst[end + 1 :] lstb.sort() lstf = lsta + lstb + lstc if lstf == lst1: print(1) else: print(2)
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 FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER STRING NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) def solve(): N = int(input()) A = [int(x) for x in input().split(" ")] S = input().strip("\n") sA = list(sorted(A)) if sA == A: print("0") return firsts, firstn, lasts, lastn, firstw, lastw = [-1] * 6 for i in range(N): if S[i] == "S": if firsts == -1: firsts = i lasts = i else: if firstn == -1: firstn = i lastn = i if sA[i] != A[i]: if firstw == -1: firstw = i lastw = i if (firsts >= 0 and firstn >= 0) and ( firstw >= firsts and lastw <= lastn or firstw >= firstn and lastw <= lasts ): print("1") elif firsts >= 0 and firstn >= 0: print("2") else: print("-1") while t: solve() t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) ai = list(map(int, input().split())) s = input() s1 = -1 s2 = -1 n1 = -1 n2 = -1 for i in range(n): if s[i] == "S": s2 = i else: n2 = i if s[n - i - 1] == "S": s1 = n - i - 1 else: n1 = n - i - 1 li = sorted(ai) if ai == li: print(0) elif s.count("N") == n or s.count("N") == 0: print(-1) elif ( ai[:s1] + sorted(ai[s1 : n2 + 1]) + ai[n2 + 1 :] == li or ai[:n1] + sorted(ai[n1 : s2 + 1]) + ai[s2 + 1 :] == li ): print(1) else: print(2)
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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def solve(A, S): arr = sorted(A) if arr == A: return 0 n_count = S.count("N") s_count = S.count("S") if n_count == 0 or s_count == 0: return -1 l = float("inf") r = 0 left_N = False right_N = False left_S = False right_S = False for i in range(len(A)): if A[i] != arr[i]: l = min(l, i) r = max(r, i) for i in range(l + 1): if S[i] == "N": left_N = True if S[i] == "S": left_S = True for i in range(r, len(A)): if S[i] == "N": right_N = True if S[i] == "S": right_S = True if left_S and right_N or left_N and right_S: return 1 return 2 test_cases = int(input()) for i in range(int(test_cases)): n = int(input()) A = list(map(int, input().split())) S = input() print(solve(A, S))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL 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 FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) while t != 0: n = int(input()) A = list(map(int, input().split())) s = input() B = sorted(A) k = set(s) if A == B: print(0) t -= 1 continue if "N" not in k or "S" not in k: print(-1) t -= 1 continue i = 0 while i < n and A[i] == B[i]: i += 1 j = n - 1 while j >= 0 and A[j] == B[j]: j -= 1 ok = 0 for k in range(i + 1): if s[k] != s[j]: ok = 1 break for k in range(n - 1, j - 1, -1): if s[k] != s[i]: ok = 1 break if i < j and ok: print(1) else: print(2) t -= 1
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
_ = int(input()) while _: _ -= 1 n = int(input()) l = list(map(int, input().split())) s = input() nl = sorted(l) if l == nl: print(0) elif "N" not in s or "S" not in s: print(-1) elif s[0] != s[-1]: print(1) else: fn = s.index("N") fs = s.index("S") ln = n - s[::-1].index("N") ls = n - s[::-1].index("S") if ( l[:fn] + sorted(l[fn:ls]) + l[ls:] == nl or l[:fs] + sorted(l[fs:ln]) + l[ln:] == nl ): print(1) else: print(2)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER STRING IF BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) st = list(input()) f = [] res = sorted(arr) for i in range(0, n): if arr[i] != res[i]: f.append(i) com = st[0] flag = 0 for i in st: if i != com: flag = 1 if len(f) == 0: print(0) elif flag == 0: print(-1) else: r = len(f) flag1 = 0 for i in range(0, f[0] + 1): for j in range(n - 1, f[r - 1] - 1, -1): if st[i] != st[j]: print(1) flag1 = 1 break if flag1 == 1: break if flag1 == 0: print(2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def solve(n, arr, s): a = list(sorted(arr)) if arr == a or n == 1: return 0 if len(set(s)) == 1: return -1 i, j = 0, n - 1 while arr[i] == a[i]: i += 1 while arr[j] == a[j]: j -= 1 lefts = "S" in s[: i + 1] leftn = "N" in s[: i + 1] rights = "S" in s[j:] rightn = "N" in s[j:] if lefts and rightn or leftn and rights: return 1 return 2 t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) s = input() print(solve(n, arr, s))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR STRING VAR VAR ASSIGN VAR STRING VAR VAR 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 FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) s = input() if a == sorted(a): print(0) continue if not s.count("N") or not s.count("S"): print(-1) continue firstN, firstS, lastN, lastS = -1, -1, -1, -1 for i in range(n): if s[i] == "N": if firstN == -1: firstN = i lastN = i if s[i] == "S": if firstS == -1: firstS = i lastS = i b = sorted(a) for i in range(n): if (i < firstN or i > lastS) and a[i] != b[i]: break else: print(1) continue for i in range(n): if (i < firstS or i > lastN) and a[i] != b[i]: break else: print(1) continue print(2)
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 IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
T = int(input()) while T: T -= 1 N = int(input()) arr = list(map(int, input().split())) polar = input() sorted_arr = sorted(arr) if arr == sorted_arr: print(0) continue if polar[0] != polar[-1]: print(1) continue if polar.count("N") == len(polar) or polar.count("S") == len(polar): print(-1) continue start = float("inf") end = float("-inf") for i in range(N): if arr[i] != sorted_arr[i]: start = min(start, i) end = max(end, i) if polar[start] != polar[end]: print(1) continue else: flag = False for i in range(start): if polar[i] != polar[end]: flag = True for j in range(end + 1, N): if polar[j] != polar[start]: flag = True if flag: print(1) else: print(2)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) s = input() arr = [] for j in a: arr.append(j) arr.sort() c = c1 = c2 = d = e = 0 for j in range(n - 1): if a[j] > a[j + 1]: c = 1 break if c == 0: print(0) else: for j in range(n): if s[j] == "N": c1 = c1 + 1 else: c2 = c2 + 1 if c1 == 0 or c2 == 0: print(-1) else: for j in range(n - 1): if s[j] != s[n - 1]: p1 = j break for j in range(n - 1, 0, -1): if s[j] != s[0]: p2 = j break for j in range(p1): if a[j] != arr[j]: d = 1 break for j in range(p2 + 1, n): if a[j] != arr[j]: e = 1 break if d == 1 and e == 1: print(2) else: print(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 FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def solve(a, p, s): if a == s: return 0 elif p[0] != p[-1]: return 1 elif ("N" in p) == False or ("S" in p) == False: return -1 if p[0] == p[-1]: start = 2147483647 last = -2147483648 for i in range(n): if s[i] != a[i]: start = min(i, start) last = max(last, i) a1 = False a2 = False for i in range(start): if p[i] != p[start]: a1 = True for i in range(last, n): if p[i] != p[start]: a1 = True if p[start] != p[last] or a1 or a2: return 1 else: return 2 T = int(input()) for _ in range(T): n = int(input()) a = list(map(int, input().split())) p = input() s = sorted(a) print(solve(a, p, s))
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF STRING VAR NUMBER STRING VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): N = input() x = list(map(int, input().split())) weights = input() sx = sorted([(a, b, weights[a]) for a, b in enumerate(x)], key=lambda x: x[1]) first_N = last_N = first_S = last_S = -1 fS = fN = fX = True fu = lu = -1 m = [float("inf"), -1] for i in range(len(x)): z = weights[i] if fN: if z == "N": fN = False first_N = i if fS: if z == "S": fS = False first_S = i if sx[i][0] != i: m[0] = min(m[0], sx[i][0]) m[1] = max(m[1], sx[i][0]) if z == "S": last_S = i if z == "N": last_N = i if m == [float("inf"), -1]: print(0) continue if first_S == -1 or first_N == -1: print(-1) continue if weights[0] != weights[-1]: print(1) continue if first_S <= m[0] and last_N >= m[1] or first_N <= m[0] and last_S >= m[1]: print(1) continue print(2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR LIST FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) lst = list(map(int, input().split())) s = input() sorted_lst = sorted(lst) if n == 1: print(0) elif lst != sorted_lst: if s[0] != s[-1]: print(1) elif s == "N" * n or s == "S" * n: print(-1) else: for i in range(n): if lst[i] != sorted_lst[i]: start = i break for i in range(n - 1, -1, -1): if lst[i] != sorted_lst[i]: end = i break if s[start] != s[end]: print(1) else: for i in range(start): if s[i] != s[end]: print(1) break else: for i in range(end, n): if s[i] != s[start]: print(1) break else: print(2) else: print(0)
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 ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP STRING VAR VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) s = input().strip() b = sorted(a) if b == a or len(a) == 1: print(0) else: st = 0 for i in range(n): if a[i] == b[i]: pass else: st = i break ed = n - 1 for i in range(ed, st - 1, -1): if a[i] == b[i]: pass else: ed = i break if s[st] != s[ed]: print(1) elif s[st] == "N": pres = 0 for i in range(st): if s[i] == "S": pres = 1 for i in range(ed + 1, n): if s[i] == "S": pres = 1 if pres: print(1) else: for i in range(st + 1, ed): if s[i] == "S": pres = 1 if pres: print(2) else: print(-1) else: pres = 0 for i in range(st): if s[i] == "N": pres = 1 for i in range(ed + 1, n): if s[i] == "N": pres = 1 if pres: print(1) else: for i in range(st + 1, ed): if s[i] == "N": pres = 1 if pres: print(2) else: print(-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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
def MostMisplaced(arr, arrSorted): for x in range(len(arr)): if arr[x] != arrSorted[x]: return x return None def insideRange(val, myRange): if val >= myRange[0] and val <= myRange[1]: return True else: return False def areIn(leftMost, rightMost, givenRange): if insideRange(leftMost, givenRange) and insideRange(rightMost, givenRange): return True return False def minCost(arr, pole): arrSorted = arr[:] arrSorted.sort() leftMisplace = MostMisplaced(arr, arrSorted) if leftMisplace == None: return 0 rightMisplace = len(arr) - 1 - MostMisplaced(arr[::-1], arrSorted[::-1]) try: leftMostN = pole.index("N") leftMostS = pole.index("S") rightMostN = len(pole) - 1 - pole[::-1].index("N") rightMostS = len(pole) - 1 - pole[::-1].index("S") NS = leftMostN, rightMostS SN = leftMostS, rightMostN if areIn(leftMisplace, rightMisplace, NS) or areIn( leftMisplace, rightMisplace, SN ): return 1 else: return 2 except: return -1 T = int(input()) for i in range(T): N = int(input()) arr = [int(x) for x in input().split()] pole = [x for x in input()] print(minCost(arr, pole))
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR RETURN NONE FUNC_DEF IF VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) s = input().strip() if a == sorted(a) or n == 1: print(0) continue if s.count("N") == n or s.count("S") == n: print(-1) continue if s[0] == "N" and s[-1] == "S" or s[0] == "S" and s[-1] == "N": print(1) else: b = sorted(a) start = s[0] A, B = 0, 0 for i in range(n): if s[i] != start: A = i break for i in range(n - 1, -1, -1): if s[i] != start: B = i break if b[:A] == a[:A] or b[B + 1 :] == a[B + 1 :]: print(1) else: print(2)
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 FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for t in range(int(input())): n = int(input()) lst = list(map(int, input().split(" "))) dirc = input() if lst == sorted(lst): print(0) elif dirc[0] != dirc[n - 1]: print(1) elif "N" in dirc and "S" in dirc: i = n - 1 while dirc[0] == dirc[i]: i -= 1 else: if sorted(lst[: i + 1]) + lst[i + 1 :] == sorted(lst): print(1) else: i = 0 while dirc[n - 1] == dirc[i]: i += 1 else: if lst[:i] + sorted(lst[i:]) == sorted(lst): print(1) else: print(2) else: print(-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 STRING ASSIGN VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF STRING VAR STRING VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) p = str(input()) f = a.copy() f.sort() if f == a: print(0) elif p.count("S") == 0 or p.count("N") == 0: print(-1) elif p[0] != p[-1]: print(1) elif p[0] == p[-1]: sm = True em = True si = 0 while p[si] == p[0] and si < n: if f[si] != a[si] and p[si] == p[0]: sm = False si += 1 ei = n - 1 while p[ei] == p[0] and ei >= 0: if f[ei] != a[ei] and p[ei] == p[0]: em = False ei -= 1 if sm == False and em == False: print(2) else: print(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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
try: t = int(input()) for q in range(t): n = int(input()) l = list(map(int, input().split())) s = input() a = [] flag = 0 left = 0 right = n - 1 for v in range(n - 1): if s[v] != s[v + 1]: flag += 1 for j in l: a.append(j) l.sort() index = [] for i in range(n): if a[i] != l[i]: index.append(i) f = 0 if len(index) != 0: for z in range(min(index) + 1): if f == 1: break for y in range(n - 1, max(index) - 1, -1): if f == 1: break if s[z] != s[y]: print(1) f = 1 if f == 0: if flag == 0: print(-1) else: print(2) else: print(0) except: pass
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 FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) for _ in range(t): n = int(input()) l = list(map(int, input().split())) s = input() k = l.copy() k.sort() if k == l: print(0) elif len(set(s)) == 1: print(-1) elif s[0] != s[-1]: print(1) else: c = s[-1] ind = 0 for i in range(n): if l[i] != k[i]: ind = i break last = n - 1 for i in range(n - 1, ind, -1): if k[i] != l[i]: last = i break c2 = s[last] flag = False for i in range(ind + 1): for j in range(last, n): if s[i] != s[j]: print(1) flag = True break if flag: break else: print(2)
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) s = input() c = sorted(a) if c == a: print(0) continue if 0 in [s.count("N"), s.count("S")]: print(-1) continue l = None r = None for j in range(n): if c[j] != a[j]: r = j if l == None: l = j if s[l] != s[r]: print(1) elif s[l] == "N": if "S" in s[: l + 1] or "S" in s[r:]: print(1) continue else: print(2) elif "N" in s[: l + 1] or "N" in s[r:]: print(1) else: print(2)
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF NUMBER LIST FUNC_CALL VAR STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING IF STRING VAR BIN_OP VAR NUMBER STRING VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF STRING VAR BIN_OP VAR NUMBER STRING VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) s = input() if "N" in s and "S" in s: if l == sorted(l): print("0") else: a = 0 b = n - 1 if s[a] != s[b]: print("1") else: a = 1 b = n - 2 while True: if s[a] != s[0]: break a = a + 1 while True: if s[b] != s[n - 1]: break b = b - 1 l1 = sorted(l) if l[:a] == l1[:a] or l[b + 1 :] == l1[b + 1 :]: print("1") else: print("2") elif l == sorted(l): print("0") else: print("-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 FUNC_CALL VAR IF STRING VAR STRING VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
There is an array A with N elements. Each element of A has a fixed *polarity*: either north or south. Chef is allowed to perform some (possibly zero) operations on the array A. In one operation, Chef can: Pick some subarray of array A, such that, the first and last elements of the subarray have different polarities, and, rearrange the elements in this subarray any way he wants. Note that the polarity of each element remains unchanged after an operation. Find the minimum number of operations required to sort the array in non-decreasing order, or state that it is impossible. A subarray of A is obtained by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. ------ Input Format ------ - The first line contains an integer T, denoting the number of test cases. The T test cases then follow. - The first line of each test case contains a single integer N. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The third line of each test case contains a string of length N, the ith character of which is either \texttt{N} or \texttt{S}, representing that the ith element of A has north or south polarity, respectively. ------ Output Format ------ For each test case, if it impossible to sort the array, output -1. Otherwise, output a single integer: the minimum number of operations required to sort the array. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - The sum of $N$ across all test cases doesn't exceed $2 \cdot 10^{5}$. ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 6 5 1 3 2 3 7 NNSNS 2 2 1 SS 3 1 8 12 SNS 3 3 2 1 NSN 5 1 2 3 5 4 NSNSN 5 1 1 2 2 1 SNSNN ----- Sample Output 1 ------ 1 -1 0 2 1 1 ----- explanation 1 ------ Let's represent elements with a polarity of north in $\color{red}{\text{red}}$, and elements with a polarity of south in $\color{blue}{\text{blue}}$. The polarity of each element is also labelled above it. - In the first test case, we can sort the array in a single operation as follows. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{1}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{7}}\color{black}{]}$. - In the second test case, the array $[\color{blue}{\stackrel{\texttt{S}}{2}}, \color{blue}{\stackrel{\texttt{S}}{1}}\color{black}{]}$ cannot be sorted, since no operations can be performed. - In the third test case, the array is already sorted, so the answer is $0$. - In the fourth test case, we can sort the array in two operations as follows. - Rearrange the subarray $[A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{1}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]}$. - Rearrange the subarray $[A_{1}, A_{2}, A_{3}]$: $[\color{red}{\stackrel{\texttt{N}}{3}}, \color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}\color{black}{]} \to [\color{red}{\stackrel{\texttt{N}}{1}}, \color{blue}{\stackrel{\texttt{S}}{2}}, \color{red}{\stackrel{\texttt{N}}{3}}\color{black}{]}$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) s = input() d = a.copy() d.sort() flag = 1 for i in range(n): if d[i] != a[i]: flag = 0 break if flag == 1: print(0) else: countn = 0 counts = 0 for i in range(n): if s[i] == "N": countn += 1 else: counts += 1 if countn == 0 or counts == 0: print(-1) elif s[0] != s[n - 1]: print(1) else: flag = 1 for i in range(n - 1, -1, -1): if a[i] != d[i]: if s[i] == s[0]: flag = 0 break elif s[i] != s[0]: break if flag == 1: print(1) else: flag = 1 for i in range(n): if a[i] != d[i]: if s[i] == s[n - 1]: flag = 0 break elif s[i] != s[0]: break if flag == 1: print(1) else: print(2)
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 ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits. Also Pasha has a number k and two sequences of length n / k (n is divisible by k) a_1, a_2, ..., a_{n} / k and b_1, b_2, ..., b_{n} / k. Let's split the phone number into blocks of length k. The first block will be formed by digits from the phone number that are on positions 1, 2,..., k, the second block will be formed by digits from the phone number that are on positions k + 1, k + 2, ..., 2·k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit b_{i} and is divisible by a_{i} if represented as an integer. To represent the block of length k as an integer, let's write it out as a sequence c_1, c_2,...,c_{k}. Then the integer is calculated as the result of the expression c_1·10^{k} - 1 + c_2·10^{k} - 2 + ... + c_{k}. Pasha asks you to calculate the number of good phone numbers of length n, for the given k, a_{i} and b_{i}. As this number can be too big, print it modulo 10^9 + 7. -----Input----- The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(n, 9)) — the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k. The second line of the input contains n / k space-separated positive integers — sequence a_1, a_2, ..., a_{n} / k (1 ≤ a_{i} < 10^{k}). The third line of the input contains n / k space-separated positive integers — sequence b_1, b_2, ..., b_{n} / k (0 ≤ b_{i} ≤ 9). -----Output----- Print a single integer — the number of good phone numbers of length n modulo 10^9 + 7. -----Examples----- Input 6 2 38 56 49 7 3 4 Output 8 Input 8 2 1 22 3 44 5 4 3 2 Output 32400 -----Note----- In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.
import sys input = sys.stdin.readline n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) MOD = 10**9 + 7 ans = 1 for ai, bi in zip(a, b): if bi == 0: h = 10 ** (k - 1) - 1 cnt = h // ai + 1 else: h = bi * 10 ** (k - 1) + 10 ** (k - 1) - 1 l = (bi - 1) * 10 ** (k - 1) + 10 ** (k - 1) - 1 cnt = h // ai - l // ai ans *= (10**k - 1) // ai + 1 - cnt ans %= MOD print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits. Also Pasha has a number k and two sequences of length n / k (n is divisible by k) a_1, a_2, ..., a_{n} / k and b_1, b_2, ..., b_{n} / k. Let's split the phone number into blocks of length k. The first block will be formed by digits from the phone number that are on positions 1, 2,..., k, the second block will be formed by digits from the phone number that are on positions k + 1, k + 2, ..., 2·k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit b_{i} and is divisible by a_{i} if represented as an integer. To represent the block of length k as an integer, let's write it out as a sequence c_1, c_2,...,c_{k}. Then the integer is calculated as the result of the expression c_1·10^{k} - 1 + c_2·10^{k} - 2 + ... + c_{k}. Pasha asks you to calculate the number of good phone numbers of length n, for the given k, a_{i} and b_{i}. As this number can be too big, print it modulo 10^9 + 7. -----Input----- The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(n, 9)) — the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k. The second line of the input contains n / k space-separated positive integers — sequence a_1, a_2, ..., a_{n} / k (1 ≤ a_{i} < 10^{k}). The third line of the input contains n / k space-separated positive integers — sequence b_1, b_2, ..., b_{n} / k (0 ≤ b_{i} ≤ 9). -----Output----- Print a single integer — the number of good phone numbers of length n modulo 10^9 + 7. -----Examples----- Input 6 2 38 56 49 7 3 4 Output 8 Input 8 2 1 22 3 44 5 4 3 2 Output 32400 -----Note----- In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.
n1 = input() q, w = n1.split() n = int(q) k = int(w) n2 = input() A1 = n2.split() n3 = input() B1 = n3.split() x = "" y = "" A = [] B = [] for i in range(0, n // k): A.append(int(A1[i])) B.append(int(B1[i])) for i in range(0, k - 1): x += "0" y += "9" fin = 0 for i in range(0, n // k): ans = int("9" + y) // A[i] + 1 h = int(str(B[i]) + x) g = int(str(B[i]) + y) d = g - g % A[i] if d >= h: ans -= (d - h) // A[i] + 1 if fin == 0 and ans > 0: fin = 1 fin = fin * ans % 1000000007 print(fin)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR STRING VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP STRING VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits. Also Pasha has a number k and two sequences of length n / k (n is divisible by k) a_1, a_2, ..., a_{n} / k and b_1, b_2, ..., b_{n} / k. Let's split the phone number into blocks of length k. The first block will be formed by digits from the phone number that are on positions 1, 2,..., k, the second block will be formed by digits from the phone number that are on positions k + 1, k + 2, ..., 2·k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit b_{i} and is divisible by a_{i} if represented as an integer. To represent the block of length k as an integer, let's write it out as a sequence c_1, c_2,...,c_{k}. Then the integer is calculated as the result of the expression c_1·10^{k} - 1 + c_2·10^{k} - 2 + ... + c_{k}. Pasha asks you to calculate the number of good phone numbers of length n, for the given k, a_{i} and b_{i}. As this number can be too big, print it modulo 10^9 + 7. -----Input----- The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(n, 9)) — the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k. The second line of the input contains n / k space-separated positive integers — sequence a_1, a_2, ..., a_{n} / k (1 ≤ a_{i} < 10^{k}). The third line of the input contains n / k space-separated positive integers — sequence b_1, b_2, ..., b_{n} / k (0 ≤ b_{i} ≤ 9). -----Output----- Print a single integer — the number of good phone numbers of length n modulo 10^9 + 7. -----Examples----- Input 6 2 38 56 49 7 3 4 Output 8 Input 8 2 1 22 3 44 5 4 3 2 Output 32400 -----Note----- In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.
def get_multypler(k, a, b): res = (10**k - 1) // a + 1 bad_residue = (a - 10 ** (k - 1) * b % a) % a suf = 10 ** (k - 1) - 1 minus = suf // a + (1 if bad_residue <= suf % a else 0) return res - minus n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 1 for i in range(n // k): ans *= get_multypler(k, a[i], b[i]) ans %= 10**9 + 7 print(ans)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits. Also Pasha has a number k and two sequences of length n / k (n is divisible by k) a_1, a_2, ..., a_{n} / k and b_1, b_2, ..., b_{n} / k. Let's split the phone number into blocks of length k. The first block will be formed by digits from the phone number that are on positions 1, 2,..., k, the second block will be formed by digits from the phone number that are on positions k + 1, k + 2, ..., 2·k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit b_{i} and is divisible by a_{i} if represented as an integer. To represent the block of length k as an integer, let's write it out as a sequence c_1, c_2,...,c_{k}. Then the integer is calculated as the result of the expression c_1·10^{k} - 1 + c_2·10^{k} - 2 + ... + c_{k}. Pasha asks you to calculate the number of good phone numbers of length n, for the given k, a_{i} and b_{i}. As this number can be too big, print it modulo 10^9 + 7. -----Input----- The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(n, 9)) — the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k. The second line of the input contains n / k space-separated positive integers — sequence a_1, a_2, ..., a_{n} / k (1 ≤ a_{i} < 10^{k}). The third line of the input contains n / k space-separated positive integers — sequence b_1, b_2, ..., b_{n} / k (0 ≤ b_{i} ≤ 9). -----Output----- Print a single integer — the number of good phone numbers of length n modulo 10^9 + 7. -----Examples----- Input 6 2 38 56 49 7 3 4 Output 8 Input 8 2 1 22 3 44 5 4 3 2 Output 32400 -----Note----- In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.
n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = n // k ans = 1 for i in range(c): t_ans = ( (10**k - 1) // a[i] + 1 + b[i] * 10 ** (k - 1) // a[i] - (b[i] + 1) * 10 ** (k - 1) // a[i] ) if b[i] * 10 ** (k - 1) % a[i] == 0: t_ans -= 1 if (b[i] + 1) * 10 ** (k - 1) % a[i] == 0: t_ans += 1 ans *= t_ans ans = ans % (10**9 + 7) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits. Also Pasha has a number k and two sequences of length n / k (n is divisible by k) a_1, a_2, ..., a_{n} / k and b_1, b_2, ..., b_{n} / k. Let's split the phone number into blocks of length k. The first block will be formed by digits from the phone number that are on positions 1, 2,..., k, the second block will be formed by digits from the phone number that are on positions k + 1, k + 2, ..., 2·k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit b_{i} and is divisible by a_{i} if represented as an integer. To represent the block of length k as an integer, let's write it out as a sequence c_1, c_2,...,c_{k}. Then the integer is calculated as the result of the expression c_1·10^{k} - 1 + c_2·10^{k} - 2 + ... + c_{k}. Pasha asks you to calculate the number of good phone numbers of length n, for the given k, a_{i} and b_{i}. As this number can be too big, print it modulo 10^9 + 7. -----Input----- The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(n, 9)) — the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k. The second line of the input contains n / k space-separated positive integers — sequence a_1, a_2, ..., a_{n} / k (1 ≤ a_{i} < 10^{k}). The third line of the input contains n / k space-separated positive integers — sequence b_1, b_2, ..., b_{n} / k (0 ≤ b_{i} ≤ 9). -----Output----- Print a single integer — the number of good phone numbers of length n modulo 10^9 + 7. -----Examples----- Input 6 2 38 56 49 7 3 4 Output 8 Input 8 2 1 22 3 44 5 4 3 2 Output 32400 -----Note----- In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.
s = input() ast = s.split(" ") n, k = int(ast[0]), int(ast[1]) s = input() a = [int(i) for i in s.split(" ")] s = input() b = [int(i) for i in s.split(" ")] L = n // k p = 10**k r = 10 ** (k - 1) rez = 1 for i in range(L): cnt = 0 if b[i] > 0: cnt += (b[i] * r - 1) // a[i] cnt += (p - 1) // a[i] cnt -= ((b[i] + 1) * r - 1) // a[i] rez *= cnt + 1 else: cnt += (p - 1) // a[i] cnt -= (r - 1) // a[i] rez *= cnt rez = rez % (10**9 + 7) print(rez)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR