message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular n Γ— m matrix a. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for i-th row it is equal r_i. What is the maximal possible value of r_1+r_2+…+r_n? Input The first line contains an integer t (1 ≀ t ≀ 40), the number of test cases in the input. The first line of each test case contains integers n and m (1 ≀ n ≀ 4, 1 ≀ m ≀ 100) β€” the number of rows and the number of columns in the given matrix a. Each of the following n lines contains m integers, the elements of a (1 ≀ a_{i, j} ≀ 10^5). Output Print t integers: answers for all test cases in the order they are given in the input. Example Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 Note In the first test case, you can shift the third column down by one, this way there will be r_1 = 5 and r_2 = 7. In the second case you can don't rotate anything at all, this way there will be r_1 = r_2 = 10 and r_3 = 9. Submitted Solution: ``` t = int(input()) while t>0: t-=1 n,m = map(int,input().split()) arr = [] for a in range(n): arr.append(list(map(int,input().split()))) maxi = [] for b in range(m): max_ = 0 for a in range(n): if arr[a][b]>max_: max_ = arr[a][b] maxi.append(max_) print(sum(list(sorted(maxi,reverse=True))[:n])) ```
instruction
0
22,941
12
45,882
No
output
1
22,941
12
45,883
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular n Γ— m matrix a. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for i-th row it is equal r_i. What is the maximal possible value of r_1+r_2+…+r_n? Input The first line contains an integer t (1 ≀ t ≀ 40), the number of test cases in the input. The first line of each test case contains integers n and m (1 ≀ n ≀ 4, 1 ≀ m ≀ 100) β€” the number of rows and the number of columns in the given matrix a. Each of the following n lines contains m integers, the elements of a (1 ≀ a_{i, j} ≀ 10^5). Output Print t integers: answers for all test cases in the order they are given in the input. Example Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 Note In the first test case, you can shift the third column down by one, this way there will be r_1 = 5 and r_2 = 7. In the second case you can don't rotate anything at all, this way there will be r_1 = r_2 = 10 and r_3 = 9. Submitted Solution: ``` def main(): input() n = int(input()) m = int(input()) maxList = list() a = [[0] * m for i in range(n)] max = 0 for i in range(n): for j in range(m): a[i][j] = int(input()) for j in range(m): max = 0 for i in range(n): if max < a[i][j]: max = a[i][j] maxList.append(max) maxList.sort() sum = 0 for el in maxList[-n:]: sum += el return sum ```
instruction
0
22,942
12
45,884
No
output
1
22,942
12
45,885
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easier version of the next problem. The difference is only in constraints. You are given a rectangular n Γ— m matrix a. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for i-th row it is equal r_i. What is the maximal possible value of r_1+r_2+…+r_n? Input The first line contains an integer t (1 ≀ t ≀ 40), the number of test cases in the input. The first line of each test case contains integers n and m (1 ≀ n ≀ 4, 1 ≀ m ≀ 100) β€” the number of rows and the number of columns in the given matrix a. Each of the following n lines contains m integers, the elements of a (1 ≀ a_{i, j} ≀ 10^5). Output Print t integers: answers for all test cases in the order they are given in the input. Example Input 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 Output 12 29 Note In the first test case, you can shift the third column down by one, this way there will be r_1 = 5 and r_2 = 7. In the second case you can don't rotate anything at all, this way there will be r_1 = r_2 = 10 and r_3 = 9. Submitted Solution: ``` rnd_mod = 1234567890133 rnd_x = 987654321098 def rnd(): global rnd_x rnd_x = rnd_x**2 % rnd_mod return (rnd_x>>5) % (1<<20) def randrange(a): return rnd() % a T = int(input()) for _ in range(T): N, M = map(int, input().split()) X = [] for __ in range(N): X.append([int(a) for a in input().split()]) Y = [[X[i][j] for i in range(N)] for j in range(M)] Y = sorted(Y, key = sum)[::-1][:N] M = len(Y) ma = 0 for t in range(1000): k = max(10 * (500 - t), 0) for i in range(M): a = randrange(N) Y[i] = [Y[i][j-a] for j in range(N)] m = sum([max([Y[i][j] for i in range(M)]) for j in range(N)]) if m < ma - k: Y[i] = [Y[i][(j+a)%N] for j in range(N)] else: ma = m print(ma) ```
instruction
0
22,943
12
45,886
No
output
1
22,943
12
45,887
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is interactive. We have hidden an array a of n pairwise different numbers (this means that no two numbers are equal). You can get some information about this array using a new device you just ordered on Amazon. This device can answer queries of the following form: in response to the positions of k different elements of the array, it will return the position and value of the m-th among them in the ascending order. Unfortunately, the instruction for the device was lost during delivery. However, you remember k, but don't remember m. Your task is to find m using queries to this device. You can ask not more than n queries. Note that the array a and number m are fixed before the start of the interaction and don't depend on your queries. In other words, interactor is not adaptive. Note that you don't have to minimize the number of queries, and you don't need to guess array a. You just have to guess m. Input The first line contains two integers n and k (1≀ k < n ≀ 500) β€” the length of the array and the number of the elements in the query. It is guaranteed that number m satisfies 1≀ m ≀ k, elements a_1, a_2, ..., a_n of the array satisfy 0≀ a_i ≀ 10^9, and all of them are different. Interaction You begin the interaction by reading n and k. To ask a question about elements on positions x_1, x_2, ..., x_k, in a separate line output ? x_1 x_2 x_3 ... x_k Numbers in the query have to satisfy 1 ≀ x_i ≀ n, and all x_i have to be different. Don't forget to 'flush', to get the answer. In response, you will receive two integers pos and a_{pos} β€” the position in the array a of the m-th in ascending order element among a_{x_1}, a_{x_2}, ..., a_{x_k}, and the element on this position. In case your query is invalid or you asked more than n queries, the program will print -1 and will finish interaction. You will receive a Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. When you determine m, output ! m After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see documentation for other languages. Hack format For the hacks use the following format: The first line has to contain three integers n, k, m (1 ≀ m ≀ k < n ≀ 500) β€” the length of the array, number of elements in the query, and which in the ascending order number the device returns. In the next line output n integers a_1, a_2, ..., a_n (0≀ a_i ≀ 10^9) β€” the elements of the array. They have to be pairwise different. Example Input 4 3 4 9 4 9 4 9 1 2 Output ? 2 3 4 ? 1 3 4 ? 1 2 4 ? 1 2 3 ! 3 Note In the example, n = 4, k = 3, m = 3, a = [2, 0, 1, 9].
instruction
0
22,960
12
45,920
Tags: constructive algorithms, interactive, math, sortings Correct Solution: ``` n, k = list(map(int, input().strip().split())) x = [] for i in range(1, k + 2): print("? " + ' '.join([str(x) for x in range(1, k + 2) if x != i])) a, b = list(map(int, input().strip().split())) x.append(b) z = 0 xx = max(x) for i in x: if (i == xx): z += 1 print("! " + str(z)) ```
output
1
22,960
12
45,921
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is interactive. We have hidden an array a of n pairwise different numbers (this means that no two numbers are equal). You can get some information about this array using a new device you just ordered on Amazon. This device can answer queries of the following form: in response to the positions of k different elements of the array, it will return the position and value of the m-th among them in the ascending order. Unfortunately, the instruction for the device was lost during delivery. However, you remember k, but don't remember m. Your task is to find m using queries to this device. You can ask not more than n queries. Note that the array a and number m are fixed before the start of the interaction and don't depend on your queries. In other words, interactor is not adaptive. Note that you don't have to minimize the number of queries, and you don't need to guess array a. You just have to guess m. Input The first line contains two integers n and k (1≀ k < n ≀ 500) β€” the length of the array and the number of the elements in the query. It is guaranteed that number m satisfies 1≀ m ≀ k, elements a_1, a_2, ..., a_n of the array satisfy 0≀ a_i ≀ 10^9, and all of them are different. Interaction You begin the interaction by reading n and k. To ask a question about elements on positions x_1, x_2, ..., x_k, in a separate line output ? x_1 x_2 x_3 ... x_k Numbers in the query have to satisfy 1 ≀ x_i ≀ n, and all x_i have to be different. Don't forget to 'flush', to get the answer. In response, you will receive two integers pos and a_{pos} β€” the position in the array a of the m-th in ascending order element among a_{x_1}, a_{x_2}, ..., a_{x_k}, and the element on this position. In case your query is invalid or you asked more than n queries, the program will print -1 and will finish interaction. You will receive a Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. When you determine m, output ! m After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see documentation for other languages. Hack format For the hacks use the following format: The first line has to contain three integers n, k, m (1 ≀ m ≀ k < n ≀ 500) β€” the length of the array, number of elements in the query, and which in the ascending order number the device returns. In the next line output n integers a_1, a_2, ..., a_n (0≀ a_i ≀ 10^9) β€” the elements of the array. They have to be pairwise different. Example Input 4 3 4 9 4 9 4 9 1 2 Output ? 2 3 4 ? 1 3 4 ? 1 2 4 ? 1 2 3 ! 3 Note In the example, n = 4, k = 3, m = 3, a = [2, 0, 1, 9].
instruction
0
22,961
12
45,922
Tags: constructive algorithms, interactive, math, sortings Correct Solution: ``` import sys def query(a): print('?', *a) sys.stdout.flush() p, v = map(int, input().split()) return p, v n, k = map(int, input().split()) a = list(range(1, k+1)) stack = [] for i in range(1, k+1): _, v = query(a) stack.append(v) a[-i] += 1 _, v = query(a) stack.append(v) stack.sort() print('!', stack.count(stack[-1])) sys.stdout.flush() ```
output
1
22,961
12
45,923
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is interactive. We have hidden an array a of n pairwise different numbers (this means that no two numbers are equal). You can get some information about this array using a new device you just ordered on Amazon. This device can answer queries of the following form: in response to the positions of k different elements of the array, it will return the position and value of the m-th among them in the ascending order. Unfortunately, the instruction for the device was lost during delivery. However, you remember k, but don't remember m. Your task is to find m using queries to this device. You can ask not more than n queries. Note that the array a and number m are fixed before the start of the interaction and don't depend on your queries. In other words, interactor is not adaptive. Note that you don't have to minimize the number of queries, and you don't need to guess array a. You just have to guess m. Input The first line contains two integers n and k (1≀ k < n ≀ 500) β€” the length of the array and the number of the elements in the query. It is guaranteed that number m satisfies 1≀ m ≀ k, elements a_1, a_2, ..., a_n of the array satisfy 0≀ a_i ≀ 10^9, and all of them are different. Interaction You begin the interaction by reading n and k. To ask a question about elements on positions x_1, x_2, ..., x_k, in a separate line output ? x_1 x_2 x_3 ... x_k Numbers in the query have to satisfy 1 ≀ x_i ≀ n, and all x_i have to be different. Don't forget to 'flush', to get the answer. In response, you will receive two integers pos and a_{pos} β€” the position in the array a of the m-th in ascending order element among a_{x_1}, a_{x_2}, ..., a_{x_k}, and the element on this position. In case your query is invalid or you asked more than n queries, the program will print -1 and will finish interaction. You will receive a Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. When you determine m, output ! m After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see documentation for other languages. Hack format For the hacks use the following format: The first line has to contain three integers n, k, m (1 ≀ m ≀ k < n ≀ 500) β€” the length of the array, number of elements in the query, and which in the ascending order number the device returns. In the next line output n integers a_1, a_2, ..., a_n (0≀ a_i ≀ 10^9) β€” the elements of the array. They have to be pairwise different. Example Input 4 3 4 9 4 9 4 9 1 2 Output ? 2 3 4 ? 1 3 4 ? 1 2 4 ? 1 2 3 ! 3 Note In the example, n = 4, k = 3, m = 3, a = [2, 0, 1, 9].
instruction
0
22,962
12
45,924
Tags: constructive algorithms, interactive, math, sortings Correct Solution: ``` from sys import stdout from collections import defaultdict d = defaultdict(int) n, k = map(int, input().split()) for i in range(1, k+2): array = ['?'] for j in range(1, k+2): if j != i: array.append(j) print(*array) stdout.flush() pos, apos = map(int, input().split()) d[apos]+=1 element = max(d.keys()) print('!', d[element]) ```
output
1
22,962
12
45,925
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is interactive. We have hidden an array a of n pairwise different numbers (this means that no two numbers are equal). You can get some information about this array using a new device you just ordered on Amazon. This device can answer queries of the following form: in response to the positions of k different elements of the array, it will return the position and value of the m-th among them in the ascending order. Unfortunately, the instruction for the device was lost during delivery. However, you remember k, but don't remember m. Your task is to find m using queries to this device. You can ask not more than n queries. Note that the array a and number m are fixed before the start of the interaction and don't depend on your queries. In other words, interactor is not adaptive. Note that you don't have to minimize the number of queries, and you don't need to guess array a. You just have to guess m. Input The first line contains two integers n and k (1≀ k < n ≀ 500) β€” the length of the array and the number of the elements in the query. It is guaranteed that number m satisfies 1≀ m ≀ k, elements a_1, a_2, ..., a_n of the array satisfy 0≀ a_i ≀ 10^9, and all of them are different. Interaction You begin the interaction by reading n and k. To ask a question about elements on positions x_1, x_2, ..., x_k, in a separate line output ? x_1 x_2 x_3 ... x_k Numbers in the query have to satisfy 1 ≀ x_i ≀ n, and all x_i have to be different. Don't forget to 'flush', to get the answer. In response, you will receive two integers pos and a_{pos} β€” the position in the array a of the m-th in ascending order element among a_{x_1}, a_{x_2}, ..., a_{x_k}, and the element on this position. In case your query is invalid or you asked more than n queries, the program will print -1 and will finish interaction. You will receive a Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. When you determine m, output ! m After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see documentation for other languages. Hack format For the hacks use the following format: The first line has to contain three integers n, k, m (1 ≀ m ≀ k < n ≀ 500) β€” the length of the array, number of elements in the query, and which in the ascending order number the device returns. In the next line output n integers a_1, a_2, ..., a_n (0≀ a_i ≀ 10^9) β€” the elements of the array. They have to be pairwise different. Example Input 4 3 4 9 4 9 4 9 1 2 Output ? 2 3 4 ? 1 3 4 ? 1 2 4 ? 1 2 3 ! 3 Note In the example, n = 4, k = 3, m = 3, a = [2, 0, 1, 9].
instruction
0
22,963
12
45,926
Tags: constructive algorithms, interactive, math, sortings Correct Solution: ``` from sys import stdout from collections import defaultdict def make_query(l): s = ["?"] for i in l: s.append(str(i)) print(" ".join(s)) stdout.flush() idx,element = map(int, input().split()) return idx,element n, k = map(int, input().split()) respuestas = defaultdict(int) for j in range(1,k+2): idx,element = make_query([i for i in range(1,k+2) if i!=j]) respuestas[(element,idx)]+=1 print("! {}".format(respuestas[max(respuestas)])) stdout.flush() ```
output
1
22,963
12
45,927
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is interactive. We have hidden an array a of n pairwise different numbers (this means that no two numbers are equal). You can get some information about this array using a new device you just ordered on Amazon. This device can answer queries of the following form: in response to the positions of k different elements of the array, it will return the position and value of the m-th among them in the ascending order. Unfortunately, the instruction for the device was lost during delivery. However, you remember k, but don't remember m. Your task is to find m using queries to this device. You can ask not more than n queries. Note that the array a and number m are fixed before the start of the interaction and don't depend on your queries. In other words, interactor is not adaptive. Note that you don't have to minimize the number of queries, and you don't need to guess array a. You just have to guess m. Input The first line contains two integers n and k (1≀ k < n ≀ 500) β€” the length of the array and the number of the elements in the query. It is guaranteed that number m satisfies 1≀ m ≀ k, elements a_1, a_2, ..., a_n of the array satisfy 0≀ a_i ≀ 10^9, and all of them are different. Interaction You begin the interaction by reading n and k. To ask a question about elements on positions x_1, x_2, ..., x_k, in a separate line output ? x_1 x_2 x_3 ... x_k Numbers in the query have to satisfy 1 ≀ x_i ≀ n, and all x_i have to be different. Don't forget to 'flush', to get the answer. In response, you will receive two integers pos and a_{pos} β€” the position in the array a of the m-th in ascending order element among a_{x_1}, a_{x_2}, ..., a_{x_k}, and the element on this position. In case your query is invalid or you asked more than n queries, the program will print -1 and will finish interaction. You will receive a Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. When you determine m, output ! m After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see documentation for other languages. Hack format For the hacks use the following format: The first line has to contain three integers n, k, m (1 ≀ m ≀ k < n ≀ 500) β€” the length of the array, number of elements in the query, and which in the ascending order number the device returns. In the next line output n integers a_1, a_2, ..., a_n (0≀ a_i ≀ 10^9) β€” the elements of the array. They have to be pairwise different. Example Input 4 3 4 9 4 9 4 9 1 2 Output ? 2 3 4 ? 1 3 4 ? 1 2 4 ? 1 2 3 ! 3 Note In the example, n = 4, k = 3, m = 3, a = [2, 0, 1, 9].
instruction
0
22,964
12
45,928
Tags: constructive algorithms, interactive, math, sortings Correct Solution: ``` import sys def solution(): n, k = [int(i) for i in input().strip().split()] foo = '? ' + ' '.join([str(i + 1) for i in range(k)]) + ' ' bar = ' ' + str(k + 1) + ' ' sys.stdout.write(foo.strip() + '\n') sys.stdout.flush() p, w = [int(i) - 1 for i in input().strip().split()] lt, eq, gt = 0, 0, 0 for i in range(k): if i == p: continue sys.stdout.write(foo.replace(' ' + str(i + 1) + ' ', bar).strip() + '\n') sys.stdout.flush() q, v = [int(i) - 1 for i in input().strip().split()] if v < w: lt += 1 elif v > w: gt += 1 else: eq += 1 if lt != 0: sys.stdout.write('! %d\n' % (k - lt)) sys.stdout.flush() return elif gt!= 0: sys.stdout.write('! %d\n' % (gt + 1)) sys.stdout.flush() return sys.stdout.write(foo.replace(' ' + str(p + 1) + ' ', bar).strip() + '\n') q, v = [int(i) - 1 for i in input().strip().split()] if v < w: sys.stdout.write('! %d\n' % (k)) sys.stdout.flush() else: sys.stdout.write('! %d\n' % (1)) sys.stdout.flush() def main(): solution() if __name__ == '__main__': main() ```
output
1
22,964
12
45,929
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is interactive. We have hidden an array a of n pairwise different numbers (this means that no two numbers are equal). You can get some information about this array using a new device you just ordered on Amazon. This device can answer queries of the following form: in response to the positions of k different elements of the array, it will return the position and value of the m-th among them in the ascending order. Unfortunately, the instruction for the device was lost during delivery. However, you remember k, but don't remember m. Your task is to find m using queries to this device. You can ask not more than n queries. Note that the array a and number m are fixed before the start of the interaction and don't depend on your queries. In other words, interactor is not adaptive. Note that you don't have to minimize the number of queries, and you don't need to guess array a. You just have to guess m. Input The first line contains two integers n and k (1≀ k < n ≀ 500) β€” the length of the array and the number of the elements in the query. It is guaranteed that number m satisfies 1≀ m ≀ k, elements a_1, a_2, ..., a_n of the array satisfy 0≀ a_i ≀ 10^9, and all of them are different. Interaction You begin the interaction by reading n and k. To ask a question about elements on positions x_1, x_2, ..., x_k, in a separate line output ? x_1 x_2 x_3 ... x_k Numbers in the query have to satisfy 1 ≀ x_i ≀ n, and all x_i have to be different. Don't forget to 'flush', to get the answer. In response, you will receive two integers pos and a_{pos} β€” the position in the array a of the m-th in ascending order element among a_{x_1}, a_{x_2}, ..., a_{x_k}, and the element on this position. In case your query is invalid or you asked more than n queries, the program will print -1 and will finish interaction. You will receive a Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. When you determine m, output ! m After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see documentation for other languages. Hack format For the hacks use the following format: The first line has to contain three integers n, k, m (1 ≀ m ≀ k < n ≀ 500) β€” the length of the array, number of elements in the query, and which in the ascending order number the device returns. In the next line output n integers a_1, a_2, ..., a_n (0≀ a_i ≀ 10^9) β€” the elements of the array. They have to be pairwise different. Example Input 4 3 4 9 4 9 4 9 1 2 Output ? 2 3 4 ? 1 3 4 ? 1 2 4 ? 1 2 3 ! 3 Note In the example, n = 4, k = 3, m = 3, a = [2, 0, 1, 9].
instruction
0
22,965
12
45,930
Tags: constructive algorithms, interactive, math, sortings Correct Solution: ``` import sys def print(s = "", end = "\n"): sys.stdout.write(str(s)+end) def input(): return sys.stdin.readline().rstrip() n, k = map(int,input().split()) ans = [] for i in range(k + 1): print("?", end = " ") for j in range(k + 1): if j != i: print(j + 1, end = " ") print() sys.stdout.flush() ans.append(list(map(int,input().split()))[1]) maxn = -1 app = 0 for i in ans: if i > maxn: maxn = i app = 1 elif i == maxn: app += 1 print("! " + str(app)) sys.stdout.flush() ```
output
1
22,965
12
45,931
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is interactive. We have hidden an array a of n pairwise different numbers (this means that no two numbers are equal). You can get some information about this array using a new device you just ordered on Amazon. This device can answer queries of the following form: in response to the positions of k different elements of the array, it will return the position and value of the m-th among them in the ascending order. Unfortunately, the instruction for the device was lost during delivery. However, you remember k, but don't remember m. Your task is to find m using queries to this device. You can ask not more than n queries. Note that the array a and number m are fixed before the start of the interaction and don't depend on your queries. In other words, interactor is not adaptive. Note that you don't have to minimize the number of queries, and you don't need to guess array a. You just have to guess m. Input The first line contains two integers n and k (1≀ k < n ≀ 500) β€” the length of the array and the number of the elements in the query. It is guaranteed that number m satisfies 1≀ m ≀ k, elements a_1, a_2, ..., a_n of the array satisfy 0≀ a_i ≀ 10^9, and all of them are different. Interaction You begin the interaction by reading n and k. To ask a question about elements on positions x_1, x_2, ..., x_k, in a separate line output ? x_1 x_2 x_3 ... x_k Numbers in the query have to satisfy 1 ≀ x_i ≀ n, and all x_i have to be different. Don't forget to 'flush', to get the answer. In response, you will receive two integers pos and a_{pos} β€” the position in the array a of the m-th in ascending order element among a_{x_1}, a_{x_2}, ..., a_{x_k}, and the element on this position. In case your query is invalid or you asked more than n queries, the program will print -1 and will finish interaction. You will receive a Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. When you determine m, output ! m After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see documentation for other languages. Hack format For the hacks use the following format: The first line has to contain three integers n, k, m (1 ≀ m ≀ k < n ≀ 500) β€” the length of the array, number of elements in the query, and which in the ascending order number the device returns. In the next line output n integers a_1, a_2, ..., a_n (0≀ a_i ≀ 10^9) β€” the elements of the array. They have to be pairwise different. Example Input 4 3 4 9 4 9 4 9 1 2 Output ? 2 3 4 ? 1 3 4 ? 1 2 4 ? 1 2 3 ! 3 Note In the example, n = 4, k = 3, m = 3, a = [2, 0, 1, 9].
instruction
0
22,966
12
45,932
Tags: constructive algorithms, interactive, math, sortings Correct Solution: ``` from sys import stdout n, k = map(int, input().split()) n1, n2 = 0, 0 e1, e2 = -1, -1 elems = [i + 1 for i in range(k + 1)] for i in range(k + 1): print('?', *elems[:i], *elems[i + 1:k + 1]) stdout.flush() pos, elem = map(int, input().split()) if elem > e1: e1, e2 = elem, e1 n1, n2 = 1, n1 elif elem == e1: n1 += 1 elif elem > e2: e2 = elem n2 = 1 elif elem == e2: n2 += 1 print('!', n1) stdout.flush() ```
output
1
22,966
12
45,933
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is interactive. We have hidden an array a of n pairwise different numbers (this means that no two numbers are equal). You can get some information about this array using a new device you just ordered on Amazon. This device can answer queries of the following form: in response to the positions of k different elements of the array, it will return the position and value of the m-th among them in the ascending order. Unfortunately, the instruction for the device was lost during delivery. However, you remember k, but don't remember m. Your task is to find m using queries to this device. You can ask not more than n queries. Note that the array a and number m are fixed before the start of the interaction and don't depend on your queries. In other words, interactor is not adaptive. Note that you don't have to minimize the number of queries, and you don't need to guess array a. You just have to guess m. Input The first line contains two integers n and k (1≀ k < n ≀ 500) β€” the length of the array and the number of the elements in the query. It is guaranteed that number m satisfies 1≀ m ≀ k, elements a_1, a_2, ..., a_n of the array satisfy 0≀ a_i ≀ 10^9, and all of them are different. Interaction You begin the interaction by reading n and k. To ask a question about elements on positions x_1, x_2, ..., x_k, in a separate line output ? x_1 x_2 x_3 ... x_k Numbers in the query have to satisfy 1 ≀ x_i ≀ n, and all x_i have to be different. Don't forget to 'flush', to get the answer. In response, you will receive two integers pos and a_{pos} β€” the position in the array a of the m-th in ascending order element among a_{x_1}, a_{x_2}, ..., a_{x_k}, and the element on this position. In case your query is invalid or you asked more than n queries, the program will print -1 and will finish interaction. You will receive a Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. When you determine m, output ! m After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see documentation for other languages. Hack format For the hacks use the following format: The first line has to contain three integers n, k, m (1 ≀ m ≀ k < n ≀ 500) β€” the length of the array, number of elements in the query, and which in the ascending order number the device returns. In the next line output n integers a_1, a_2, ..., a_n (0≀ a_i ≀ 10^9) β€” the elements of the array. They have to be pairwise different. Example Input 4 3 4 9 4 9 4 9 1 2 Output ? 2 3 4 ? 1 3 4 ? 1 2 4 ? 1 2 3 ! 3 Note In the example, n = 4, k = 3, m = 3, a = [2, 0, 1, 9].
instruction
0
22,967
12
45,934
Tags: constructive algorithms, interactive, math, sortings Correct Solution: ``` from sys import stdout n,k = list(map(int,input().split())) value1,value2 = -1,-1 count1,count2=0,0 for i in range(1,k+2): query = ["?"] for j in range(1,k+2): if i!=j: query.append(j) print(*query) stdout.flush() a,b = list(map(int,input().split())) if i==1: value1 = b elif value2==-1 and b!=value1: value2=b if value1==b: count1+=1 elif value2==b: count2+=1 if value1>value2: count1,count2=count2,count1 print("!",k+1-count1) ```
output
1
22,967
12
45,935
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
23,008
12
46,016
Tags: brute force, data structures, dp, two pointers Correct Solution: ``` # cook your dish here from sys import stdin import sys if __name__=="__main__": for _ in range (int(stdin.readline())): n=int(stdin.readline()) arr=list(map(int,stdin.readline().split())) dp=[[0]*(n+1) for i in range (200)] pos=[[] for i in range (200)] # print(pos) for i in range (n): for c in range (200): dp[c][i+1]=dp[c][i] dp[arr[i]-1][i+1]+=1 pos[arr[i]-1].append(i) # print(dp) ans=0 for i in range (200): ans=max(ans,len(pos[i])) for j in range (len(pos[i])//2): l=pos[i][j]+1 r=pos[i][len(pos[i])-j-1]-1 for k in range (200): res=dp[k][r+1]-dp[k][l] ans=max(ans,(2*(j+1))+res) print(ans) ```
output
1
23,008
12
46,017
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
23,009
12
46,018
Tags: brute force, data structures, dp, two pointers Correct Solution: ``` # import bisect # import os # import io # from collections import Counter import bisect from collections import defaultdict # import math # import random # import heapq as hq # from math import sqrt import sys from functools import reduce, cmp_to_key from collections import deque import threading from itertools import combinations # sys.setrecursionlimit(200000) # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def tinput(): return input().split() def rinput(): return map(int, tinput()) def rlinput(): return list(rinput()) # mod = int(1e9)+7 def factors(n): return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))) # ---------------------------------------------------- # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') if __name__ == "__main__": for _ in range(iinput()): n = iinput() a = rlinput() d = defaultdict(list) ll = [[0 for i in range(201)] for i in range(n+1)] for i in range(n): d[a[i]].append(i) for k in range(1, 201): ll[i][k] = ll[i-1][k] ll[i][a[i]] += 1 # print(ll[7][2]) ans = 1 for i in d: temp = d[i] l = len(temp) ans = max(ans, l) # print(i) for j in range(l//2): start = temp[j] end = temp[l - j - 1] for k in d: if k != i: val = ll[end][k] - ll[start-1][k] ans = max(ans, val+2*(j+1)) print(ans) ```
output
1
23,009
12
46,019
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
23,010
12
46,020
Tags: brute force, data structures, dp, two pointers Correct Solution: ``` from collections import defaultdict def solve(): n = int(input()) a = list(map(int, input().split())) V = max(a) #a[i]<=200 ans = 0 cnt = [[0]*(n+1) for i in range(V+1)] for i, v in enumerate(a): cnt[v][i+1] = 1 for i in range(1, n+1): for j in range(V+1): cnt[j][i] += cnt[j][i-1] subcnt = [defaultdict(int) for i in range(V+1)] appear_cnt = [0]*(V+1) for i, v in enumerate(a): subcnt[v][appear_cnt[v]+1] = i+1 appear_cnt[v] += 1 for x in range(1, V+1): ans = max(ans, cnt[x][n]) if cnt[x][n] == 0: continue xcnt = cnt[x][n] for c in range(1, xcnt//2+1): lx = subcnt[x][c] rx = subcnt[x][xcnt-c+1] #print(x,y,"xcnt:",xcnt,"lx,rx:",lx,rx) ans = max(ans, 2*c + max(cnt[y][rx-1]-cnt[y][lx] for y in range(V+1))) print(ans) return def main(): t = int(input()) for i in range(t): solve() return if __name__ == "__main__": main() ```
output
1
23,010
12
46,021
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
23,011
12
46,022
Tags: brute force, data structures, dp, two pointers Correct Solution: ``` import sys input=sys.stdin.readline t=int(input()) for i in range(t): n=int(input()) a=[int(i)-1 for i in input().split()] dp=[[0 for _ in range(200)] for _ in range(n)] for i in range(n): for j in range(200): dp[i][j]=dp[i-1][j] dp[i][a[i]]+=1 #print(dp) ans=1 for i in range(200): l,r,count=0,n-1,0 while l<r: while l<n and a[l]!=i: l+=1 while r>-1 and a[r]!=i: r-=1 if l<r: count+=2 for j in range(200): ans=max(ans,count+dp[r-1][j]-dp[l][j]) l+=1 r-=1 sys.stdout.write(str(ans)+'\n') ```
output
1
23,011
12
46,023
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
23,012
12
46,024
Tags: brute force, data structures, dp, two pointers Correct Solution: ``` #import io, os #input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline import sys input=sys.stdin.readline MOD = 1000000007 MOD2 = 998244353 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int,input())) f = lambda: map(int, input().split()) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) let = 'abcdefghijklmnopqrstuvwxyz' for _ in range(ii()): n=ii() l=il() cntr=dict() mxcnt = 0 dp=[[0 for i in range(201)] for j in range(n+1)] for i in range(n-1,-1,-1): for j in range(1,201): dp[i][j]=dp[i+1][j]+(l[i]==j) mxcnt=max(dp[i][l[i]],mxcnt) x=str(dp[i][l[i]])+','+str(l[i]) cntr[x]=i cntr2=dict() for i in range(n): cntr2[l[i]]=cntr2.get(l[i],0)+1 x=str(cntr2[l[i]])+','+str(l[i]) if x in cntr and cntr[x]>i: for j in range(1,201): mxcnt=max(cntr2[l[i]]*2+(dp[i+1][j]-dp[cntr[x]][j]),mxcnt) print(mxcnt) ```
output
1
23,012
12
46,025
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
23,013
12
46,026
Tags: brute force, data structures, dp, two pointers Correct Solution: ``` import sys import collections import threading import string def sparsetable(a): tmp = [0]*201 st = [] for i in range(len(a)): tmp[a[i]] += 1 st.append(tmp[:]) return st def func(x, y, st): if x>=y: return 0 ans = 0 for i in range(201): ans = max(ans, st[y][i] - st[x][i] ) return ans def main(): testn = int(input()) for _ in range(testn): n = int( input() ) a = list(map(int, input().split() )) d = collections.defaultdict(list) st = sparsetable(a) for i in range(n): d[ a[i] ].append(i) ans = 1 for key in d: z = len(d[key]) l = d[key] for i in range(z//2): ans = max(ans, (i+1)*2 + func(l[i], l[z-1-i]-1, st)) print(ans) input = sys.stdin.readline main() ```
output
1
23,013
12
46,027
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
23,014
12
46,028
Tags: brute force, data structures, dp, two pointers Correct Solution: ``` #!/usr/bin/env python import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") t = int(input()) for _ in range(t): n = int(input()) arr = [int(x) for x in input().split()] counters = [[] for _ in range(len(arr))] counter = [0 for _ in range(201)] mp = dict() for idx in range(len(arr)): counter[arr[idx]] += 1 counters[idx] = counter.copy() mp[(arr[idx], counter[arr[idx]])] = idx ans = 1 for idx_v in range(len(arr)): v = arr[idx_v] occ_v = counters[idx_v][v] total_occs_v = counters[-1][v] symmetric_idx_v = mp[(v, total_occs_v - occ_v + 1)] if idx_v >= symmetric_idx_v: continue mx = max([second - first for first, second in zip(counters[idx_v], counters[symmetric_idx_v - 1])]) + occ_v * 2 ans = max(ans, mx) print(ans) ```
output
1
23,014
12
46,029
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3
instruction
0
23,015
12
46,030
Tags: brute force, data structures, dp, two pointers Correct Solution: ``` # this problem is solved with the help of tutorial. def sm(c,l,r): return occur[c][r+1]-occur[c][l] t=int(input()) for z in range(t): n=int(input()) arr=list(map(int,input().split())) brr=list(set(arr)) pos=[[] for i in range(201)] occur=[[] for i in range(201)] for num in brr: occur[num].append(0) count=0 for i in range(n): if arr[i]==num: pos[num].append(i) count=count+1 occur[num].append(count) ans=0 for num in brr: ans=max(ans,occur[num][n]) ln=len(pos[num]) for k in range(ln//2): l=pos[num][k]+1 r=pos[num][ln-k-1]-1 cntin=0 for nm in brr: cntin=max(cntin,sm(nm,l,r)) ans=max(ans,2*(k+1) + cntin) print(ans) ```
output
1
23,015
12
46,031
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` # ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) st = [] indexes = [[] for __ in range(200)] for i in range(n): indexes[a[i]-1] += [i] for i in range(1,201): b = [0]*n b[0] = 0 if a[0] != i else 1 for j in range(1,n): b[j] = b[j-1]+1 if a[j] == i else b[j-1] st += [b] ans = 0 for i in range(200): ans = max(ans, st[i][n-1]) for ind in range(200): for i in range(len(indexes[ind])//2): start, end = indexes[ind][i]+1, indexes[ind][-1-i] extra = 0 for j in range(200): if start != 0: extra = max(extra, st[j][end-1] - st[j][start-1]) ans = max(ans, extra + 2*(i+1)) print(ans) ```
instruction
0
23,016
12
46,032
Yes
output
1
23,016
12
46,033
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline MOD = 1000000007 MOD2 = 998244353 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int,input())) f = lambda: map(int, input().split()) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) let = 'abcdefghijklmnopqrstuvwxyz' for _ in range(ii()): n=ii() l=il() cntr=dict() mxcnt = 0 dp=[[0 for i in range(201)] for j in range(n+1)] for i in range(n-1,-1,-1): for j in range(1,201): dp[i][j]=dp[i+1][j]+(l[i]==j) mxcnt=max(dp[i][l[i]],mxcnt) x=str(dp[i][l[i]])+','+str(l[i]) cntr[x]=i cntr2=dict() for i in range(n): cntr2[l[i]]=cntr2.get(l[i],0)+1 x=str(cntr2[l[i]])+','+str(l[i]) if x in cntr and cntr[x]>i: for j in range(1,201): mxcnt=max(cntr2[l[i]]*2+(dp[i+1][j]-dp[cntr[x]][j]),mxcnt) print(mxcnt) ```
instruction
0
23,017
12
46,034
Yes
output
1
23,017
12
46,035
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` import sys input = sys.stdin.readline from bisect import bisect_left Q = int(input()) Query = [] for _ in range(Q): N = int(input()) A = list(map(int, input().split())) Query.append((N, A)) for N, A in Query: dic = {} for i, a in enumerate(A): if a in dic: dic[a].append(i) else: dic[a] = [i] Ns = list(dic.keys()) ans = 0 for a, A1 in dic.items(): ans = max(ans, len(A1)) for b, B1 in dic.items(): if a == b: continue l1 = 0; r1 = len(A1)-1 l2 = 0; r2 = len(B1)-1 while l1 < r1: while l2 <= len(B1)-1 and B1[l2] < A1[l1]: l2 += 1 while r2 >= 0 and B1[r2] > A1[r1]: r2 -= 1 t = (l1+1)*2+(r2-l2+1) if t > ans: ans = t l1 += 1 r1 -= 1 print(ans) ```
instruction
0
23,018
12
46,036
Yes
output
1
23,018
12
46,037
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` numbers = 200 for _ in range(int(input())): n = int(input()) seq = [int(x)-1 for x in input().split()] #TODO pre = [(n+1)*[0] for i in range(numbers)] pos = [[] for i in range(numbers)] for j, val in enumerate(seq, 1): for i in range(numbers): if val == i: pre[i][j] = pre[i][j-1] + 1 pos[i].append(j) else: pre[i][j] = pre[i][j-1] ans = 1 for i in range(numbers): # for x value max_val = pre[i][-1] // 2 for x in range(max_val): # for min x len # bmin = pre[i].index(x) # leftmost # bmax = n+1-pre[i][::-1].index(pre[i][-1]-x) # right most # print(x, pos[i]) bmin = pos[i][x] # leftmost bmax = pos[i][-x-1] # rightmost # print(pre[i],x,bmin, bmax) ymax = 0 if bmax - bmin > 1: for j in range(numbers): # for y ymax = max(ymax, pre[j][bmax-1] - pre[j][bmin]) # print('sol', i,x,ymax) ans = max(ans, ymax + 2*(x+1)) # print("ANS ", end='') print(ans) ```
instruction
0
23,019
12
46,038
Yes
output
1
23,019
12
46,039
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` numbers = 200 for _ in range(int(input())): n = int(input()) seq = [int(x)-1 for x in input().split()] #TODO pre = [(n+1)*[0] for i in range(numbers)] for j, val in enumerate(seq, 1): for i in range(numbers): if val == i: pre[i][j] = pre[i][j-1] + 1 else: pre[i][j] = pre[i][j-1] ans = 1 for i in range(numbers): # for x value max_val = pre[i][-1] // 2 for x in range(1, 1+max_val): # for min x len bmin = pre[i].index(x) # leftmost bmax = n+1-pre[i][::-1].index(pre[i][-1]-x) # right most # print(pre[i],x,bmin, bmax) ymax = 0 if bmax - bmin > 1: for j in range(numbers): # for y ymax = max(ymax, pre[j][bmax-1] - pre[j][bmin]) else: continue # print('sol', i,x,ymax) ans = max(ans, ymax + 2*x) # print("ANS ", end='') print(ans) ```
instruction
0
23,020
12
46,040
No
output
1
23,020
12
46,041
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ from math import factorial from collections import Counter, defaultdict from heapq import heapify, heappop, heappush def RL(): return map(int, sys.stdin.readline().rstrip().split()) def N(): return int(input()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) mod = 1000000007 INF = float('inf') # ------------------------------ def main(): for _ in range(N()): n = N() arr = list(RL()) sm = [[0]*201] if len(set(arr))==1: print(len(arr)) else: for i in arr: now = sm[-1].copy() now[i]+=1 sm.append(now) res = 0 for i in range(n): for j in range(i, n): if arr[i]!=arr[j]: continue pre = sm[i] ind = [sm[j+1][ii]-sm[i][ii] for ii in range(201)] suf = [sm[-1][ii]-sm[j+1][ii] for ii in range(201)] mp = 0 for k in range(1, 201): mp = max(mp, min(pre[k], suf[k])) res = max(res, max(ind)+mp*2) if n-i<res: break print(res) if __name__ == "__main__": main() ```
instruction
0
23,021
12
46,042
No
output
1
23,021
12
46,043
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` for _ in range(int(input())): n=int(input()) a=list(map(lambda x: int(x)-1,input().split())) idx=[[] for _ in range(200)] for i in range(len(a)): idx[a[i]].append(i) mxl=1 dp=[[0]*(len(a)) for _ in range(200)] for i in range(len(a)): for j in range(200): dp[j][i]=dp[j][i-1] dp[a[i]][i]+=1 for r in range(200): for c in range(r+1,200): if len(idx[r])==0: break if len(idx[c])==0: continue mxl=max(mxl,len(idx[r]),len(idx[c])) for i in idx[r]: for j in idx[c]: if dp[a[i]][i]>0 and dp[a[j]][j]-dp[a[j]][i]>0 and dp[a[i]][-1]-dp[a[i]][j]>0: mxl=max(2*min(dp[a[i]][i],dp[a[i]][-1]-dp[a[i]][j])+dp[a[j]][j]-dp[a[j]][i],mxl) # for j in range(i+1,len(a)): # if a[i]==a[j]: # mxl=max(mxl,dp[a[i]][-1]) # continue # if dp[a[i]][i]>0 and dp[a[j]][j]-dp[a[j]][i]>0 and dp[a[i]][-1]-dp[a[i]][j]>0: # mxl=max(2*min(dp[a[i]][i],dp[a[i]][-1]-dp[a[i]][j])+dp[a[j]][j]-dp[a[j]][i],mxl) print(mxl) ```
instruction
0
23,022
12
46,044
No
output
1
23,022
12
46,045
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is constraints. You are given a sequence a consisting of n positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not. Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome. You have to answer t independent test cases. Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2]. Input The first line of the input contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 200), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 200. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5 (βˆ‘ n ≀ 2 β‹… 10^5). Output For each test case, print the answer β€” the maximum possible length of some subsequence of a that is a three blocks palindrome. Example Input 6 8 1 1 2 2 3 2 1 1 3 1 3 3 4 1 10 10 1 1 26 2 2 1 3 1 1 1 Output 7 2 4 1 1 3 Submitted Solution: ``` import sys import math input = sys.stdin.readline from functools import cmp_to_key; def pi(): return(int(input())) def pl(): return(int(input(), 16)) def ti(): return(list(map(int,input().split()))) def ts(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) mod = 1000000007; f = []; def fact(n,m): global f; f = [1 for i in range(n+1)]; f[0] = 1; for i in range(1,n+1): f[i] = (f[i-1]*i)%m; def fast_mod_exp(a,b,m): res = 1; while b > 0: if b & 1: res = (res*a)%m; a = (a*a)%m; b = b >> 1; return res; def inverseMod(n,m): return fast_mod_exp(n,m-2,m); def ncr(n,r,m): if r == 0: return 1; return ((f[n]*inverseMod(f[n-r],m))%m*inverseMod(f[r],m))%m; def main(): E2(); def xdfs(root, v, sub, parent): st = [root]; while len(st) > 0: node = st.pop(); for i in range(len(v[node])): if v[node][i] != node: st.append(v[node][i]); def X(): try: t = pi(); while t: t -= 1; n = pi(); v = [[] for i in range(n)]; for i in range(n-1): [x,y] = ti(); v[x-1].append(y-1); v[y-1].append(x-1); m = pi(); p = ti(); e = [0 for i in range(n-1)]; sub = [0 for i in range(n)]; dfs(0,v,sub,-1) for i in range(1,n): e[i-1] = (sub[i]*(n-sub[i])); if len(p) < n-1: while len(p) < n-1: p.append(1); p = sorted(p); if len(p) > n-1: x = 1; for i in range(n-2,len(p)): x = (x*p[i]); while len(p) > n-2: p.pop(); p.append(x); e = sorted(e); res = 0; for i in range(n-1): res = (res+(p[i]*e[i]))%mod; print(res); except: print(sys.exc_info()); def bfs(v,root): q = [root,None]; l = 0; visited = [0 for i in range(len(v))]; dist = [0 for i in range(len(v))]; while len(q) > 0: node = q.pop(0); if node is not None: visited[node] = 1; dist[node] = l; for i in range(len(v[node])): if visited[v[node][i]] == 0: visited[v[node][i]] = 1; q.append(v[node][i]); else: l += 1; if len(q) != 0: q.append(None); return dist; def B(): n = pi(); v = [[] for i in range(n)]; for i in range(n-1): [x,y] = ti(); v[x-1].append(y-1); v[y-1].append(x-1); leafs = []; for i in range(n): if len(v[i]) == 1: leafs.append(i); mn = 1; d = bfs(v,leafs[0]); for i in range(1,len(leafs)): if d[leafs[i]] % 2 != 0: mn = 3; break; count = 0; for i in range(n): f = 0; for j in range(len(v[i])): if len(v[v[i][j]]) == 1: f = 1; break; if f: count += 1; mx = n-1-len(leafs)+count; print(mn,mx) def E2(): t = pi(); while t: t -= 1; n,a = pi(),ti(); if n == 1: print(1); continue; pz = [[] for i in range(201)]; counts = [[0 for j in range(n)] for i in range(201)]; for i in range(201): for j in range(n): if a[j] == i: counts[i][j] = counts[i][j-1] + 1 if j != 0 else 1; pz[i].append(j); else: counts[i][j] = counts[i][j-1] if j != 0 else 0; res = 0; for i in range(201): for sz in range(1,n+1): if len(pz[i]) >= 2*sz: st = pz[i][sz-1]; en = pz[i][len(pz[i])-sz]; mx = 0; for j in range(201): mx = max(mx, counts[j][en-1]-counts[j][st]); res = max(res,mx+2*sz); print(res); main(); ```
instruction
0
23,023
12
46,046
No
output
1
23,023
12
46,047
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Yura owns a quite ordinary and boring array a of length n. You think there is nothing more boring than that, but Vladik doesn't agree! In order to make Yura's array even more boring, Vladik makes q boring queries. Each query consists of two integers x and y. Before answering a query, the bounds l and r for this query are calculated: l = (last + x) mod n + 1, r = (last + y) mod n + 1, where last is the answer on the previous query (zero initially), and mod is the remainder operation. Whenever l > r, they are swapped. After Vladik computes l and r for a query, he is to compute the least common multiple (LCM) on the segment [l; r] of the initial array a modulo 10^9 + 7. LCM of a multiset of integers is the smallest positive integer that is divisible by all the elements of the multiset. The obtained LCM is the answer for this query. Help Vladik and compute the answer for each query! Input The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the length of the array. The second line contains n integers a_i (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the elements of the array. The third line contains a single integer q (1 ≀ q ≀ 10^5) β€” the number of queries. The next q lines contain two integers x and y each (1 ≀ x, y ≀ n) β€” the description of the corresponding query. Output Print q integers β€” the answers for the queries. Example Input 3 2 3 5 4 1 3 3 3 2 3 2 3 Output 6 2 15 30 Note Consider the example: * boundaries for first query are (0 + 1) mod 3 + 1 = 2 and (0 + 3) mod 3 + 1 = 1. LCM for segment [1, 2] is equal to 6; * boundaries for second query are (6 + 3) mod 3 + 1 = 1 and (6 + 3) mod 3 + 1 = 1. LCM for segment [1, 1] is equal to 2; * boundaries for third query are (2 + 2) mod 3 + 1 = 2 and (2 + 3) mod 3 + 1 = 3. LCM for segment [2, 3] is equal to 15; * boundaries for fourth query are (15 + 2) mod 3 + 1 = 3 and (15 + 3) mod 3 + 1 = 1. LCM for segment [1, 3] is equal to 30. Submitted Solution: ``` from functools import reduce def gcd(a, b): while(b): a, b = b, a % b return a def lcm(a, b): return abs(a * b) // gcd(a, b) n = int(input()) a = [int(s) for s in input().split()] q = int(input()) last = 0 for t in range(q): x, y = map(int, input().split()) l = (last+x) % n + 1 r = (last+y) % n + 1 if l > r: l, r = r, l res = reduce(lcm, a[l-1:r]) print(res) last = res ```
instruction
0
23,073
12
46,146
No
output
1
23,073
12
46,147
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Yura owns a quite ordinary and boring array a of length n. You think there is nothing more boring than that, but Vladik doesn't agree! In order to make Yura's array even more boring, Vladik makes q boring queries. Each query consists of two integers x and y. Before answering a query, the bounds l and r for this query are calculated: l = (last + x) mod n + 1, r = (last + y) mod n + 1, where last is the answer on the previous query (zero initially), and mod is the remainder operation. Whenever l > r, they are swapped. After Vladik computes l and r for a query, he is to compute the least common multiple (LCM) on the segment [l; r] of the initial array a modulo 10^9 + 7. LCM of a multiset of integers is the smallest positive integer that is divisible by all the elements of the multiset. The obtained LCM is the answer for this query. Help Vladik and compute the answer for each query! Input The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the length of the array. The second line contains n integers a_i (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the elements of the array. The third line contains a single integer q (1 ≀ q ≀ 10^5) β€” the number of queries. The next q lines contain two integers x and y each (1 ≀ x, y ≀ n) β€” the description of the corresponding query. Output Print q integers β€” the answers for the queries. Example Input 3 2 3 5 4 1 3 3 3 2 3 2 3 Output 6 2 15 30 Note Consider the example: * boundaries for first query are (0 + 1) mod 3 + 1 = 2 and (0 + 3) mod 3 + 1 = 1. LCM for segment [1, 2] is equal to 6; * boundaries for second query are (6 + 3) mod 3 + 1 = 1 and (6 + 3) mod 3 + 1 = 1. LCM for segment [1, 1] is equal to 2; * boundaries for third query are (2 + 2) mod 3 + 1 = 2 and (2 + 3) mod 3 + 1 = 3. LCM for segment [2, 3] is equal to 15; * boundaries for fourth query are (15 + 2) mod 3 + 1 = 3 and (15 + 3) mod 3 + 1 = 1. LCM for segment [1, 3] is equal to 30. Submitted Solution: ``` def gcd(a, b): if a == 0: return b if b == 0: return a if a==b: return b if b > a: a, b = b, a return gcd(b, a%b) last = 0 l = 0 r = 0 buf = 1 n = int(input()) arr = list(map(int, input().split())) q = int(input()) for i in range(q): x, y = map(int, input().split()) l = (last + x) % n r = (last + y) % n if l > r: l, r = r, l buf = arr[l] for j in range(l + 1, r + 1): buf = buf * arr[j] / gcd(buf, arr[j]) print(int(buf % 1000000007)) ```
instruction
0
23,074
12
46,148
No
output
1
23,074
12
46,149
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Yura owns a quite ordinary and boring array a of length n. You think there is nothing more boring than that, but Vladik doesn't agree! In order to make Yura's array even more boring, Vladik makes q boring queries. Each query consists of two integers x and y. Before answering a query, the bounds l and r for this query are calculated: l = (last + x) mod n + 1, r = (last + y) mod n + 1, where last is the answer on the previous query (zero initially), and mod is the remainder operation. Whenever l > r, they are swapped. After Vladik computes l and r for a query, he is to compute the least common multiple (LCM) on the segment [l; r] of the initial array a modulo 10^9 + 7. LCM of a multiset of integers is the smallest positive integer that is divisible by all the elements of the multiset. The obtained LCM is the answer for this query. Help Vladik and compute the answer for each query! Input The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the length of the array. The second line contains n integers a_i (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the elements of the array. The third line contains a single integer q (1 ≀ q ≀ 10^5) β€” the number of queries. The next q lines contain two integers x and y each (1 ≀ x, y ≀ n) β€” the description of the corresponding query. Output Print q integers β€” the answers for the queries. Example Input 3 2 3 5 4 1 3 3 3 2 3 2 3 Output 6 2 15 30 Note Consider the example: * boundaries for first query are (0 + 1) mod 3 + 1 = 2 and (0 + 3) mod 3 + 1 = 1. LCM for segment [1, 2] is equal to 6; * boundaries for second query are (6 + 3) mod 3 + 1 = 1 and (6 + 3) mod 3 + 1 = 1. LCM for segment [1, 1] is equal to 2; * boundaries for third query are (2 + 2) mod 3 + 1 = 2 and (2 + 3) mod 3 + 1 = 3. LCM for segment [2, 3] is equal to 15; * boundaries for fourth query are (15 + 2) mod 3 + 1 = 3 and (15 + 3) mod 3 + 1 = 1. LCM for segment [1, 3] is equal to 30. Submitted Solution: ``` n=int(input()) s=input().split() m=int(input()) pku=[] pre=0 for i in range(m): r=[] a,b=map(int,input().split()) tyb=(a+pre)%n+1 xhj=(b+pre)%n+1 twh=max(tyb,xhj) zzl=min(tyb,xhj) for i in range (zzl-1,twh): r.append(int(s[i])) xlk=min(r) x=[] for j in range (1,xlk+1): c=0 for item in r: if item%j==0: c=c+1 else: c=c if c==len(r): x.append(j) zxy=max(x) if len(r)==1: vb=r[0] pku.append(vb) if len(r)>1: sum=1 for i in range(len(r)): sum=sum*r[i] vb=sum//zxy pku.append(vb) pre=vb for item in pku: print(item) ```
instruction
0
23,075
12
46,150
No
output
1
23,075
12
46,151
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Yura owns a quite ordinary and boring array a of length n. You think there is nothing more boring than that, but Vladik doesn't agree! In order to make Yura's array even more boring, Vladik makes q boring queries. Each query consists of two integers x and y. Before answering a query, the bounds l and r for this query are calculated: l = (last + x) mod n + 1, r = (last + y) mod n + 1, where last is the answer on the previous query (zero initially), and mod is the remainder operation. Whenever l > r, they are swapped. After Vladik computes l and r for a query, he is to compute the least common multiple (LCM) on the segment [l; r] of the initial array a modulo 10^9 + 7. LCM of a multiset of integers is the smallest positive integer that is divisible by all the elements of the multiset. The obtained LCM is the answer for this query. Help Vladik and compute the answer for each query! Input The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the length of the array. The second line contains n integers a_i (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the elements of the array. The third line contains a single integer q (1 ≀ q ≀ 10^5) β€” the number of queries. The next q lines contain two integers x and y each (1 ≀ x, y ≀ n) β€” the description of the corresponding query. Output Print q integers β€” the answers for the queries. Example Input 3 2 3 5 4 1 3 3 3 2 3 2 3 Output 6 2 15 30 Note Consider the example: * boundaries for first query are (0 + 1) mod 3 + 1 = 2 and (0 + 3) mod 3 + 1 = 1. LCM for segment [1, 2] is equal to 6; * boundaries for second query are (6 + 3) mod 3 + 1 = 1 and (6 + 3) mod 3 + 1 = 1. LCM for segment [1, 1] is equal to 2; * boundaries for third query are (2 + 2) mod 3 + 1 = 2 and (2 + 3) mod 3 + 1 = 3. LCM for segment [2, 3] is equal to 15; * boundaries for fourth query are (15 + 2) mod 3 + 1 = 3 and (15 + 3) mod 3 + 1 = 1. LCM for segment [1, 3] is equal to 30. Submitted Solution: ``` from math import gcd n = int(input()) a = [int(i) for i in input().split()] q = int(input()) last = 0 lcms = {} def get_lcm(i,j): if i in lcms: if j in lcms[i]: return lcms[i][j] else: lcms[i] = {} if j not in lcms: lcms[j] = {} a = int((i*j)/gcd(i,j)) lcms[i][j] = a lcms[j][i] = a return a def find_arr_lcm(k): if (len(k) == 2): return get_lcm(k[0], k[1]) elif (len(k) == 1): return k[0] else: print(k, len(k)) return get_lcm(k[-1], find_arr_lcm(k[:-1])) for i in range(q): x, y = [int(i) for i in input().split()] l,r = ((last+x)%n)+1, ((last+y)%n)+1 if l > r: l,r = r,l print("111 ", last, x,y,l, r, lcms, a[l-1:r]) last = find_arr_lcm(a[l-1:r]) print(last) ```
instruction
0
23,076
12
46,152
No
output
1
23,076
12
46,153
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a sequence b_1, b_2, b_3 ..., b_{k - 1}, b_k almost increasing if $$$min(b_1, b_2) ≀ min(b_2, b_3) ≀ ... ≀ min(b_{k - 1}, b_k).$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers a_1, a_2, ..., a_n. Calculate the length of its longest almost increasing subsequence. You'll be given t test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of independent test cases. The first line of each test case contains a single integer n (2 ≀ n ≀ 5 β‹… 10^5) β€” the length of the sequence a. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the sequence itself. It's guaranteed that the total sum of n over all test cases doesn't exceed 5 β‹… 10^5. Output For each test case, print one integer β€” the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Output 6 2 7 Note In the first test case, one of the optimal answers is subsequence 1, 2, 7, 2, 2, 3. In the second and third test cases, the whole sequence a is already almost increasing.
instruction
0
23,083
12
46,166
Tags: data structures, dp, greedy Correct Solution: ``` import sys input = sys.stdin.readline def ri(): return [int(i) for i in input().split()] # Fenwick max def ft_max_set(t, at, val): while at < len(t): t[at] = max(t[at], val) at |= at + 1 def ft_max_query(t, at): res = 0 while at >= 0: res = max(res, t[at]) at = (at & (at + 1)) - 1 return res def do_nxt(b): st = [] nxt = [len(b)] * len(b) for i in range(len(b)): while len(st) > 0 and st[-1][0] < b[i]: (val, at) = st.pop() nxt[at] = i st.append((b[i], i)) return nxt def main(): t = ri()[0] for _ in range(t): n = ri()[0] b = ri() MAX_VAL = 5 * 10 ** 5 + 10 nxt = do_nxt(b) # ==len(b) if not found extra = [[] for _ in range(n)] t = [0] * MAX_VAL # Fenwick: lastval→longest # prepend 0 to the sequence (will _always_ be too long by 1) ft_max_set(t, 0, 1) extra[0].append((0, 2)) for i in range(n): # print(f'---------i={i}') # seek best with last min-accepted = b[i] best = ft_max_query(t, b[i]) # print(f'query\tbest for {b[i]} → {best}') best += 1 # accept this one ft_max_set(t, at=b[i], val=best) # print(f'set \tai={b[i]} → best={best}') if nxt[i] < n: # print(f'write extra at {nxt[i]} as ai={b[i]} → {best + 1}') extra[nxt[i]].append((b[i], best + 1)) # extra updates; all ai < b[i] for (ai, e_best) in extra[i]: # print(f'extra at ai={ai} → best={e_best}') ft_max_set(t, at=ai, val=e_best) print(ft_max_query(t, MAX_VAL - 1)-1) main() ```
output
1
23,083
12
46,167
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a sequence b_1, b_2, b_3 ..., b_{k - 1}, b_k almost increasing if $$$min(b_1, b_2) ≀ min(b_2, b_3) ≀ ... ≀ min(b_{k - 1}, b_k).$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers a_1, a_2, ..., a_n. Calculate the length of its longest almost increasing subsequence. You'll be given t test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of independent test cases. The first line of each test case contains a single integer n (2 ≀ n ≀ 5 β‹… 10^5) β€” the length of the sequence a. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the sequence itself. It's guaranteed that the total sum of n over all test cases doesn't exceed 5 β‹… 10^5. Output For each test case, print one integer β€” the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Output 6 2 7 Note In the first test case, one of the optimal answers is subsequence 1, 2, 7, 2, 2, 3. In the second and third test cases, the whole sequence a is already almost increasing.
instruction
0
23,084
12
46,168
Tags: data structures, dp, greedy Correct Solution: ``` import bisect as bs for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) if n <= 2: print(n) continue stack, m, p = [a[0]], [], [(1000000, 0)] for v in a[1:]: k = bs.bisect_right(m, v) if k == len(m): stack.append(v) m.append(min(stack[-1], stack[-2])) else: stack[k + 1] = m[k] = v while p[-1][0] <= v: vv, kk = p.pop() m[kk] = min(m[kk], vv) if k + 1 < len(m): p.append((v, k + 1)) print(len(stack)) ```
output
1
23,084
12
46,169
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a sequence b_1, b_2, b_3 ..., b_{k - 1}, b_k almost increasing if $$$min(b_1, b_2) ≀ min(b_2, b_3) ≀ ... ≀ min(b_{k - 1}, b_k).$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers a_1, a_2, ..., a_n. Calculate the length of its longest almost increasing subsequence. You'll be given t test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of independent test cases. The first line of each test case contains a single integer n (2 ≀ n ≀ 5 β‹… 10^5) β€” the length of the sequence a. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the sequence itself. It's guaranteed that the total sum of n over all test cases doesn't exceed 5 β‹… 10^5. Output For each test case, print one integer β€” the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Output 6 2 7 Note In the first test case, one of the optimal answers is subsequence 1, 2, 7, 2, 2, 3. In the second and third test cases, the whole sequence a is already almost increasing.
instruction
0
23,085
12
46,170
Tags: data structures, dp, greedy Correct Solution: ``` import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): def __init__(self, file): self.newlines = 0 self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline() # -------------------------------------------------------------------- def RL(): return map(int, sys.stdin.readline().split()) def RLL(): return list(map(int, sys.stdin.readline().split())) def N(): return int(input()) def S(): return input().strip() def print_list(l): print(' '.join(map(str, l))) # sys.setrecursionlimit(100000) # import random # from functools import reduce # from functools import lru_cache # from heapq import * # from collections import deque as dq # from math import ceil import bisect as bs # from collections import Counter # from collections import defaultdict as dc # a = "169 291 471 404 199 306 21 289 147 223 109 97 100 477 188 485 454 170 95 429 398 385 397 12 53 207 107 163 417 418 487 94 335 53 286 69 207 358 268 285 368 54 420 33 213 357 282 453 141 16 479 439 272 404 84 109 452 466 107 155 313 205 364 395 76 105 18 300 184 24 364 471 132 148 88 223 387 343 239 320 101 75 335 491 359 498 444 464 398 18 451 302 36 382 418 279 158 260 326 238 297 333 483 419 373 252 325 327 481 71 13 253 357 36 339 6 374 242 335 149 73 322 476 256 180 350 180 113 340 489 323 74 214 172 27 182 410 170 5 465 27 490 47 232 289 92 52 150 205 28 319 357 128 91 220 315 197 471 334 209 326 184 323 40 27 309 460 26 420 427 46 237 317 7 477 137 321 446 417 177 392 40 356 143 172 227 489 357 109 295 88 388 421 289 364 476 137 397 388 65 397 258 199 440 200 410 496 333 326 187 130 351 448 451 484 319 64 244 374 69 406 110 143 60 472 88 498 382 66 331 21 39 271 51 364 277 283 203 147 87 67 297 287 170 320 104 165 457 264 421 448 223 159 126 370 403 139 354 463 299 37 207 486 98 285 414 498 253 102 374 494 475 188 158 313 48 360 287 486 198 480 175 13 307 4 453 41 104 178 142 61 410 59 133 79 412 192 240 255 254 121 268 373 97 453 365 391 84 349 20 84 249 48 368 163 234 274 262 133 496 218 172 467 231 285 205 380 432 366 19 189 230 169 228 224 393 291 178 457 51 441 232 201 194 157 143 421 179 455 281 276 38 369 356 193 403 224 100 294 238 45 281 416 292 121 391 4 328 109 22 39 51 286 438 323 97 42 260 72 370 281 230 196 286 213 139 223 204 331 113 4 39 320 246 410 412 133 491 256 457 23 332 417 270 275 238 317 223 91 375 278 211 96 486 232 488 374 156 182 88 395 472 62 401 207 180 289 199 481 226 232 249 145 228 172 363 261 160 216 487 469 89 421 459 122 308 429 144 6 479 321 199 278 438 85 162 187 277 280 484 14 363 77 106 476 382 299 139 239 309 394 349 406 85 500 21 201 396 397 131 303 73 52 411 122 61 76 130 215 500 475 107 351 94 370 246 95 227 23 236" # a = list(map(int, a.split(' '))) for _ in range(N()): n = N() a = RLL() if n <= 2: print(n) continue stack = [a[0], a[1]] m = [min(stack)] p = [(1000000, 0)] for v in a[2:]: k = bs.bisect_right(m, v) if k == len(m): stack.append(v) m.append(min(stack[-1], stack[-2])) else: stack[k + 1] = v m[k] = v while p[-1][0] <= v: vv, kk = p.pop() m[kk] = min(m[kk], vv) stack[kk + 1] = min(stack[kk + 1], v) if k + 1 < len(m): p.append((v, k + 1)) # print(v) # print_list(stack) # print_list(m) print(len(stack)) ```
output
1
23,085
12
46,171
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a sequence b_1, b_2, b_3 ..., b_{k - 1}, b_k almost increasing if $$$min(b_1, b_2) ≀ min(b_2, b_3) ≀ ... ≀ min(b_{k - 1}, b_k).$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers a_1, a_2, ..., a_n. Calculate the length of its longest almost increasing subsequence. You'll be given t test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of independent test cases. The first line of each test case contains a single integer n (2 ≀ n ≀ 5 β‹… 10^5) β€” the length of the sequence a. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the sequence itself. It's guaranteed that the total sum of n over all test cases doesn't exceed 5 β‹… 10^5. Output For each test case, print one integer β€” the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Output 6 2 7 Note In the first test case, one of the optimal answers is subsequence 1, 2, 7, 2, 2, 3. In the second and third test cases, the whole sequence a is already almost increasing.
instruction
0
23,086
12
46,172
Tags: data structures, dp, greedy Correct Solution: ``` import bisect as bs for _ in range(int(input())): n, a = int(input()), list(map(int, input().split())) stack, m, p = [a[0]], [], [(1000000, 0)] for v in a[1:]: k = bs.bisect_right(m, v) if k == len(m): stack.append(v) m.append(min(stack[-1], stack[-2])) else: stack[k + 1] = m[k] = v while p[-1][0] <= v: vv, kk = p.pop() m[kk] = min(m[kk], vv) if k + 1 < len(m): p.append((v, k + 1)) print(len(stack)) ```
output
1
23,086
12
46,173
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a sequence b_1, b_2, b_3 ..., b_{k - 1}, b_k almost increasing if $$$min(b_1, b_2) ≀ min(b_2, b_3) ≀ ... ≀ min(b_{k - 1}, b_k).$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers a_1, a_2, ..., a_n. Calculate the length of its longest almost increasing subsequence. You'll be given t test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of independent test cases. The first line of each test case contains a single integer n (2 ≀ n ≀ 5 β‹… 10^5) β€” the length of the sequence a. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the sequence itself. It's guaranteed that the total sum of n over all test cases doesn't exceed 5 β‹… 10^5. Output For each test case, print one integer β€” the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Output 6 2 7 Note In the first test case, one of the optimal answers is subsequence 1, 2, 7, 2, 2, 3. In the second and third test cases, the whole sequence a is already almost increasing.
instruction
0
23,087
12
46,174
Tags: data structures, dp, greedy Correct Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq, bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default=2**51, func=lambda a, b: a & b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=0, func=lambda a, b: max(a , b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] < key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] >= k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary------------------------------------ for ik in range(int(input())): n=int(input()) l=list(map(int,input().split())) ma=max(l) dp1=[0]*(ma+1) dp=[0]*(ma+1) dp[l[1]]=2 dp[l[0]]=max(dp[l[0]],1) dp1[l[0]]=1 s=SegmentTree(dp) s1=SegmentTree(dp1) q=[l[1]] heapq.heapify(q) for i in range(2,n): t=dp[l[i]] dp[l[i]]=max(dp[l[i]],s.query(0,l[i])+1,s1.query(0,l[i])+2) s.__setitem__(l[i],dp[l[i]]) if len(q)>0: e=heapq.heappop(q) while(e<l[i]): dp1[e] = max(dp1[e], dp[e]) s1.__setitem__(e, dp1[e]) if len(q)>0: e=heapq.heappop(q) else: e=-1 break if e!=-1: heapq.heappush(q,e) dp1[l[i]] = max(dp1[l[i]], t) s1.__setitem__(l[i], dp1[l[i]]) heapq.heappush(q,l[i]) print(s.query(0,ma)) ```
output
1
23,087
12
46,175
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a sequence b_1, b_2, b_3 ..., b_{k - 1}, b_k almost increasing if $$$min(b_1, b_2) ≀ min(b_2, b_3) ≀ ... ≀ min(b_{k - 1}, b_k).$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers a_1, a_2, ..., a_n. Calculate the length of its longest almost increasing subsequence. You'll be given t test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of independent test cases. The first line of each test case contains a single integer n (2 ≀ n ≀ 5 β‹… 10^5) β€” the length of the sequence a. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the sequence itself. It's guaranteed that the total sum of n over all test cases doesn't exceed 5 β‹… 10^5. Output For each test case, print one integer β€” the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Output 6 2 7 Note In the first test case, one of the optimal answers is subsequence 1, 2, 7, 2, 2, 3. In the second and third test cases, the whole sequence a is already almost increasing.
instruction
0
23,088
12
46,176
Tags: data structures, dp, greedy Correct Solution: ``` import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): def __init__(self, file): self.newlines = 0 self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline() # -------------------------------------------------------------------- def RL(): return map(int, sys.stdin.readline().split()) def RLL(): return list(map(int, sys.stdin.readline().split())) def N(): return int(input()) def S(): return input().strip() def print_list(l): print(' '.join(map(str, l))) # sys.setrecursionlimit(100000) # import random # from functools import reduce # from functools import lru_cache # from heapq import * # from collections import deque as dq # from math import ceil import bisect as bs # from collections import Counter # from collections import defaultdict as dc # a = "169 291 471 404 199 306 21 289 147 223 109 97 100 477 188 485 454 170 95 429 398 385 397 12 53 207 107 163 417 418 487 94 335 53 286 69 207 358 268 285 368 54 420 33 213 357 282 453 141 16 479 439 272 404 84 109 452 466 107 155 313 205 364 395 76 105 18 300 184 24 364 471 132 148 88 223 387 343 239 320 101 75 335 491 359 498 444 464 398 18 451 302 36 382 418 279 158 260 326 238 297 333 483 419 373 252 325 327 481 71 13 253 357 36 339 6 374 242 335 149 73 322 476 256 180 350 180 113 340 489 323 74 214 172 27 182 410 170 5 465 27 490 47 232 289 92 52 150 205 28 319 357 128 91 220 315 197 471 334 209 326 184 323 40 27 309 460 26 420 427 46 237 317 7 477 137 321 446 417 177 392 40 356 143 172 227 489 357 109 295 88 388 421 289 364 476 137 397 388 65 397 258 199 440 200 410 496 333 326 187 130 351 448 451 484 319 64 244 374 69 406 110 143 60 472 88 498 382 66 331 21 39 271 51 364 277 283 203 147 87 67 297 287 170 320 104 165 457 264 421 448 223 159 126 370 403 139 354 463 299 37 207 486 98 285 414 498 253 102 374 494 475 188 158 313 48 360 287 486 198 480 175 13 307 4 453 41 104 178 142 61 410 59 133 79 412 192 240 255 254 121 268 373 97 453 365 391 84 349 20 84 249 48 368 163 234 274 262 133 496 218 172 467 231 285 205 380 432 366 19 189 230 169 228 224 393 291 178 457 51 441 232 201 194 157 143 421 179 455 281 276 38 369 356 193 403 224 100 294 238 45 281 416 292 121 391 4 328 109 22 39 51 286 438 323 97 42 260 72 370 281 230 196 286 213 139 223 204 331 113 4 39 320 246 410 412 133 491 256 457 23 332 417 270 275 238 317 223 91 375 278 211 96 486 232 488 374 156 182 88 395 472 62 401 207 180 289 199 481 226 232 249 145 228 172 363 261 160 216 487 469 89 421 459 122 308 429 144 6 479 321 199 278 438 85 162 187 277 280 484 14 363 77 106 476 382 299 139 239 309 394 349 406 85 500 21 201 396 397 131 303 73 52 411 122 61 76 130 215 500 475 107 351 94 370 246 95 227 23 236" # a = list(map(int, a.split(' '))) for _ in range(N()): n = N() a = RLL() stack, m, p = [a[0]], [], [(1000000, 0)] for v in a[1:]: k = bs.bisect_right(m, v) if k == len(m): stack.append(v) m.append(min(stack[-1], stack[-2])) else: stack[k + 1] = m[k] = v while p[-1][0] <= v: vv, kk = p.pop() m[kk] = min(m[kk], vv) if k + 1 < len(m): p.append((v, k + 1)) print(len(stack)) ```
output
1
23,088
12
46,177
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call a sequence b_1, b_2, b_3 ..., b_{k - 1}, b_k almost increasing if $$$min(b_1, b_2) ≀ min(b_2, b_3) ≀ ... ≀ min(b_{k - 1}, b_k).$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers a_1, a_2, ..., a_n. Calculate the length of its longest almost increasing subsequence. You'll be given t test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of independent test cases. The first line of each test case contains a single integer n (2 ≀ n ≀ 5 β‹… 10^5) β€” the length of the sequence a. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the sequence itself. It's guaranteed that the total sum of n over all test cases doesn't exceed 5 β‹… 10^5. Output For each test case, print one integer β€” the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Output 6 2 7 Note In the first test case, one of the optimal answers is subsequence 1, 2, 7, 2, 2, 3. In the second and third test cases, the whole sequence a is already almost increasing.
instruction
0
23,089
12
46,178
Tags: data structures, dp, greedy Correct Solution: ``` import bisect as bs for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) if n <= 2: print(n) continue stack, m, p = [a[0]], [], [(1000000, 0)] for v in a[1:]: k = bs.bisect_right(m, v) if k == len(m): stack.append(v) m.append(min(stack[-1], stack[-2])) else: stack[k + 1] = m[k] = v while p[-1][0] <= v: vv, kk = p.pop() m[kk] = min(m[kk], vv) stack[kk + 1] = min(stack[kk + 1], v) if k + 1 < len(m): p.append((v, k + 1)) print(len(stack)) ```
output
1
23,089
12
46,179
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call a sequence b_1, b_2, b_3 ..., b_{k - 1}, b_k almost increasing if $$$min(b_1, b_2) ≀ min(b_2, b_3) ≀ ... ≀ min(b_{k - 1}, b_k).$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers a_1, a_2, ..., a_n. Calculate the length of its longest almost increasing subsequence. You'll be given t test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of independent test cases. The first line of each test case contains a single integer n (2 ≀ n ≀ 5 β‹… 10^5) β€” the length of the sequence a. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the sequence itself. It's guaranteed that the total sum of n over all test cases doesn't exceed 5 β‹… 10^5. Output For each test case, print one integer β€” the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Output 6 2 7 Note In the first test case, one of the optimal answers is subsequence 1, 2, 7, 2, 2, 3. In the second and third test cases, the whole sequence a is already almost increasing. Submitted Solution: ``` n = int(input()) for _ in range(n): m = int(input()) arr = list(map(int, input().split())) stacks = [[0, arr[0]]] for i in arr[1:]: lower = 0 upper = len(stacks) while lower < upper: mid = (lower + upper) // 2 if min(stacks[mid]) <= i: lower = mid + 1 else: upper = mid if lower == 0: continue else: if lower == len(stacks): stacks.append([stacks[-1][1], i]) else: if min([stacks[lower - 1][1], i]) <= min(stacks[lower]): stacks[lower] = [stacks[lower - 1][1], i] print(len(stacks)) ```
instruction
0
23,090
12
46,180
No
output
1
23,090
12
46,181
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call a sequence b_1, b_2, b_3 ..., b_{k - 1}, b_k almost increasing if $$$min(b_1, b_2) ≀ min(b_2, b_3) ≀ ... ≀ min(b_{k - 1}, b_k).$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers a_1, a_2, ..., a_n. Calculate the length of its longest almost increasing subsequence. You'll be given t test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of independent test cases. The first line of each test case contains a single integer n (2 ≀ n ≀ 5 β‹… 10^5) β€” the length of the sequence a. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the sequence itself. It's guaranteed that the total sum of n over all test cases doesn't exceed 5 β‹… 10^5. Output For each test case, print one integer β€” the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Output 6 2 7 Note In the first test case, one of the optimal answers is subsequence 1, 2, 7, 2, 2, 3. In the second and third test cases, the whole sequence a is already almost increasing. Submitted Solution: ``` import sys def upperbinsearch(lastVals, l, h, x): while l + 1 < h: mid = l + ((h - l)//2) if min(lastVals[mid][0], lastVals[mid][1]) <= x: l = mid else: h = mid return h t = int(input()) inf = float("inf") for cutiepie in range(t): n = int(sys.stdin.readline()) arr = [int(amit) for amit in sys.stdin.readline().split(" ")] #LIS lastVals = [(-inf, -inf) for i in range(len(arr) + 1)] # an increasing array (but not strictly increasing) dp = [0 for i in range(len(arr))] res = 0 lastVals[1] = (arr[0], -inf, -inf) res = 1 dp[0] = 1 #print (lastVals) for i in range(1, len(arr)): cur = arr[i] #print (cur) if cur >= min(lastVals[res][0], lastVals[res][1]): lastVals[res+1] = (cur, lastVals[res][0], lastVals[res][1]) dp[i] = res + 1 res += 1 elif cur < lastVals[1][0]: lastVals[1] = (cur, -inf, -inf) dp[i] = 1 else: k = upperbinsearch(lastVals, 1, res, cur) # 1 to res will be non null # Longest almost increasing subsequence allows entries to be less than the previous value as long as its >= two values ago # we know that cur <= lastVals[k][0] if cur <= lastVals[k][0] and (min(lastVals[k][2], lastVals[k][1]) <= min(lastVals[k][1], cur)): lastVals[k] = (cur, lastVals[k][1], lastVals[k][2]) dp[i] = k #print (lastVals) print (res) ```
instruction
0
23,091
12
46,182
No
output
1
23,091
12
46,183
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call a sequence b_1, b_2, b_3 ..., b_{k - 1}, b_k almost increasing if $$$min(b_1, b_2) ≀ min(b_2, b_3) ≀ ... ≀ min(b_{k - 1}, b_k).$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers a_1, a_2, ..., a_n. Calculate the length of its longest almost increasing subsequence. You'll be given t test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of independent test cases. The first line of each test case contains a single integer n (2 ≀ n ≀ 5 β‹… 10^5) β€” the length of the sequence a. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the sequence itself. It's guaranteed that the total sum of n over all test cases doesn't exceed 5 β‹… 10^5. Output For each test case, print one integer β€” the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Output 6 2 7 Note In the first test case, one of the optimal answers is subsequence 1, 2, 7, 2, 2, 3. In the second and third test cases, the whole sequence a is already almost increasing. Submitted Solution: ``` t=int(input()) for i in range(t): n=int(input()) z=list(map(int,input().split())) i=0 j=1 c=2 while(i<n and j<n-1): if(min(z[i],z[j])<=min(z[j],z[j+1])): c=c+2 i=i+2 j=j+2 else: i=i+1 j=j+1 print(min(c,n)) ```
instruction
0
23,092
12
46,184
No
output
1
23,092
12
46,185
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call a sequence b_1, b_2, b_3 ..., b_{k - 1}, b_k almost increasing if $$$min(b_1, b_2) ≀ min(b_2, b_3) ≀ ... ≀ min(b_{k - 1}, b_k).$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers a_1, a_2, ..., a_n. Calculate the length of its longest almost increasing subsequence. You'll be given t test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of independent test cases. The first line of each test case contains a single integer n (2 ≀ n ≀ 5 β‹… 10^5) β€” the length of the sequence a. The second line of each test case contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” the sequence itself. It's guaranteed that the total sum of n over all test cases doesn't exceed 5 β‹… 10^5. Output For each test case, print one integer β€” the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Output 6 2 7 Note In the first test case, one of the optimal answers is subsequence 1, 2, 7, 2, 2, 3. In the second and third test cases, the whole sequence a is already almost increasing. Submitted Solution: ``` import sys def upperbinsearch(lastVals, l, h, x): while l + 1 < h: mid = l + ((h - l)//2) if min(lastVals[mid], lastVals[mid-1]) <= x: l = mid else: h = mid return h t = int(input()) inf = float("inf") for cutiepie in range(t): n = int(sys.stdin.readline()) arr = [int(amit) for amit in sys.stdin.readline().split(" ")] #LIS lastVals = [-inf for i in range(len(arr) + 1)] # an increasing array (but not strictly increasing) dp = [0 for i in range(len(arr))] res = 0 lastVals[1] = arr[0] res = 1 dp[0] = 1 for i in range(1, len(arr)): cur = arr[i] if cur >= min(lastVals[res], lastVals[res-1]): lastVals[res+1] = cur dp[i] = res + 1 res += 1 elif cur < lastVals[1]: lastVals[1] = cur dp[i] = 1 else: k = upperbinsearch(lastVals, 2, res, cur) # 1 to res will be non null # Longest almost increasing subsequence allows entries to be less than the previous value as long as its >= two values ago if k >= res + 1: k = res + 1 res += 1 lastVals[k] = cur dp[i] = k print (res) ```
instruction
0
23,093
12
46,186
No
output
1
23,093
12
46,187
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
instruction
0
23,166
12
46,332
Tags: constructive algorithms, math Correct Solution: ``` n = int(input()) if n%4 > 1: print(-1) else: a = [n+1>>1]*n for i in range(n//4): j = i*2 a[j], a[j+1], a[-2-j], a[-1-j] = j+2, n-j, j+1, n-1-j print(' '.join(map(str, a))) ```
output
1
23,166
12
46,333
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
instruction
0
23,167
12
46,334
Tags: constructive algorithms, math Correct Solution: ``` ''' Created on @author: linhz ''' import sys usedNum=0 n=int(input()) p=[0 for i in range(n+1)] usedNum=0 if n%4==3 or n%4==2: print(-1) else: i=1 j=n a=1 b=n while j>i: p[i]=a+1 p[i+1]=b p[j]=b-1 p[j-1]=a i+=2 j-=2 a+=2 b-=2 if j==i: p[i]=a ans="" for i in range(1,n+1): ans+=str(p[i])+" " print(ans) ```
output
1
23,167
12
46,335
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
instruction
0
23,168
12
46,336
Tags: constructive algorithms, math Correct Solution: ``` from itertools import permutations from sys import stdin def checkit(vector, upto=-1): if upto == -1: upto = len(vector) for i in range(0, upto): if vector[vector[i] - 1] != len(vector) - (i + 1) + 1: return False return True def calculate(n): numbers = list(range(1, n + 1)) result = [0] * n for i in range(0, n): if result[i] != 0: continue if i > 0 and checkit(result, i): continue expected = n - i for v in numbers: if v - 1 == i and expected != v: continue if v == expected: result[v-1] = v numbers.remove(v) break elif result[v - 1] == expected: numbers.remove(v) result[i] = v break elif result[v - 1] == 0: assert expected in numbers result[i] = v result[v - 1] = expected numbers.remove(v) numbers.remove(expected) break return result def calculate_v2(n): result = [0] * n first_sum = n + 2 second_sum = n nf = n i = 0 while nf > first_sum // 2: result[i] = first_sum - nf result[i + 1] = nf nf -= 2 i += 2 if n % 2 == 1: result[i] = i + 1 i = n - 1 while i > n // 2: result[i] = i result[i-1] = second_sum - i i -= 2 return result def main(): number = int(stdin.readline()) result = calculate_v2(number) if not checkit(result): print(-1) else: print(" ".join([str(v) for v in result])) if __name__ == "__main__": main() ```
output
1
23,168
12
46,337
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
instruction
0
23,169
12
46,338
Tags: constructive algorithms, math Correct Solution: ``` import sys from collections import deque n = int(input()) if n % 4 == 2 or n % 4 == 3: print('-1') sys.exit() arr = [None] * (n + 1) qt = deque([i for i in range(1, n + 1)]) mark = set() while qt: while qt and qt[0] in mark: qt.popleft() if not qt: break a = qt.popleft() while qt and qt[0] in mark: qt.popleft() if not qt: break b = qt.popleft() for i in range(4): mark.add(a) mark.add(b) arr[a] = b arr[b] = n - a + 1 a = b b = arr[b] for i in range(1, n + 1): if not arr[i]: arr[i] = a break print(' '.join(map(str, arr[1:]))) ```
output
1
23,169
12
46,339
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
instruction
0
23,170
12
46,340
Tags: constructive algorithms, math Correct Solution: ``` n=int(input()) if n%4>1:print(-1) else: ans=[i for i in range(1,n+1)] for i in range(0,n//2,2): ans[i]=i+2 ans[i+1]=n-i ans[n-i-1]=n-i-1 ans[n-i-2]=i+1 print(*ans) ```
output
1
23,170
12
46,341
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
instruction
0
23,171
12
46,342
Tags: constructive algorithms, math Correct Solution: ``` n = int(input()) if n%4==2 or n%4==3: print(-1) else: ans = [0]*n for i in range(0, n//2, 2): ans[i] = i+2 ans[i+1] = n-i ans[n-i-1] = n-i-1 ans[n-i-2] = i+1 if n%4 == 1: ans[n//2] = n//2 + 1 print(*ans) ```
output
1
23,171
12
46,343
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
instruction
0
23,172
12
46,344
Tags: constructive algorithms, math Correct Solution: ``` n = int(input()) if n % 4 > 1: print(-1) else: k = n // 2 t = [0] * n for i in range(0, k, 2): t[i] = str(i + 2) for i in range(1, k, 2): t[i] = str(n - i + 1) if n & 1: k += 1 t[k - 1] = str(k) for i in range(k, n, 2): t[i] = str(n - i - 1) for i in range(k + 1, n, 2): t[i] = str(i) print(' '.join(t)) ```
output
1
23,172
12
46,345
Provide tags and a correct Python 3 solution for this coding contest problem. A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
instruction
0
23,173
12
46,346
Tags: constructive algorithms, math Correct Solution: ``` n=int(input()) if n==1: print (1) exit() if n%4>1: print (-1) exit() ans=[-1]*n left=n start=n-2 nums=1 nume=n while left>=4: ans[start]=nums ans[nums-1]=nums+1 ans[nums]=nume ans[nume-1]=nume-1 start-=2 nums+=2 nume-=2 left-=4 # print (ans) if left==1: ans[start+1]=start+2 print (*ans) ```
output
1
23,173
12
46,347