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.
You are given an array a_1, a_2, ..., a_n and an array b_1, b_2, ..., b_n.
For one operation you can sort in non-decreasing order any subarray a[l ... r] of the array a.
For example, if a = [4, 2, 2, 1, 3, 1] and you choose subbarray a[2 ... 5], then the array turns into [4, 1, 2, 2, 3, 1].
You are asked to determine whether it is possible to obtain the array b by applying this operation any number of times (possibly zero) to the array a.
Input
The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of queries.
The first line of each query contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).
The second line of each query contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n).
The third line of each query contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ n).
It is guaranteed that ∑ n ≤ 3 ⋅ 10^5 over all queries in a test.
Output
For each query print YES (in any letter case) if it is possible to obtain an array b and NO (in any letter case) otherwise.
Example
Input
4
7
1 7 1 4 4 5 6
1 1 4 4 5 7 6
5
1 1 3 3 5
1 1 3 3 5
2
1 1
1 2
3
1 2 3
3 2 1
Output
YES
YES
NO
NO
Note
In first test case the can sort subarray a_1 ... a_5, then a will turn into [1, 1, 4, 4, 7, 5, 6], and then sort subarray a_5 ... a_6.
Submitted Solution:
```
import sys
input = sys.stdin.readline
Q=int(input())
from collections import Counter
for testcases in range(Q):
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
if Counter(A)!=Counter(B):
print("NO")
continue
seg_el=1<<(n.bit_length())
SEG=[0]*(2*seg_el)
for i in range(n):
SEG[i+seg_el]=A[i]
for i in range(seg_el-1,0,-1):
SEG[i]=min(SEG[i*2],SEG[i*2+1])
def update(n,x,seg_el):
i=n+seg_el
SEG[i]=x
i>>=1
while i!=0:
SEG[i]=min(SEG[i*2],SEG[i*2+1])
i>>=1
def getvalues(l,r):
L=l+seg_el
R=r+seg_el
ANS=1<<30
while L<R:
if L & 1:
ANS=min(ANS , SEG[L])
L+=1
if R & 1:
R-=1
ANS=min(ANS , SEG[R])
L>>=1
R>>=1
return ANS
A0=[[] for i in range(n+1)]
B0=[[] for i in range(n+1)]
for i in range(n):
A0[A[i]].append(i)
B0[B[i]].append(i)
COUNTN=[0]*(n+1)
for b in B:
#print(A0[b][COUNTN[b]],B0[b][COUNTN[b]])
#print(getvalues(0,A0[b][COUNTN[b]]))
if getvalues(0,A0[b][COUNTN[b]])<b:
print("NO")
break
update(A0[b][COUNTN[b]],1<<30,seg_el)
COUNTN[b]+=1
else:
print("YES")
``` | instruction | 0 | 63,214 | 12 | 126,428 |
Yes | output | 1 | 63,214 | 12 | 126,429 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ..., a_n and an array b_1, b_2, ..., b_n.
For one operation you can sort in non-decreasing order any subarray a[l ... r] of the array a.
For example, if a = [4, 2, 2, 1, 3, 1] and you choose subbarray a[2 ... 5], then the array turns into [4, 1, 2, 2, 3, 1].
You are asked to determine whether it is possible to obtain the array b by applying this operation any number of times (possibly zero) to the array a.
Input
The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of queries.
The first line of each query contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).
The second line of each query contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n).
The third line of each query contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ n).
It is guaranteed that ∑ n ≤ 3 ⋅ 10^5 over all queries in a test.
Output
For each query print YES (in any letter case) if it is possible to obtain an array b and NO (in any letter case) otherwise.
Example
Input
4
7
1 7 1 4 4 5 6
1 1 4 4 5 7 6
5
1 1 3 3 5
1 1 3 3 5
2
1 1
1 2
3
1 2 3
3 2 1
Output
YES
YES
NO
NO
Note
In first test case the can sort subarray a_1 ... a_5, then a will turn into [1, 1, 4, 4, 7, 5, 6], and then sort subarray a_5 ... a_6.
Submitted Solution:
```
from sys import stdin
from collections import deque
class Stree:
def __init__(self, f, n, default, init_data):
self.ln = 2**(n-1).bit_length()
self.data = [default] * (self.ln * 2)
self.f = f
for i, d in init_data.items():
self.data[self.ln + i] = d
for j in range(self.ln - 1, -1, -1):
self.data[j] = f(self.data[j*2], self.data[j*2+1])
def update(self, i, a):
p = self.ln + i
self.data[p] = a
while p > 1:
p = p // 2
self.data[p] = self.f(self.data[p*2], self.data[p*2+1])
def get(self, i, j):
def _get(l, r, p):
if i <= l and j >= r:
return self.data[p]
else:
m = (l+r)//2
if j <= m:
return _get(l, m, p*2)
elif i >= m:
return _get(m, r, p*2+1)
else:
return self.f(_get(l, m, p*2), _get(m, r, p*2+1))
return _get(0, self.ln, 1)
si = iter(stdin)
t = int(next(si))
for _ in range(t):
n = int(next(si))
aa = list(map(int, next(si).split()))
bb = list(map(int, next(si).split()))
pp = {}
for i in range(n-1, -1, -1):
a = aa[i]
if a in pp:
pp[a].append(i)
else:
pp[a] = [i]
pplast = { a: p[-1] for (a,p) in pp.items()}
stree = Stree(min, n+1, n+1, pplast)
result = 'YES'
m = 0
for b in bb:
if b in pp and pp[b] != []:
p = pp[b].pop()
if stree.get(0, b) < p:
result = 'NO'
break
# del aa[p]
# aa.insert(i,b)
# print(aa)
if pp[b] == []:
stree.update(b, n+1)
else:
stree.update(b, pp[b][-1])
else:
result = 'NO'
break
print(result)
``` | instruction | 0 | 63,215 | 12 | 126,430 |
Yes | output | 1 | 63,215 | 12 | 126,431 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ..., a_n and an array b_1, b_2, ..., b_n.
For one operation you can sort in non-decreasing order any subarray a[l ... r] of the array a.
For example, if a = [4, 2, 2, 1, 3, 1] and you choose subbarray a[2 ... 5], then the array turns into [4, 1, 2, 2, 3, 1].
You are asked to determine whether it is possible to obtain the array b by applying this operation any number of times (possibly zero) to the array a.
Input
The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of queries.
The first line of each query contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).
The second line of each query contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n).
The third line of each query contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ n).
It is guaranteed that ∑ n ≤ 3 ⋅ 10^5 over all queries in a test.
Output
For each query print YES (in any letter case) if it is possible to obtain an array b and NO (in any letter case) otherwise.
Example
Input
4
7
1 7 1 4 4 5 6
1 1 4 4 5 7 6
5
1 1 3 3 5
1 1 3 3 5
2
1 1
1 2
3
1 2 3
3 2 1
Output
YES
YES
NO
NO
Note
In first test case the can sort subarray a_1 ... a_5, then a will turn into [1, 1, 4, 4, 7, 5, 6], and then sort subarray a_5 ... a_6.
Submitted Solution:
```
import sys
from collections import Counter
class Segtree:
def __init__(self, A, intv, initialize = True, segf = min):
self.N = len(A)
self.N0 = 2**(self.N-1).bit_length()
self.intv = intv
self.segf = segf
if initialize:
self.data = [intv]*(self.N0-1) + A + [intv]*(self.N0 - self.N + 1)
for i in range(self.N0-2, -1, -1):
self.data[i] = self.segf(self.data[2*i+1], self.data[2*i+2])
else:
self.data = [intv]*(2*self.N0)
def update(self, k, x):
k += self.N0-1
self.data[k] = x
while k >= 0 :
k = (k-1)//2
self.data[k] = self.segf(self.data[2*k+1], self.data[2*k+2])
def query(self, l, r):
L, R = l+self.N0, r+self.N0
s = self.intv
while L < R:
if R & 1:
R -= 1
s = self.segf(s, self.data[R-1])
if L & 1:
s = self.segf(s, self.data[L-1])
L += 1
L >>= 1
R >>= 1
return s
T = int(input())
def solve(N, A, B):
if Counter(A) != Counter(B):
return False
ctr = [0]*(N+1)
tableA = [[] for _ in range(N+1)]
for i in range(N):
tableA[A[i]].append(i)
bri = [None]*N
for i in range(N):
b = B[i]
bri[i] = tableA[b][ctr[b]]
ctr[b] += 1
St = Segtree(A, 10**10, True, min)
for idx in bri:
if A[idx] > St.query(0, idx):
return False
St.update(idx, 10**10)
return True
for _ in range(T):
if solve(int(sys.stdin.readline()), list(map(int, sys.stdin.readline().split())), list(map(int, sys.stdin.readline().split()))):
print('YES')
else:
print('NO')
``` | instruction | 0 | 63,216 | 12 | 126,432 |
Yes | output | 1 | 63,216 | 12 | 126,433 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ..., a_n and an array b_1, b_2, ..., b_n.
For one operation you can sort in non-decreasing order any subarray a[l ... r] of the array a.
For example, if a = [4, 2, 2, 1, 3, 1] and you choose subbarray a[2 ... 5], then the array turns into [4, 1, 2, 2, 3, 1].
You are asked to determine whether it is possible to obtain the array b by applying this operation any number of times (possibly zero) to the array a.
Input
The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of queries.
The first line of each query contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).
The second line of each query contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n).
The third line of each query contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ n).
It is guaranteed that ∑ n ≤ 3 ⋅ 10^5 over all queries in a test.
Output
For each query print YES (in any letter case) if it is possible to obtain an array b and NO (in any letter case) otherwise.
Example
Input
4
7
1 7 1 4 4 5 6
1 1 4 4 5 7 6
5
1 1 3 3 5
1 1 3 3 5
2
1 1
1 2
3
1 2 3
3 2 1
Output
YES
YES
NO
NO
Note
In first test case the can sort subarray a_1 ... a_5, then a will turn into [1, 1, 4, 4, 7, 5, 6], and then sort subarray a_5 ... a_6.
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
s1 = list(map(int,input().split()))
s2 = list(map(int,input().split()))
res = []
index =0
while index < n:
while index < n and s1[index] == s2[index]:
res.append(s1[index])
index+=1
start = index
while index < n and s1[index] <= s1[start] and s1[start] > s2[index]:
index+=1
if index == n:
res += sorted(s1[start:])
else:
res += sorted(s1[start : index+1])
index +=1
if res == s2: print("YES")
else: print("NO")
``` | instruction | 0 | 63,217 | 12 | 126,434 |
No | output | 1 | 63,217 | 12 | 126,435 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ..., a_n and an array b_1, b_2, ..., b_n.
For one operation you can sort in non-decreasing order any subarray a[l ... r] of the array a.
For example, if a = [4, 2, 2, 1, 3, 1] and you choose subbarray a[2 ... 5], then the array turns into [4, 1, 2, 2, 3, 1].
You are asked to determine whether it is possible to obtain the array b by applying this operation any number of times (possibly zero) to the array a.
Input
The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of queries.
The first line of each query contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).
The second line of each query contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n).
The third line of each query contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ n).
It is guaranteed that ∑ n ≤ 3 ⋅ 10^5 over all queries in a test.
Output
For each query print YES (in any letter case) if it is possible to obtain an array b and NO (in any letter case) otherwise.
Example
Input
4
7
1 7 1 4 4 5 6
1 1 4 4 5 7 6
5
1 1 3 3 5
1 1 3 3 5
2
1 1
1 2
3
1 2 3
3 2 1
Output
YES
YES
NO
NO
Note
In first test case the can sort subarray a_1 ... a_5, then a will turn into [1, 1, 4, 4, 7, 5, 6], and then sort subarray a_5 ... a_6.
Submitted Solution:
```
import sys
input = sys.stdin.readline
q = int(input())
for i in range(q):
t = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = 0
while i < t:
if a[i] == b[i]:
i = i + 1
continue
elif a[i] < b[i]:
print("NO")
break
else: # a[i] > b[i]:
for j in range(i + 1, t):
if a[i] == b[j]:
a[i:j+1] = sorted(a[i:j+1])
break
flag = False
for j in range(i, j+1):
if a[j] != b[j]:
flag = True
if flag:
print("NO")
break
else:
i = j + 1
else:
print("YES")
``` | instruction | 0 | 63,218 | 12 | 126,436 |
No | output | 1 | 63,218 | 12 | 126,437 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ..., a_n and an array b_1, b_2, ..., b_n.
For one operation you can sort in non-decreasing order any subarray a[l ... r] of the array a.
For example, if a = [4, 2, 2, 1, 3, 1] and you choose subbarray a[2 ... 5], then the array turns into [4, 1, 2, 2, 3, 1].
You are asked to determine whether it is possible to obtain the array b by applying this operation any number of times (possibly zero) to the array a.
Input
The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of queries.
The first line of each query contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).
The second line of each query contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n).
The third line of each query contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ n).
It is guaranteed that ∑ n ≤ 3 ⋅ 10^5 over all queries in a test.
Output
For each query print YES (in any letter case) if it is possible to obtain an array b and NO (in any letter case) otherwise.
Example
Input
4
7
1 7 1 4 4 5 6
1 1 4 4 5 7 6
5
1 1 3 3 5
1 1 3 3 5
2
1 1
1 2
3
1 2 3
3 2 1
Output
YES
YES
NO
NO
Note
In first test case the can sort subarray a_1 ... a_5, then a will turn into [1, 1, 4, 4, 7, 5, 6], and then sort subarray a_5 ... a_6.
Submitted Solution:
```
q = int(input())
for i in range(q):
n = int(input())
a1 = list(map(int, input().split()))
a2 = list(map(int, input().split()))
if sorted(a1) != sorted(a2):
print('NO')
continue
new = sorted(a1)
mins = [0] * (n + 1)
for i in range(n):
mins[new[i]] = i
pos = [[] for _ in range(n + 1)]
for i in range(n - 1, -1, -1):
pos[a1[i]].append(i)
for i in range(n):
if pos[a2[i]].pop() < i and mins[a2[i]] < i:
print('NO')
break
else:
print('YES')
``` | instruction | 0 | 63,219 | 12 | 126,438 |
No | output | 1 | 63,219 | 12 | 126,439 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ..., a_n and an array b_1, b_2, ..., b_n.
For one operation you can sort in non-decreasing order any subarray a[l ... r] of the array a.
For example, if a = [4, 2, 2, 1, 3, 1] and you choose subbarray a[2 ... 5], then the array turns into [4, 1, 2, 2, 3, 1].
You are asked to determine whether it is possible to obtain the array b by applying this operation any number of times (possibly zero) to the array a.
Input
The first line contains one integer t (1 ≤ t ≤ 3 ⋅ 10^5) — the number of queries.
The first line of each query contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).
The second line of each query contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n).
The third line of each query contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ n).
It is guaranteed that ∑ n ≤ 3 ⋅ 10^5 over all queries in a test.
Output
For each query print YES (in any letter case) if it is possible to obtain an array b and NO (in any letter case) otherwise.
Example
Input
4
7
1 7 1 4 4 5 6
1 1 4 4 5 7 6
5
1 1 3 3 5
1 1 3 3 5
2
1 1
1 2
3
1 2 3
3 2 1
Output
YES
YES
NO
NO
Note
In first test case the can sort subarray a_1 ... a_5, then a will turn into [1, 1, 4, 4, 7, 5, 6], and then sort subarray a_5 ... a_6.
Submitted Solution:
```
# !/usr/bin/env python3
# encoding: UTF-8
# Modified: <30/Jun/2019 09:34:41 PM>
# ✪ H4WK3yE乡
# Mohd. Farhan Tahir
# Indian Institute Of Information Technology (IIIT), Gwalior
import sys
import os
from io import IOBase, BytesIO
def main():
for tc in range(int(input())):
n = int(input())
a = get_array()
tmp = sorted(a)
b = get_array()
if (tmp != sorted(b)):
print('NO')
continue
d = {}
for i in range(n):
if a[i] not in d:
d[a[i]] = i
for i in range(n):
d[tmp[i]] = min(d[tmp[i]], i)
ans = 'YES'
for i in range(n):
#print(i, b[i], d[b[i]])
if i < d[b[i]]:
ans = 'NO'
break
print(ans)
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill():
pass
return super(FastIO, self).read()
def readline(self):
while self.newlines == 0:
s = self._fill()
self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
py2 = round(0.5)
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2 == 1:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
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)
def get_array(): return list(map(int, sys.stdin.readline().split()))
def get_ints(): return map(int, sys.stdin.readline().split())
def input(): return sys.stdin.readline().strip()
if __name__ == "__main__":
main()
``` | instruction | 0 | 63,220 | 12 | 126,440 |
No | output | 1 | 63,220 | 12 | 126,441 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied:
For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1.
For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1.
<image>
Input
The first and the only line contain one integer n (1 ≤ n ≤ 10^5).
Output
If there is no solution, output "NO" in the first line.
If there is a solution, output "YES" in the first line. In the second line output 2n numbers — numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them.
Examples
Input
3
Output
YES
1 4 5 2 3 6
Input
4
Output
NO
Note
Example from the statement is shown for the first example.
It can be proved that there is no solution in the second example. | instruction | 0 | 63,223 | 12 | 126,446 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
'''input
5
'''
from sys import stdin
import heapq
def check(arr):
arr = arr + arr
# print(arr)
myset = set()
temp = sum(arr[:n])
myset.add(temp)
for i in range(1, 2 *n):
temp -= arr[i]
temp += arr[i + n]
myset.add(temp)
if len(myset) <= 2:
return True
else:
return False
# main starts
n = int(stdin.readline().strip())
arr = [-1] * (2 * n)
first = 1
second = 2
for i in range(n):
arr[i] = first
arr[i + n] = second
first += 2
second += 2
first, second = second, first
if check(arr):
print("YES")
print(*arr)
else:
print("NO")
``` | output | 1 | 63,223 | 12 | 126,447 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied:
For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1.
For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1.
<image>
Input
The first and the only line contain one integer n (1 ≤ n ≤ 10^5).
Output
If there is no solution, output "NO" in the first line.
If there is a solution, output "YES" in the first line. In the second line output 2n numbers — numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them.
Examples
Input
3
Output
YES
1 4 5 2 3 6
Input
4
Output
NO
Note
Example from the statement is shown for the first example.
It can be proved that there is no solution in the second example. | instruction | 0 | 63,224 | 12 | 126,448 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
n = int(input())
if n % 2 == 0:
print('NO')
else:
print('YES')
l = 1
r = 2 * n
while True:
if l < r:
print(l, end= ' ')
else:
break
if l < r - 2:
print(r, end=' ')
l += 2
r -= 2
l = 2
r = 2 * n - 1
while True:
if l < r:
print(l, end=' ')
else:
break
if l < r - 2:
print(r, end= ' ')
l += 2
r -= 2
print(l)
``` | output | 1 | 63,224 | 12 | 126,449 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied:
For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1.
For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1.
<image>
Input
The first and the only line contain one integer n (1 ≤ n ≤ 10^5).
Output
If there is no solution, output "NO" in the first line.
If there is a solution, output "YES" in the first line. In the second line output 2n numbers — numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them.
Examples
Input
3
Output
YES
1 4 5 2 3 6
Input
4
Output
NO
Note
Example from the statement is shown for the first example.
It can be proved that there is no solution in the second example. | instruction | 0 | 63,225 | 12 | 126,450 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
n = int(input())
if n%2 == 0:
print("NO")
else:
print("YES")
a = []
b = []
for i in range(1, 2*n+1, 2):
if i//2%2 == 0:
a += [i]
b += [i+1]
else:
b += [i]
a += [i+1]
print(*a, *b)
``` | output | 1 | 63,225 | 12 | 126,451 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied:
For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1.
For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1.
<image>
Input
The first and the only line contain one integer n (1 ≤ n ≤ 10^5).
Output
If there is no solution, output "NO" in the first line.
If there is a solution, output "YES" in the first line. In the second line output 2n numbers — numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them.
Examples
Input
3
Output
YES
1 4 5 2 3 6
Input
4
Output
NO
Note
Example from the statement is shown for the first example.
It can be proved that there is no solution in the second example. | instruction | 0 | 63,226 | 12 | 126,452 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
n=int(input())
if n%2==0:
print('NO')
else:
print('YES')
ans=1
c=3
for i in range(n):
if i==0:
print(ans,end=' ')
else:
print(ans+c,end=' ')
ans=ans+c
c=1 if c==3 else 3
ans=2
c=1
for i in range(n):
if i==0:
print(ans,end=' ')
else:
print(ans+c,end=' ')
ans=ans+c
c=1 if c==3 else 3
``` | output | 1 | 63,226 | 12 | 126,453 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied:
For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1.
For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1.
<image>
Input
The first and the only line contain one integer n (1 ≤ n ≤ 10^5).
Output
If there is no solution, output "NO" in the first line.
If there is a solution, output "YES" in the first line. In the second line output 2n numbers — numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them.
Examples
Input
3
Output
YES
1 4 5 2 3 6
Input
4
Output
NO
Note
Example from the statement is shown for the first example.
It can be proved that there is no solution in the second example. | instruction | 0 | 63,228 | 12 | 126,456 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import math
# helpful:
# r,g,b=map(int,input().split())
#list1 = input().split()
#for i in range(len(list1)):
# list1[i] = int(list1[i])
# print(list1)
# arr = [[0 for x in range(columns)] for y in range(rows)]
n = int(input())
if(n % 2 == 0):
print("NO")
else:
print("YES")
arr = [0]*(2*n)
incr = False
for i in range(n):
incr = not incr
if(incr):
arr[i] = 2*i + 1
arr[i+n] = 2*i + 2
else:
arr[i+n] = 2*i+1
arr[i] = 2*i + 2
print(' '.join(map(str, arr)))
``` | output | 1 | 63,228 | 12 | 126,457 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12 | instruction | 0 | 63,285 | 12 | 126,570 |
Tags: constructive algorithms
Correct Solution:
```
# [2,4,1,3]
# [2,4,1,3] + [5]
# [2,4,1,5] + [3,6]
# [2,4,1,5] + [3,6,7]
# [2,4,1,5] + [7,3,6]
t = int(input())
for _ in range(t):
A = []
n = int(input())
if n in [2,3]:
print(-1)
continue
for i in range(n//4):
A += [2 + i*4, 4 + i*4, 1 + i*4, 3 + i*4]
if n%4 == 1:
A.append(n)
if n%4 == 2:
A.append(n-1)
A.append(n)
A[-3],A[-2] = A[-2],A[-3]
elif n%4 == 3:
A.append(n-2)
A.append(n-1)
A.append(n)
A[-4],A[-3] = A[-3],A[-4]
A[-3],A[-2] = A[-2],A[-3]
A[-3],A[-1] = A[-1],A[-3]
print(*A)
``` | output | 1 | 63,285 | 12 | 126,571 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12 | instruction | 0 | 63,286 | 12 | 126,572 |
Tags: constructive algorithms
Correct Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
temp=[]
if n<4:
print('-1')
continue
for i in range(n,0,-1):
if (i&1):
temp.append(i)
temp.append(4)
temp.append(2)
for i in range(6,n+1,2):
temp.append(i)
print(*temp)
``` | output | 1 | 63,286 | 12 | 126,573 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12 | instruction | 0 | 63,287 | 12 | 126,574 |
Tags: constructive algorithms
Correct Solution:
```
t=int(input())
for T in range(t):
n=int(input())
if(n<=3):
print(-1)
else:
f=[y for y in range(2,n+1,4)]
m=[z for z in range(4,n+1,4)]
s=[x for x in range(1,n+1,2)]
ans=f+m[: :-1]+s
print(*ans)
``` | output | 1 | 63,287 | 12 | 126,575 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12 | instruction | 0 | 63,288 | 12 | 126,576 |
Tags: constructive algorithms
Correct Solution:
```
for tc in range(int(input())):
n = int(input())
if n < 4:
print(-1)
continue
out = []
for i in range(2 * (n // 2) , 4, -2):
out.append(i)
out.append(2)
out.append(4)
for i in range(1, n + 1, 2):
out.append(i)
print(*out)
``` | output | 1 | 63,288 | 12 | 126,577 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12 | instruction | 0 | 63,289 | 12 | 126,578 |
Tags: constructive algorithms
Correct Solution:
```
def main():
n = int(input())
if n <= 3:
print(-1)
return
s = 2
if n % 2:
s = 1
while s <= n:
print(s, end=" ")
s += 2
s -= 5
print(s, end=" ")
print(s+2, end=" ")
while s > 2:
s -= 2
print(s, end=" ")
print("")
for _ in range(int(input())):
main()
``` | output | 1 | 63,289 | 12 | 126,579 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12 | instruction | 0 | 63,290 | 12 | 126,580 |
Tags: constructive algorithms
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
if(n<4):
print(-1)
else:
if n%2!=0:
if n==5:
l=[1,3,5,2,4]
else:
l=[]
lodd=0
for i in range(1,n+1,2):
l.append(i)
lodd=i
l.append(l[-1]-3)
l.append(l[-2]-1)
l.append(l[-1]-4)
#print(l)
while(l[-1]!=2):
l.append(l[-1]-2)
print(*l)
else:
if n==4:
l=[3,1,4,2]
elif n==6:
l=[1,3,5,2,6,4]
else:
l,d=[],{}
for i in range(1,n,2):
l.append(i)
d[i]=0
l.append(l[-1]-3)
d[l[-1]]=0
l.append(l[-2]+1)
d[l[-1]]=0
while(l[-1]!=2):
if l[-1]-2 not in d.keys():
l.append(l[-1]-2)
else:
l.append(l[-1]-4)
print(*l)
``` | output | 1 | 63,290 | 12 | 126,581 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12 | instruction | 0 | 63,291 | 12 | 126,582 |
Tags: constructive algorithms
Correct Solution:
```
t = int(input())
for _ in range(t):
out = []
works = True
n = int(input())
while n:
if n == 1:
out.append(1)
break
elif n == 2:
works = False
break
elif n == 3:
works = False
break
elif n == 4:
out += [3, 1, 4, 2]
break
elif n == 5:
out += [5,2,4,1,3]
break
elif n == 7:
out += [7, 4, 2, 6, 3, 1, 5]
break
elif n == 8:
out += [8, 4, 7, 3, 6, 2, 5, 1]
break
elif n == 9:
out += [9, 7, 4, 2, 6, 8, 5, 1, 3]
break
else:
out.append(n)
out.append(n - 3)
out.append(n - 1)
out.append(n - 4)
out.append(n - 2)
n -= 5
if works:
print(*out)
else:
print(-1)
``` | output | 1 | 63,291 | 12 | 126,583 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12 | instruction | 0 | 63,292 | 12 | 126,584 |
Tags: constructive algorithms
Correct Solution:
```
# problem 3
T=int(input())
for _ in range(T):
N=int(input())
if N<4:
print(-1)
else:
d=[]
for i in range(N//2,2,-1):
d.append(2*i)
d.append(3)
d.append(1)
d.append(4)
d.append(2)
for i in range(2,N//2):
d.append(2*i+1)
if N%2!=0:
d.append(N)
print(*d)
``` | output | 1 | 63,292 | 12 | 126,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12
Submitted Solution:
```
import math
def r():
return map(int,input().split())
def lis():
return list(map(int,input().split()))
def i():
return int(input())
def si():
return input()
def pYes():
print("YES")
def pNo():
print("NO")
def plist(l):
print("".join(l))
t = int(input())
for i in range(t):
n = int(input())
if n==2 or n==3:
print(-1)
else:
res="3 1 4 2"
for i in range(5,n+1):
if i%2==1:
res=res+" "+str(i)
else:
res=str(i)+" "+res
print(res)
``` | instruction | 0 | 63,293 | 12 | 126,586 |
Yes | output | 1 | 63,293 | 12 | 126,587 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12
Submitted Solution:
```
t = int(input())
for y in range(t):
n = int(input())
ans = []
for i in range(n-(n%2==0),0,-2):
ans.append(i)
ans.append(4)
ans.append(2)
for i in range(6,n+1,2):
ans.append(i)
if(n <= 3):
print(-1)
else:
print(*ans)
``` | instruction | 0 | 63,294 | 12 | 126,588 |
Yes | output | 1 | 63,294 | 12 | 126,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12
Submitted Solution:
```
import sys, math
input = sys.stdin.readline
for i in range(int(input())):
n = int(input())
if(n<4):
print(-1)
elif n==4:
print("3 1 4 2")
else:
j = 1
while j<n:
print(j, end=' ')
j+=2
if j==n:
print(j,j-3,j-1, end=' ')
else:
j-=2
print(j-3,j+1,j-1, end=' ')
j-=5
while j>0:
print(j, end=' ')
j-=2
#r, x1, y1, x2, y2 = [int(c) for c in input().split()]
``` | instruction | 0 | 63,295 | 12 | 126,590 |
Yes | output | 1 | 63,295 | 12 | 126,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12
Submitted Solution:
```
def doit(ans, init, n) :
ans.append(init)
cnt = 0
while ans[-1] + 2 <= n:
if ans[-1] + 4 <= n:
ans.append(ans[-1] + 4)
else :
ans.append(ans[-1] + 2)
cnt = 1
if cnt == 1 :
ans.append(ans[-1] - 4)
else :
ans.append(ans[-1] - 2)
while ans[-1] - 4 >= init - 2:
ans.append(ans[-1] - 4)
def solve() :
n = int(input())
if n < 4:
print(-1)
return
ans = list()
doit(ans, 3, n)
doit(ans, 4, n)
print(' '.join(map(str, ans)))
t = int(input())
while t > 0 :
t -= 1
solve()
``` | instruction | 0 | 63,296 | 12 | 126,592 |
Yes | output | 1 | 63,296 | 12 | 126,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
ans = []
counter = 0
if n <= 3:
print(-1)
else:
if n % 5 == 4:
n -= 4
counter += 4
ans = [3, 1, 4, 2]
while n != 0:
n -= 5
ans.append(counter + 3)
ans.append(counter + 1)
ans.append(counter + 4)
ans.append(counter + 2)
ans.append(counter + 5)
counter += 5
elif n % 5 == 0:
while n != 0:
n -= 5
ans.append(counter + 3)
ans.append(counter + 1)
ans.append(counter + 4)
ans.append(counter + 2)
ans.append(counter + 5)
counter += 5
elif n % 5 == 1:
n -= 6
counter += 6
ans = [1, 3, 6, 4, 2, 5]
while n != 0:
n -= 5
ans.append(counter + 3)
ans.append(counter + 1)
ans.append(counter + 4)
ans.append(counter + 2)
ans.append(counter + 5)
counter += 5
elif n % 5 == 2:
n -= 7
counter += 7
ans = [5, 1, 3, 6, 2, 4, 7]
while n != 0:
n -= 5
ans.append(counter + 3)
ans.append(counter + 1)
ans.append(counter + 4)
ans.append(counter + 2)
ans.append(counter + 5)
counter += 5
else:
n -= 8
counter += 8
ans = [1, 5, 2, 6, 3, 7, 4, 8]
while n != 0:
n -= 5
ans.append(counter + 3)
ans.append(counter + 1)
ans.append(counter + 4)
ans.append(counter + 2)
ans.append(counter + 5)
counter += 5
print(*ans)
``` | instruction | 0 | 63,297 | 12 | 126,594 |
No | output | 1 | 63,297 | 12 | 126,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12
Submitted Solution:
```
from math import factorial
def read(): return [int(i) for i in input().split()]
t = int(input())
for _ in range(t):
n = int(input())
l = []
if n==2:
print(-1)
continue
if n==3:
l=[3,1,2]
print(*l)
continue
for i in range(1,1+(n//2)):
l.append(i)
l.append(i+3)
if n%2:
l.append(n)
print(*l)
``` | instruction | 0 | 63,298 | 12 | 126,596 |
No | output | 1 | 63,298 | 12 | 126,597 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12
Submitted Solution:
```
from collections import deque as dq
for t in range(int(input())):
n=int(input())
if n<=3:
print(-1)
else:
if n%2==1:
ans=[]
for i in range(n,0,-2):
ans.append(i)
for i in range(n-1,0,-2):
ans.append(i)
else:
ans=[]
for i in range(n-1,0,-2):
ans.append(i)
for i in range(n,0,-2):
ans.append(i)
print(*ans)
``` | instruction | 0 | 63,299 | 12 | 126,598 |
No | output | 1 | 63,299 | 12 | 126,599 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array p=[p_1,p_2,...,p_n], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.
For a given number n (n ≥ 2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2 ≤ |p_i - p_{i+1}| ≤ 4 for each i (1 ≤ i < n).
Print any such permutation for the given integer n or determine that it does not exist.
Input
The first line contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the input. Then t test cases follow.
Each test case is described by a single line containing an integer n (2 ≤ n ≤ 1000).
Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
Input
6
10
2
4
6
7
13
Output
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12
Submitted Solution:
```
t = int(input())
out = ''
while t > 0:
t -= 1
n = int(input())
if n < 4:
out += '-1\n'
continue
a = []
for i in reversed(range(1, n, 2)):
a.append(i)
a.append(4)
a.append(2)
for i in range(6, n, 2):
a.append(i)
out += ' '.join(str(i) for i in a) + '\n'
print(out)
``` | instruction | 0 | 63,300 | 12 | 126,600 |
No | output | 1 | 63,300 | 12 | 126,601 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of this problem. The only difference between easy and hard versions is the constraints on k and m (in this version k=2 and m=3). Also, in this version of the problem, you DON'T NEED to output the answer by modulo.
You are given a sequence a of length n consisting of integers from 1 to n. The sequence may contain duplicates (i.e. some elements can be equal).
Find the number of tuples of m = 3 elements such that the maximum number in the tuple differs from the minimum by no more than k = 2. Formally, you need to find the number of triples of indices i < j < z such that
$$$max(a_i, a_j, a_z) - min(a_i, a_j, a_z) ≤ 2.$$$
For example, if n=4 and a=[1,2,4,3], then there are two such triples (i=1, j=2, z=4 and i=2, j=3, z=4). If n=4 and a=[1,1,1,1], then all four possible triples are suitable.
Input
The first line contains a single integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. Then t test cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the sequence a.
The next line contains n integers a_1, a_2,…, a_n (1 ≤ a_i ≤ n) — the sequence a.
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
Output t answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than 2. Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer.
Example
Input
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
Output
2
4
0
15 | instruction | 0 | 63,338 | 12 | 126,676 |
Tags: binary search, combinatorics, math, sortings, two pointers
Correct Solution:
```
# cook your dish here
from sys import stdin
for _ in range(int(stdin.readline())):
n=int(stdin.readline())
a=list(map(int,stdin.readline().split()))
a.sort()
j,c,e=0,0,0
for i in range(2,n):
while a[i]-a[j]>2:
j=j+1
d=((i-j)*(i-j-1))//2
e=e+d
print(e)
``` | output | 1 | 63,338 | 12 | 126,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of this problem. The only difference between easy and hard versions is the constraints on k and m (in this version k=2 and m=3). Also, in this version of the problem, you DON'T NEED to output the answer by modulo.
You are given a sequence a of length n consisting of integers from 1 to n. The sequence may contain duplicates (i.e. some elements can be equal).
Find the number of tuples of m = 3 elements such that the maximum number in the tuple differs from the minimum by no more than k = 2. Formally, you need to find the number of triples of indices i < j < z such that
$$$max(a_i, a_j, a_z) - min(a_i, a_j, a_z) ≤ 2.$$$
For example, if n=4 and a=[1,2,4,3], then there are two such triples (i=1, j=2, z=4 and i=2, j=3, z=4). If n=4 and a=[1,1,1,1], then all four possible triples are suitable.
Input
The first line contains a single integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. Then t test cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the sequence a.
The next line contains n integers a_1, a_2,…, a_n (1 ≤ a_i ≤ n) — the sequence a.
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
Output t answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than 2. Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer.
Example
Input
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
Output
2
4
0
15 | instruction | 0 | 63,339 | 12 | 126,678 |
Tags: binary search, combinatorics, math, sortings, two pointers
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
import math
from collections import defaultdict, deque
from itertools import combinations
import random
# to output - sys.stdout.write('{} {}\n'.format(*a))
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")
# n, k = map(int, input().split(" "))
# l = list(map(int, input().split(" ")))
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split(" ")))
l.sort()
j = 0
ans = 0
for i in range(n):
while j < n and l[j]-l[i] <= 2:
j+=1
ans += max(0, (j-i-1)*(j-i-2)//2)
print(ans)
``` | output | 1 | 63,339 | 12 | 126,679 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of this problem. The only difference between easy and hard versions is the constraints on k and m (in this version k=2 and m=3). Also, in this version of the problem, you DON'T NEED to output the answer by modulo.
You are given a sequence a of length n consisting of integers from 1 to n. The sequence may contain duplicates (i.e. some elements can be equal).
Find the number of tuples of m = 3 elements such that the maximum number in the tuple differs from the minimum by no more than k = 2. Formally, you need to find the number of triples of indices i < j < z such that
$$$max(a_i, a_j, a_z) - min(a_i, a_j, a_z) ≤ 2.$$$
For example, if n=4 and a=[1,2,4,3], then there are two such triples (i=1, j=2, z=4 and i=2, j=3, z=4). If n=4 and a=[1,1,1,1], then all four possible triples are suitable.
Input
The first line contains a single integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. Then t test cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the sequence a.
The next line contains n integers a_1, a_2,…, a_n (1 ≤ a_i ≤ n) — the sequence a.
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
Output t answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than 2. Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer.
Example
Input
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
Output
2
4
0
15 | instruction | 0 | 63,340 | 12 | 126,680 |
Tags: binary search, combinatorics, math, sortings, two pointers
Correct Solution:
```
#const
ans = []
#setting
# import sys
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
#import
import bisect
x_test = 1
if x_test == 0:
x_test = 1
else:
x_test = int(input())
def _main():
def f(x):
if x < 1: return 0
return x * (x + 1) // 2
for _ in range(x_test):
#Body
n = int(input())
a = list(map(int, input().split()))
if n < 3:
ans.append(0)
else:
a.sort()
result = 0
for i in range(2, n):
left_value = a[i] - 2
j = bisect.bisect_left(a, left_value)
x = i - j - 1
result += f(x)
ans.append(result)
if x_test == 200000:
ans = [0] * 200000
else:
_main()
#result
ans = str(ans)
lls = len(ans)
_ans = ans[1:lls - 1].replace(', ', '\n')
print(_ans)
#status:
``` | output | 1 | 63,340 | 12 | 126,681 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of this problem. The only difference between easy and hard versions is the constraints on k and m (in this version k=2 and m=3). Also, in this version of the problem, you DON'T NEED to output the answer by modulo.
You are given a sequence a of length n consisting of integers from 1 to n. The sequence may contain duplicates (i.e. some elements can be equal).
Find the number of tuples of m = 3 elements such that the maximum number in the tuple differs from the minimum by no more than k = 2. Formally, you need to find the number of triples of indices i < j < z such that
$$$max(a_i, a_j, a_z) - min(a_i, a_j, a_z) ≤ 2.$$$
For example, if n=4 and a=[1,2,4,3], then there are two such triples (i=1, j=2, z=4 and i=2, j=3, z=4). If n=4 and a=[1,1,1,1], then all four possible triples are suitable.
Input
The first line contains a single integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. Then t test cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the sequence a.
The next line contains n integers a_1, a_2,…, a_n (1 ≤ a_i ≤ n) — the sequence a.
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
Output t answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than 2. Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer.
Example
Input
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
Output
2
4
0
15 | instruction | 0 | 63,341 | 12 | 126,682 |
Tags: binary search, combinatorics, math, sortings, two pointers
Correct Solution:
```
import math
import string
import random
import sys
from random import randrange
from collections import deque
from collections import defaultdict
from bisect import bisect, bisect_right, bisect_left
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
def fac(x):
if x<2:
return 0
return ((x-1)*x)//2
for t in range(int(data())):
n=int(data())
arr = sorted(mdata())
ans=0
for i in range(n):
b=bisect_right(arr,arr[i]+2)-1
ans+=fac(b-i)
out(ans)
``` | output | 1 | 63,341 | 12 | 126,683 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of this problem. The only difference between easy and hard versions is the constraints on k and m (in this version k=2 and m=3). Also, in this version of the problem, you DON'T NEED to output the answer by modulo.
You are given a sequence a of length n consisting of integers from 1 to n. The sequence may contain duplicates (i.e. some elements can be equal).
Find the number of tuples of m = 3 elements such that the maximum number in the tuple differs from the minimum by no more than k = 2. Formally, you need to find the number of triples of indices i < j < z such that
$$$max(a_i, a_j, a_z) - min(a_i, a_j, a_z) ≤ 2.$$$
For example, if n=4 and a=[1,2,4,3], then there are two such triples (i=1, j=2, z=4 and i=2, j=3, z=4). If n=4 and a=[1,1,1,1], then all four possible triples are suitable.
Input
The first line contains a single integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. Then t test cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the sequence a.
The next line contains n integers a_1, a_2,…, a_n (1 ≤ a_i ≤ n) — the sequence a.
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
Output t answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than 2. Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer.
Example
Input
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
Output
2
4
0
15 | instruction | 0 | 63,342 | 12 | 126,684 |
Tags: binary search, combinatorics, math, sortings, two pointers
Correct Solution:
```
'''Author- Akshit Monga'''
from sys import stdin,stdout
input=stdin.readline
t=int(input())
for _ in range(t):
n=int(input())
arr=[int(x) for x in input().split()]
vals=[0 for x in range(0,n+1)]
for i in arr:
vals[i]+=1
ans=0
for i in range(1,n+1):
a=vals[i]
p=0
if i+1<=n:
p+=vals[i+1]
if i+2<=n:
p+=vals[i+2]
ans+=(a*(p*(p-1)//2)+((a*(a-1)//2)*p))+(a*(a-1)*(a-2))//6
print(ans)
``` | output | 1 | 63,342 | 12 | 126,685 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of this problem. The only difference between easy and hard versions is the constraints on k and m (in this version k=2 and m=3). Also, in this version of the problem, you DON'T NEED to output the answer by modulo.
You are given a sequence a of length n consisting of integers from 1 to n. The sequence may contain duplicates (i.e. some elements can be equal).
Find the number of tuples of m = 3 elements such that the maximum number in the tuple differs from the minimum by no more than k = 2. Formally, you need to find the number of triples of indices i < j < z such that
$$$max(a_i, a_j, a_z) - min(a_i, a_j, a_z) ≤ 2.$$$
For example, if n=4 and a=[1,2,4,3], then there are two such triples (i=1, j=2, z=4 and i=2, j=3, z=4). If n=4 and a=[1,1,1,1], then all four possible triples are suitable.
Input
The first line contains a single integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. Then t test cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the sequence a.
The next line contains n integers a_1, a_2,…, a_n (1 ≤ a_i ≤ n) — the sequence a.
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
Output t answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than 2. Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer.
Example
Input
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
Output
2
4
0
15 | instruction | 0 | 63,343 | 12 | 126,686 |
Tags: binary search, combinatorics, math, sortings, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
def nc3(n):
return n * (n - 1) // 2
for _ in range(int(input())):
n = int(input())
a = sorted([*map(int,input().split())])
i = 0; j = 0; ans = 0
while i < n:
while j + 1 < n and a[j+1] - a[i] <= 2:
j += 1
ans += nc3(j - i)
i += 1
print(ans)
``` | output | 1 | 63,343 | 12 | 126,687 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of this problem. The only difference between easy and hard versions is the constraints on k and m (in this version k=2 and m=3). Also, in this version of the problem, you DON'T NEED to output the answer by modulo.
You are given a sequence a of length n consisting of integers from 1 to n. The sequence may contain duplicates (i.e. some elements can be equal).
Find the number of tuples of m = 3 elements such that the maximum number in the tuple differs from the minimum by no more than k = 2. Formally, you need to find the number of triples of indices i < j < z such that
$$$max(a_i, a_j, a_z) - min(a_i, a_j, a_z) ≤ 2.$$$
For example, if n=4 and a=[1,2,4,3], then there are two such triples (i=1, j=2, z=4 and i=2, j=3, z=4). If n=4 and a=[1,1,1,1], then all four possible triples are suitable.
Input
The first line contains a single integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. Then t test cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the sequence a.
The next line contains n integers a_1, a_2,…, a_n (1 ≤ a_i ≤ n) — the sequence a.
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
Output t answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than 2. Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer.
Example
Input
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
Output
2
4
0
15 | instruction | 0 | 63,344 | 12 | 126,688 |
Tags: binary search, combinatorics, math, sortings, two pointers
Correct Solution:
```
# Not my code
# https://codeforces.com/contest/1462/submission/101312768
import sys
input = lambda: sys.stdin.readline().rstrip()
from bisect import bisect_right
def main():
for _ in range(int(input())):
n=int(input())
a=sorted(list(map(int,input().split())))
s=0
for i in range(n-2):
t=bisect_right(a,a[i]+2)
x=t-i-1
s+=x*(x-1)//2
print(s)
main()
``` | output | 1 | 63,344 | 12 | 126,689 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of this problem. The only difference between easy and hard versions is the constraints on k and m (in this version k=2 and m=3). Also, in this version of the problem, you DON'T NEED to output the answer by modulo.
You are given a sequence a of length n consisting of integers from 1 to n. The sequence may contain duplicates (i.e. some elements can be equal).
Find the number of tuples of m = 3 elements such that the maximum number in the tuple differs from the minimum by no more than k = 2. Formally, you need to find the number of triples of indices i < j < z such that
$$$max(a_i, a_j, a_z) - min(a_i, a_j, a_z) ≤ 2.$$$
For example, if n=4 and a=[1,2,4,3], then there are two such triples (i=1, j=2, z=4 and i=2, j=3, z=4). If n=4 and a=[1,1,1,1], then all four possible triples are suitable.
Input
The first line contains a single integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. Then t test cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the sequence a.
The next line contains n integers a_1, a_2,…, a_n (1 ≤ a_i ≤ n) — the sequence a.
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
Output t answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than 2. Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer.
Example
Input
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
Output
2
4
0
15 | instruction | 0 | 63,345 | 12 | 126,690 |
Tags: binary search, combinatorics, math, sortings, two pointers
Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @oj: codeforces
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020/12/16 10:10
# @url: https://codeforc.es/contest/1462/problem/E1
import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
# region fastio
BUFSIZE = 8192
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")
# ------------------------------
## 注意嵌套括号!!!!!!
## 先有思路,再写代码,别着急!!!
## 先有朴素解法,不要有思维定式,试着换思路解决
## 精度 print("%.10f" % ans)
## sqrt:int(math.sqrt(n))+1
## 字符串拼接不要用+操作,会超时
## 二进制转换:bin(1)[2:].rjust(32,'0')
## array copy:cur=array[::]
## oeis:example 1, 3, _, 1260, _, _, _, _, _, 12164510040883200
## sqrt:Decimal(x).sqrt()避免精度误差
## 无穷大表示:float('inf')
def main():
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
a.sort()
# print(a)
m, k = 3, 2
ans = 0
for j in range(n - m + 1):
if j != 0 and a[j] == a[j - 1]:
if pos != -1:
cnt = pos - j - 1
ans += cnt * (cnt - 1) // 2
continue
pos = -1
for x in range(k + 1):
index = bisect.bisect_right(a, a[j] + x)
if index - j >= m and a[index - 1] == a[j] + x:
pos = max(pos, index)
if pos != -1:
cnt = pos - j - 1
ans += cnt * (cnt - 1) // 2
print(ans)
if __name__ == "__main__":
main()
``` | output | 1 | 63,345 | 12 | 126,691 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant loves permutations of integers from 1 to n very much. But most of all he loves sorting them. To sort a permutation, the Little Elephant repeatedly swaps some elements. As a result, he must receive a permutation 1, 2, 3, ..., n.
This time the Little Elephant has permutation p1, p2, ..., pn. Its sorting program needs to make exactly m moves, during the i-th move it swaps elements that are at that moment located at the ai-th and the bi-th positions. But the Little Elephant's sorting program happened to break down and now on every step it can equiprobably either do nothing or swap the required elements.
Now the Little Elephant doesn't even hope that the program will sort the permutation, but he still wonders: if he runs the program and gets some permutation, how much will the result of sorting resemble the sorted one? For that help the Little Elephant find the mathematical expectation of the number of permutation inversions after all moves of the program are completed.
We'll call a pair of integers i, j (1 ≤ i < j ≤ n) an inversion in permutatuon p1, p2, ..., pn, if the following inequality holds: pi > pj.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 1000, n > 1) — the permutation size and the number of moves. The second line contains n distinct integers, not exceeding n — the initial permutation. Next m lines each contain two integers: the i-th line contains integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the positions of elements that were changed during the i-th move.
Output
In the only line print a single real number — the answer to the problem. The answer will be considered correct if its relative or absolute error does not exceed 10 - 6.
Examples
Input
2 1
1 2
1 2
Output
0.500000000
Input
4 3
1 3 2 4
1 2
2 3
1 4
Output
3.000000000 | instruction | 0 | 63,422 | 12 | 126,844 |
Tags: dp, math, probabilities
Correct Solution:
```
inp = input().split(' ')
val=[];
totNums = int(inp[0]); totOpt = int(inp[1]);
inp = input().split(' '); #assert(len(inp) == totNums);
for it in inp: val.append(int(it))
dp = [[0.0 for _ in range(0,totNums)] for __ in range(0,totNums)]
for i in range(0,totNums):
for j in range(0,totNums):
if val[i]>val[j]: dp[i][j] = 1.0
while totOpt>0:
totOpt -= 1
inp = input().split(' ')
fr = int(inp[0])-1; to = int(inp[1])-1;
for i in range(0,totNums):
if i!=fr and i!=to:
dp[i][fr] = dp[i][to] = (dp[i][fr] + dp[i][to]) / 2;
dp[fr][i] = dp[to][i] = (dp[fr][i] + dp[to][i]) / 2;
dp[fr][to] = dp[to][fr] = (dp[fr][to] + dp[to][fr]) / 2;
ans = 0.0
for i in range(0,totNums):
for j in range(i+1,totNums):
ans += dp[i][j]
print('%.10f'%ans)
``` | output | 1 | 63,422 | 12 | 126,845 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant loves permutations of integers from 1 to n very much. But most of all he loves sorting them. To sort a permutation, the Little Elephant repeatedly swaps some elements. As a result, he must receive a permutation 1, 2, 3, ..., n.
This time the Little Elephant has permutation p1, p2, ..., pn. Its sorting program needs to make exactly m moves, during the i-th move it swaps elements that are at that moment located at the ai-th and the bi-th positions. But the Little Elephant's sorting program happened to break down and now on every step it can equiprobably either do nothing or swap the required elements.
Now the Little Elephant doesn't even hope that the program will sort the permutation, but he still wonders: if he runs the program and gets some permutation, how much will the result of sorting resemble the sorted one? For that help the Little Elephant find the mathematical expectation of the number of permutation inversions after all moves of the program are completed.
We'll call a pair of integers i, j (1 ≤ i < j ≤ n) an inversion in permutatuon p1, p2, ..., pn, if the following inequality holds: pi > pj.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 1000, n > 1) — the permutation size and the number of moves. The second line contains n distinct integers, not exceeding n — the initial permutation. Next m lines each contain two integers: the i-th line contains integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the positions of elements that were changed during the i-th move.
Output
In the only line print a single real number — the answer to the problem. The answer will be considered correct if its relative or absolute error does not exceed 10 - 6.
Examples
Input
2 1
1 2
1 2
Output
0.500000000
Input
4 3
1 3 2 4
1 2
2 3
1 4
Output
3.000000000
Submitted Solution:
```
inp = input().split(' ')
val=[];
totNums = int(inp[0]); totOpt = int(inp[1]);
inp = input().split(' '); assert(len(inp) == totNums);
for it in inp: val.append(it)
dp = [[0.0 for _ in range(0,totNums)] for __ in range(0,totNums)]
for i in range(0,totNums):
for j in range(0,totNums):
if inp[i]>inp[j]: dp[i][j] = 1.0
else: dp[j][i] = 1.0
while totOpt>0:
totOpt -= 1
inp = input().split(' ')
fr = int(inp[0])-1; to = int(inp[1])-1;
for i in range(0,totNums):
if i!=fr and i!=to:
dp[i][fr] = dp[i][to] = (dp[i][fr] + dp[i][to]) / 2;
dp[fr][i] = dp[to][i] = (dp[fr][i] + dp[to][i]) / 2;
dp[fr][to] = dp[to][fr] = (dp[fr][to] + dp[to][fr]) / 2;
ans = 0.0
for i in range(0,totNums):
for j in range(i+1,totNums):
ans += dp[i][j]
print('%.8f'%ans)
``` | instruction | 0 | 63,423 | 12 | 126,846 |
No | output | 1 | 63,423 | 12 | 126,847 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}. | instruction | 0 | 63,424 | 12 | 126,848 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
num=input()
nos=[int(x) for x in input().split()]
stack,ans=[],0
for n in nos:
while stack:
ans=max(ans,stack[-1]^n)
if stack[-1]>n:
break
else:
stack.pop()
stack.append(n)
print(ans)
``` | output | 1 | 63,424 | 12 | 126,849 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}. | instruction | 0 | 63,425 | 12 | 126,850 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
from sys import stdin
n = int(stdin.readline())
s = list(map(int, stdin.readline().split()))
lucky = 0
stack = []
for i in s:
while stack:
lucky = max(lucky, stack[-1]^i)
if i > stack[-1]:
stack.pop()
else:
break
stack.append(i)
print(lucky)
``` | output | 1 | 63,425 | 12 | 126,851 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}. | instruction | 0 | 63,426 | 12 | 126,852 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
size=int(input())
array=list(map(int,input().split()))
mystack=[]
score=None
maxim=0
index=0
while index<size:
if (not mystack) or array[mystack[-1]]>array[index]:
mystack.append(index)
index+=1
else:
while mystack and array[mystack[-1]]<array[index]:
top=mystack.pop()
score=int(array[top] ^ array[index])
maxim=max(score,maxim)
mystack.append(index)
index+=1
mystack.clear()
index=size-1
while index>=0:
if (not mystack) or array[mystack[-1]]>array[index]:
mystack.append(index)
index-=1
else:
while mystack and array[mystack[-1]]<array[index]:
top=mystack.pop()
score=int(array[top] ^ array[index])
maxim=max(score,maxim)
mystack.append(index)
index-=1
print(maxim)
``` | output | 1 | 63,426 | 12 | 126,853 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}. | instruction | 0 | 63,427 | 12 | 126,854 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
n = int(input())
arr = list(map(int, input().split()))
s = []
ans = 0
for i in range(n):
while s != []:
ans = max(ans,arr[i]^s[-1])
if arr[i] < s[-1]:
break
else:
s.pop()
s.append(arr[i])
print(ans)
``` | output | 1 | 63,427 | 12 | 126,855 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}. | instruction | 0 | 63,428 | 12 | 126,856 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
from sys import stdin
lines, line_index = stdin.readlines(), -1
def get_line():
global lines, line_index
line_index += 1
return lines[line_index]
def main():
n = int(get_line())
seq = [int(x) for x in get_line().split()]
stack = []
ans = 0
for val in seq:
while stack:
if stack[-1] < val:
ans = max(ans, stack[-1] ^ val)
stack.pop()
else:
ans = max(ans, stack[-1] ^ val)
break
stack.append(val)
print(ans)
main()
``` | output | 1 | 63,428 | 12 | 126,857 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}. | instruction | 0 | 63,429 | 12 | 126,858 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
m = 0
stack = []
for i in a:
while stack:
m = max(m, stack[-1]^i)
if stack[-1]>i:
break
else:
stack.pop()
stack.append(i)
print(m)
``` | output | 1 | 63,429 | 12 | 126,859 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}. | instruction | 0 | 63,430 | 12 | 126,860 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
from collections import deque
def ngr(a,n):
s=deque()
q=deque()
l=[]
for j in range(n-1,-1,-1):
if len(s)==0:
s.append(a[j])
else:
while len(s)!=0 and s[len(s)-1]<=a[j]:
s.pop()
if len(s)==0:
pass
else:
l.append(s[len(s)-1]^a[j])
s.append(a[j])
return l
def ngl(a,n):
s=deque()
l=[]
for j in range(n):
if len(s)==0:
s.append(a[j])
else:
while len(s)!=0 and s[len(s)-1]<=a[j]:
s.pop()
if len(s)==0:
pass
else:
l.append(s[len(s)-1]^a[j])
s.append(a[j])
return l
n=int(input())
a=list(map(int,input().split()))
c=ngr(a,n)
d=ngl(a,n)
c.extend(d)
print(max(c))
``` | output | 1 | 63,430 | 12 | 126,861 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}. | instruction | 0 | 63,431 | 12 | 126,862 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
#http://codeforces.com/problemset/problem/281/D
#SC = TC = o(n)
n = int(input())
a = list(map(int, input().strip().split()))
stack = [] #decreasing stack
max_xor = 0
for k in range(n):
#if stack empty
if not stack:
stack.append(a[k])
#if less than top of stack
elif a[k]<stack[-1]:
max_xor = max(max_xor, a[k]^stack[-1])
stack.append(a[k])
#input will be distinct so no equal condition
#if greater than top of stack
elif a[k]>stack[-1]:
#pop and xor
while stack and a[k]>stack[-1]:
popped = stack.pop()
max_xor = max(max_xor, a[k]^popped)
#now if stack is not empty means below element is greater
#so xor will it and push #case 9 8 3 5 7
if stack:
max_xor = max(max_xor, a[k]^stack[-1])
stack.append(a[k])
print(max_xor)
``` | output | 1 | 63,431 | 12 | 126,863 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}.
Submitted Solution:
```
n=int(input())
a=[int(x) for x in input().split()]
st=[a[0]]
ans=[]
for i in range(1,n):
if(a[i]<st[-1]):
ans.append(a[i]^st[-1])
st.append(a[i])
else:
while(1):
if(st==[]):
st.append(a[i])
break
if(a[i]>st[-1]):
ans.append(a[i]^st[-1])
st.pop()
elif(a[i]<st[-1]):
ans.append(a[i]^st[-1])
st.append(a[i])
break
print(max(ans))
``` | instruction | 0 | 63,432 | 12 | 126,864 |
Yes | output | 1 | 63,432 | 12 | 126,865 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}.
Submitted Solution:
```
'''
10
4547989 39261040 94929326 38131456 26174500 7152864 71295827 77784626 89898294 68006331
'''
def maxxor(a,b,ans):
t = a^b
#print('{} {}'.format(a,b))
if t > ans: return t
else: return ans
n = int(input().strip())
arr = list(map(int, input().strip().split()))
s = list()
s.append(arr[0])
s.append(arr[1])
ans = maxxor(arr[0], arr[1], 0)
index = 2
while index < n:
#print('ans = {} index = {} s = {}'.format(ans,index, s))
if arr[index] > s[-1]:
while (arr[index] > s[-1]):
t = s.pop()
ans = maxxor(arr[index], t, ans)
if len(s) == 0: break
if len(s) != 0: ans = maxxor(arr[index], s[-1], ans)
s.append(arr[index])
index += 1
print(ans)
``` | instruction | 0 | 63,433 | 12 | 126,866 |
Yes | output | 1 | 63,433 | 12 | 126,867 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}.
Submitted Solution:
```
def maximum_xor_secondary(sequence):
stack, answer = [], 0
for x in sequence:
while stack:
answer = max(answer, stack[-1] ^ x)
if stack[-1] > x:
break
else:
stack.pop()
stack.append(x)
return answer
size, num = input(), [int(x) for x in input().split()]
print(maximum_xor_secondary(num))
``` | instruction | 0 | 63,434 | 12 | 126,868 |
Yes | output | 1 | 63,434 | 12 | 126,869 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>.
The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}.
Submitted Solution:
```
n=int(input())
arr=[int(s) for s in input().split()]
stck=[]
ans=0
for i in arr:
# print(i,stck)
if not stck:
stck.append(i)
else:
while(stck and i>stck[len(stck)-1]):
ans=max(ans,i^stck[len(stck)-1])
stck.pop(len(stck)-1)
if stck:
ans=max(ans,i^stck[len(stck)-1])
stck.append(i)
# print(stck)
print(ans)
``` | instruction | 0 | 63,435 | 12 | 126,870 |
Yes | output | 1 | 63,435 | 12 | 126,871 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.