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.
A piece of paper contains an array of n integers a1, a2, ..., an. Your task is to find a number that occurs the maximum number of times in this array.
However, before looking for such number, you are allowed to perform not more than k following operations — choose an arbitrary element from the array and add 1 to it. In other words, you are allowed to increase some array element by 1 no more than k times (you are allowed to increase the same element of the array multiple times).
Your task is to find the maximum number of occurrences of some number in the array after performing no more than k allowed operations. If there are several such numbers, your task is to find the minimum one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105; 0 ≤ k ≤ 109) — the number of elements in the array and the number of operations you are allowed to perform, correspondingly.
The third line contains a sequence of n integers a1, a2, ..., an (|ai| ≤ 109) — the initial array. The numbers in the lines are separated by single spaces.
Output
In a single line print two numbers — the maximum number of occurrences of some number in the array after at most k allowed operations are performed, and the minimum number that reaches the given maximum. Separate the printed numbers by whitespaces.
Examples
Input
5 3
6 3 4 0 2
Output
3 4
Input
3 4
5 5 5
Output
3 5
Input
5 3
3 1 2 2 1
Output
4 2
Note
In the first sample your task is to increase the second element of the array once and increase the fifth element of the array twice. Thus, we get sequence 6, 4, 4, 0, 4, where number 4 occurs 3 times.
In the second sample you don't need to perform a single operation or increase each element by one. If we do nothing, we get array 5, 5, 5, if we increase each by one, we get 6, 6, 6. In both cases the maximum number of occurrences equals 3. So we should do nothing, as number 5 is less than number 6.
In the third sample we should increase the second array element once and the fifth element once. Thus, we get sequence 3, 2, 2, 2, 2, where number 2 occurs 4 times.
Submitted Solution:
```
from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
def main():
n, k = [ int(x) for x in input().split() ]
array = [ int(x) for x in input().split() ]
array.sort()
accumulated = [0] * n
accumulated[0] = array[0]
for i in range(1, n):
accumulated[i] = accumulated[i-1] + array[i]
maxNumOfOccurrences = 1
minNumThatMeetsCondition = array[-1]
for i in range(1, n):
left, right = 0, i - 1
while left <= right:
mid = (left + right) // 2
expectedSum = array[i]*(i - mid + 1)
realSum = accumulated[i] - (
accumulated[left - 1] if left > 0 else 0
)
numOfOperations = expectedSum - realSum
if numOfOperations <= k:
right = mid - 1
else:
left = mid + 1
if numOfOperations > k:
mid += 1
if i - mid + 1 > maxNumOfOccurrences:
maxNumOfOccurrences = i - mid + 1
minNumThatMeetsCondition = array[i]
print(maxNumOfOccurrences, minNumThatMeetsCondition)
# binary search
# sort the array of numbers
# array de soma acumulada
# for each i -> get the max number of previous elements that
# can be transformed into array[i], without exceeding k
# if ok -> high = mid - 1
# else -> low = mid + 1
# remember to check if the sum at mid is still lower or equal than
# k after finding the answer
BUFFSIZE = 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, BUFFSIZE))
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, BUFFSIZE))
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")
input = lambda: sys.stdin.readline().rstrip("\r\n")
def print(*args, **kwargs):
sep = kwargs.pop("sep", " ")
file = kwargs.pop("file", sys.stdout)
atStart = True
for x in args:
if not atStart:
file.write(sep)
file.write(str(x))
atStart = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
main()
``` | instruction | 0 | 80,230 | 12 | 160,460 |
No | output | 1 | 80,230 | 12 | 160,461 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A piece of paper contains an array of n integers a1, a2, ..., an. Your task is to find a number that occurs the maximum number of times in this array.
However, before looking for such number, you are allowed to perform not more than k following operations — choose an arbitrary element from the array and add 1 to it. In other words, you are allowed to increase some array element by 1 no more than k times (you are allowed to increase the same element of the array multiple times).
Your task is to find the maximum number of occurrences of some number in the array after performing no more than k allowed operations. If there are several such numbers, your task is to find the minimum one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105; 0 ≤ k ≤ 109) — the number of elements in the array and the number of operations you are allowed to perform, correspondingly.
The third line contains a sequence of n integers a1, a2, ..., an (|ai| ≤ 109) — the initial array. The numbers in the lines are separated by single spaces.
Output
In a single line print two numbers — the maximum number of occurrences of some number in the array after at most k allowed operations are performed, and the minimum number that reaches the given maximum. Separate the printed numbers by whitespaces.
Examples
Input
5 3
6 3 4 0 2
Output
3 4
Input
3 4
5 5 5
Output
3 5
Input
5 3
3 1 2 2 1
Output
4 2
Note
In the first sample your task is to increase the second element of the array once and increase the fifth element of the array twice. Thus, we get sequence 6, 4, 4, 0, 4, where number 4 occurs 3 times.
In the second sample you don't need to perform a single operation or increase each element by one. If we do nothing, we get array 5, 5, 5, if we increase each by one, we get 6, 6, 6. In both cases the maximum number of occurrences equals 3. So we should do nothing, as number 5 is less than number 6.
In the third sample we should increase the second array element once and the fifth element once. Thus, we get sequence 3, 2, 2, 2, 2, where number 2 occurs 4 times.
Submitted Solution:
```
temp=input()
temp=temp.split()
n=int(temp[0])
k=int(temp[1])
a=input()
a=a.split()
for i in range(len(a)):
a[i]=int(a[i])
a.sort()
index=n-1
q=index-1
maxi=0
while index>=0:
tempmax=1
while (k>=(a[index]-a[q]) and q>=0):
k=k-(a[index]-a[q])
q-=1
tempmax+=1
if tempmax>maxi:
maxi=tempmax
target=a[index]
index-=1
q=index
print(maxi,target)
``` | instruction | 0 | 80,231 | 12 | 160,462 |
No | output | 1 | 80,231 | 12 | 160,463 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A piece of paper contains an array of n integers a1, a2, ..., an. Your task is to find a number that occurs the maximum number of times in this array.
However, before looking for such number, you are allowed to perform not more than k following operations — choose an arbitrary element from the array and add 1 to it. In other words, you are allowed to increase some array element by 1 no more than k times (you are allowed to increase the same element of the array multiple times).
Your task is to find the maximum number of occurrences of some number in the array after performing no more than k allowed operations. If there are several such numbers, your task is to find the minimum one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105; 0 ≤ k ≤ 109) — the number of elements in the array and the number of operations you are allowed to perform, correspondingly.
The third line contains a sequence of n integers a1, a2, ..., an (|ai| ≤ 109) — the initial array. The numbers in the lines are separated by single spaces.
Output
In a single line print two numbers — the maximum number of occurrences of some number in the array after at most k allowed operations are performed, and the minimum number that reaches the given maximum. Separate the printed numbers by whitespaces.
Examples
Input
5 3
6 3 4 0 2
Output
3 4
Input
3 4
5 5 5
Output
3 5
Input
5 3
3 1 2 2 1
Output
4 2
Note
In the first sample your task is to increase the second element of the array once and increase the fifth element of the array twice. Thus, we get sequence 6, 4, 4, 0, 4, where number 4 occurs 3 times.
In the second sample you don't need to perform a single operation or increase each element by one. If we do nothing, we get array 5, 5, 5, if we increase each by one, we get 6, 6, 6. In both cases the maximum number of occurrences equals 3. So we should do nothing, as number 5 is less than number 6.
In the third sample we should increase the second array element once and the fifth element once. Thus, we get sequence 3, 2, 2, 2, 2, where number 2 occurs 4 times.
Submitted Solution:
```
n,k=input().split()
n=int(n)
k=int(k)
l=[int(x) for x in input().split()]
l.sort()
dicti={}
for i in range(n):
if(l[i] not in dicti):
dicti[l[i]]=1
else:
dicti[l[i]]+=1
a=k
m=dicti.copy()
ll=list(dicti.keys())
for i in range(len(ll)):
a=k
for j in range(i-1,-1,-1):
if(a==0):
break
else:
b=(ll[i]-ll[j])
if(a>b*dicti[ll[j]]):
m[ll[i]]+=dicti[ll[j]]
a-=b
else:
m[ll[i]]+=a//b
break
b=max(m,key=m.get)
print(m[b],b)
``` | instruction | 0 | 80,232 | 12 | 160,464 |
No | output | 1 | 80,232 | 12 | 160,465 |
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 | 80,249 | 12 | 160,498 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
RI = lambda : [int(x) for x in input().split()]
rw = lambda : [input().strip()]
n=int(input())
l=RI()
stack=[]
index_stack=-1
maxo=0
i=0
while(i<n):
if(index_stack==-1 or stack[index_stack]>l[i]):
stack.append(l[i])
index_stack+=1
i+=1
else:
#print(stack[index_stack],end=" ")
#print(l[i])
maxo=max(stack[index_stack]^l[i],maxo)
temp=stack[index_stack]
stack.pop()
index_stack-=1
if(len(stack)!=0):
#print(stack[index_stack],end=" ")
#print(stack[index_stack])
maxo=max(temp^stack[index_stack],maxo)
while(len(stack)!=1):
temp=stack[index_stack]
stack.pop()
index_stack-=1
#print(stack[index_stack],end=" ")
#print(stack[index_stack])
maxo=max(temp^stack[index_stack],maxo)
print(maxo)
``` | output | 1 | 80,249 | 12 | 160,499 |
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 | 80,250 | 12 | 160,500 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
# https://codeforces.com/problemset/problem/281/D
def solve(array):
st = []
res = array[0] ^ array[1]
for i in range(n):
while(len(st) > 0 and array[i] >= st[-1]):
st.pop()
st.append(array[i])
if len(st) >= 2:
res = max(res, st[-1] ^ st[-2])
return res
n = int(input())
array = list(map(int, input().split()))
res = solve(array)
res = max(res, solve(array[::-1]))
print(res)
``` | output | 1 | 80,250 | 12 | 160,501 |
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 | 80,251 | 12 | 160,502 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
from sys import stdin
ip = stdin.readline
ip()
a = list(map(int, ip().split()))
stk = []
ans = 0
for i in a:
while stk:
ans = max(ans, stk[-1]^i)
if stk[-1]>i: break
stk.pop()
stk.append(i)
print(ans)
``` | output | 1 | 80,251 | 12 | 160,503 |
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 | 80,252 | 12 | 160,504 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
n = int(input())
li = [int(x) for x in input().split()]
monD = []
ans = 0
for i, j in zip( li, range( 0, len(li) ) ):
if j == 0:
monD.append(i)
else:
#print(monD, "adsd", monD[-1])
fl = 1
if monD[-1] > i:
ans = max(ans, i ^ monD[-1])
monD.append(i)
else:
while monD[-1] < i:
ans = max(ans, i ^ monD[-1])
_ = monD.pop()
if len(monD) == 0:
fl = 0
break
if fl != 0:
ans = max(ans, i ^ monD[-1])
monD.append(i)
print(ans)
``` | output | 1 | 80,252 | 12 | 160,505 |
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 | 80,253 | 12 | 160,506 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
def max_ex(nums):
maximum = float('-inf')
stack=[]
for i in range(len(nums)):
if not stack:
stack.append(nums[i])
else:
while stack and stack[-1]<nums[i]:
maximum = max(maximum, stack[-1]^nums[i])
stack.pop()
if stack:
maximum = max(maximum, stack[-1]^nums[i])
stack.append(nums[i])
if stack:
temp=stack.pop()
while stack:
maximum=max(maximum, temp^stack[-1])
temp = stack.pop()
return maximum
n = int(input())
arr = [int(i) for i in input().split()]
print(max_ex(arr))
``` | output | 1 | 80,253 | 12 | 160,507 |
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 | 80,254 | 12 | 160,508 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
n=int(input())
l=[int(i) for i in input().split()]
ans=0
stack=[]
for i in range(n):
while len(stack)!=0:
if l[i]>stack[len(stack)-1]:
ans=max(ans,l[i]^stack[len(stack)-1])
stack.pop()
else:
ans=max(ans,l[i]^stack[len(stack)-1])
break
stack.append(l[i])
print(ans)
``` | output | 1 | 80,254 | 12 | 160,509 |
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 | 80,255 | 12 | 160,510 |
Tags: data structures, implementation, two pointers
Correct Solution:
```
n = int(input())
s = list(map(int,input().split()))
stack = []
ans = 0
for i in s:
while stack and stack[-1]<i:
t = stack.pop()
ans = max(ans,t^i)
stack.append(i)
if stack[0]!=stack[-1]:
ans = max(ans,stack[-1]^stack[-2])
print(ans)
``` | output | 1 | 80,255 | 12 | 160,511 |
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 | 80,256 | 12 | 160,512 |
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 | 80,256 | 12 | 160,513 |
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 = 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)
``` | instruction | 0 | 80,257 | 12 | 160,514 |
Yes | output | 1 | 80,257 | 12 | 160,515 |
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))
# Made By Mostafa_Khaled
``` | instruction | 0 | 80,258 | 12 | 160,516 |
Yes | output | 1 | 80,258 | 12 | 160,517 |
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=list(map(int,input().split()))
stack = []#[a[0],a[1]]
xor = 0#stack[0]^stack[1]
for i in range(n):
while(stack!=[] and stack[-1]<a[i]):
xor= max(xor,a[i]^stack[-1])
stack.pop()
# print("while",stack)
if stack!=[] :
xor= max(xor,a[i]^stack[-1])
# print("if",stack)
stack.append(a[i])
# print(stack)
print(xor)
``` | instruction | 0 | 80,259 | 12 | 160,518 |
Yes | output | 1 | 80,259 | 12 | 160,519 |
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:
```
import sys,os
from io import BytesIO,IOBase
mod = 10**9+7; Mod = 998244353; INF = float('inf')
# input = lambda: sys.stdin.readline().rstrip("\r\n")
# inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split()))
#______________________________________________________________________________________________________
# region fastio
# '''
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# endregion'''
#______________________________________________________________________________________________________
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split()))
# ______________________________________________________________________________________________________
# from math import *
# from bisect import *
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
# from collections import Counter as cc
# from collections import deque
# from itertools import groupby
# from functools import lru_cache
# from itertools import combinations
# sys.setrecursionlimit(100_100) #this is must for dfs
# ______________________________________________________________________________________________________
# segment tree for range minimum query and update 0 indexing
# init = float('inf')
# st = [init for i in range(4*len(a))]
# def build(a,ind,start,end):
# if start == end:
# st[ind] = a[start]
# else:
# mid = (start+end)//2
# build(a,2*ind+1,start,mid)
# build(a,2*ind+2,mid+1,end)
# st[ind] = min(st[2*ind+1],st[2*ind+2])
# build(a,0,0,n-1)
# def query(ind,l,r,start,end):
# if start>r or end<l:
# return init
# if l<=start<=end<=r:
# return st[ind]
# mid = (start+end)//2
# return min(query(2*ind+1,l,r,start,mid),query(2*ind+2,l,r,mid+1,end))
# def update(ind,val,stind,start,end):
# if start<=ind<=end:
# if start==end:
# st[stind] = a[start] = val
# else:
# mid = (start+end)//2
# update(ind,val,2*stind+1,start,mid)
# update(ind,val,2*stind+2,mid+1,end)
# st[stind] = min(st[left],st[right])
# ______________________________________________________________________________________________________
# Checking prime in O(root(N))
# def isprime(n):
# if (n % 2 == 0 and n > 2) or n == 1: return 0
# else:
# s = int(n**(0.5)) + 1
# for i in range(3, s, 2):
# if n % i == 0:
# return 0
# return 1
# def lcm(a,b):
# return (a*b)//gcd(a,b)
# returning factors in O(root(N))
# def factors(n):
# fact = []
# N = int(n**0.5)+1
# for i in range(1,N):
# if (n%i==0):
# fact.append(i)
# if (i!=n//i):
# fact.append(n//i)
# return fact
# ______________________________________________________________________________________________________
# Merge sort for inversion count
# def mergeSort(left,right,arr,temp):
# inv_cnt = 0
# if left<right:
# mid = (left+right)//2
# inv1 = mergeSort(left,mid,arr,temp)
# inv2 = mergeSort(mid+1,right,arr,temp)
# inv3 = merge(left,right,mid,arr,temp)
# inv_cnt = inv1+inv3+inv2
# return inv_cnt
# def merge(left,right,mid,arr,temp):
# i = left
# j = mid+1
# k = left
# inv = 0
# while(i<=mid and j<=right):
# if(arr[i]<=arr[j]):
# temp[k] = arr[i]
# i+=1
# else:
# temp[k] = arr[j]
# inv+=(mid+1-i)
# j+=1
# k+=1
# while(i<=mid):
# temp[k]=arr[i]
# i+=1
# k+=1
# while(j<=right):
# temp[k]=arr[j]
# j+=1
# k+=1
# for k in range(left,right+1):
# arr[k] = temp[k]
# return inv
# ______________________________________________________________________________________________________
# nCr under mod
# def C(n,r,mod = 10**9+7):
# if r>n: return 0
# if r>n-r: r = n-r
# num = den = 1
# for i in range(r):
# num = (num*(n-i))%mod
# den = (den*(i+1))%mod
# return (num*pow(den,mod-2,mod))%mod
# def C(n,r):
# if r>n:
# return 0
# if r>n-r:
# r = n-r
# ans = 1
# for i in range(r):
# ans = (ans*(n-i))//(i+1)
# return ans
# ______________________________________________________________________________________________________
# For smallest prime factor of a number
# M = 5*10**5+100
# spf = [i for i in range(M)]
# def spfs(M):
# for i in range(2,M):
# if spf[i]==i:
# for j in range(i*i,M,i):
# if spf[j]==j:
# spf[j] = i
# return
# spfs(M)
# p = [0]*M
# for i in range(2,M):
# p[i]+=(p[i-1]+(spf[i]==i))
# ______________________________________________________________________________________________________
# def gtc(p):
# print('Case #'+str(p)+': ',end='')
# ______________________________________________________________________________________________________
tc = 1
# tc = int(input())
for test in range(1,tc+1):
n = int(input())
a = inp()
Prev = [i for i in range(n)]
Next = [i for i in range(n)]
stack = []
for i in range(n):
while(stack and a[stack[-1]]<=a[i]):
stack.pop()
if stack:
Prev[i] = stack[-1]
stack.append(i)
stack = []
for i in range(n-1,-1,-1):
while(stack and a[stack[-1]]<=a[i]):
stack.pop()
if stack:
Next[i] = stack[-1]
stack.append(i)
ans = 0
for i in range(n):
ans = max(ans,a[i]^a[Prev[i]])
ans = max(ans,a[i]^a[Next[i]])
print(ans)
``` | instruction | 0 | 80,260 | 12 | 160,520 |
Yes | output | 1 | 80,260 | 12 | 160,521 |
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:
```
# cook your dish here
t=input()
l=sorted(list(map(int,input().split())))
print(l[-2]^l[-3])
``` | instruction | 0 | 80,261 | 12 | 160,522 |
No | output | 1 | 80,261 | 12 | 160,523 |
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 countBits(n):
count = 0
while (n):
count += 1
n >>= 1
return count
def btd(binary):
binary1 = binary
decimal, i, n = 0, 0, 0
while (binary != 0):
dec = binary % 10
decimal = decimal + dec * pow(2, i)
binary = binary // 10
i += 1
return decimal
n = int(input())
s = list(map(int, input().split()))
d = s
p = list()
q = 0
num = 0
for i in range(n):
a = max(d)
c = countBits(a)
num = 0
for k in range(c):
num += 1 * (10 ** k)
#print("num=", num)
c = btd(num)
#print("c=", c)
b = a ^ c
#print("a= {} b={} c={}".format(a, b, c))
if b in d:
i1 = d.index(a)
i2 = d.index(b)
#print(i1 ,i2)
if i2>i1:
#print(d[i1:i2+1])
M1 = max(d[i1:i2+1])
M2 = max(d[i1 + 1:i2 + 1])
else:
d.remove(a)
continue
#print("i1={} i2={} M1={} M2={} and d={}".format(i1, i2, M1, M2, d))
if M1 == a and M2 == b:
#print("c>q",c,q)
if q < c:
q = c
#print("q={} d={}".format(q, d))
#print("a=",a)
#print(d)
d.remove(a)
#print(d)
print(q)
``` | instruction | 0 | 80,262 | 12 | 160,524 |
No | output | 1 | 80,262 | 12 | 160,525 |
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:
```
# A simple Python program
# to find max subarray XOR
def maxSubarrayXOR(arr, n):
ans = -2147483648 # Initialize result
# Pick starting points of subarrays
for i in range(n):
# to store xor of current subarray
curr_xor = 0
# Pick ending points of
# subarrays starting with i
for j in range(i, n):
curr_xor = curr_xor ^ arr[j]
ans = max(ans, curr_xor)
return ans
# Driver code
arr = [8, 1, 2, 12]
n = len(arr)
print("Max subarray XOR is ",
maxSubarrayXOR(arr, n))
``` | instruction | 0 | 80,263 | 12 | 160,526 |
No | output | 1 | 80,263 | 12 | 160,527 |
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())
li = [int(x) for x in input().split()]
monD = []
ans = 0
for i, j in zip( li, range( 0, len(li) ) ):
if j == 0:
monD.append(i)
else:
print(monD, "adsd", monD[-1])
fl = 1
if monD[-1] > i:
ans = max(ans, i ^ monD[-1])
monD.append(i)
else:
while monD[-1] < i:
ans = max(ans, i ^ monD[-1])
_ = monD.pop()
if len(monD) == 0:
fl = 0
break
if fl != 0:
ans = max(ans, i ^ monD[-1])
monD.append(i)
print(ans)
``` | instruction | 0 | 80,264 | 12 | 160,528 |
No | output | 1 | 80,264 | 12 | 160,529 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists. | instruction | 0 | 80,265 | 12 | 160,530 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect, insort
from time import perf_counter
from fractions import Fraction
import copy
from copy import deepcopy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
try:
# sys.setrecursionlimit(int(pow(10,6)))
sys.stdin = open("input.txt", "r")
# sys.stdout = open("../output.txt", "w")
except:
pass
def pmat(A):
for ele in A:
print(*ele,end="\n")
n=L()[0]
A=[i for i in range(n)]
B=[(i-1)%n for i in range(n)]
if n%2==0:
print(-1)
else:
print(*A)
print(*B)
C=[(A[i]+B[i])%n for i in range(n)]
print(*C)
endtime = time.time()
# print(f"Runtime of the program is {endtime - starttime}")
``` | output | 1 | 80,265 | 12 | 160,531 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists. | instruction | 0 | 80,266 | 12 | 160,532 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
n = int(input())
if n % 2 == 0:
print(-1)
else:
print(*range(n))
print(*range(n))
print(*((i + i) % n for i in range(n)))
``` | output | 1 | 80,266 | 12 | 160,533 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists. | instruction | 0 | 80,267 | 12 | 160,534 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
n=int(input())
if n%2:
a=[i for i in range(n)]
b=[i for i in range(n)]
c=[(a[i]+b[i])%n for i in range(n)]
print(*a)
print(*b)
print(*c)
else:
print(-1)
``` | output | 1 | 80,267 | 12 | 160,535 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists. | instruction | 0 | 80,268 | 12 | 160,536 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
n = int(input())
if n % 2 == 0:
print(-1)
else:
print(*range(n))
print(*range(n))
print(*map(lambda x: x * 2 % n, range(n)))
``` | output | 1 | 80,268 | 12 | 160,537 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists. | instruction | 0 | 80,269 | 12 | 160,538 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
n = int(input())
if n % 2 == 1:
for i in range(n):
print(i, end=" ")
print()
for i in range(n):
print((i + 1)%n, end=" ")
print()
for i in range(n):
print((i + (i + 1)%n)%n, end=" ")
else:
print(-1)
``` | output | 1 | 80,269 | 12 | 160,539 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists. | instruction | 0 | 80,270 | 12 | 160,540 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
n=int(input())
a,b=[],[]
c=[]
if(n%2!=0):
for i in range(n):
a+=[i]
b+=[i]
c.append((a[i]+b[i])%n)
print(*a)
print(*b)
print(*c)
else:
print(-1)
``` | output | 1 | 80,270 | 12 | 160,541 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists. | instruction | 0 | 80,271 | 12 | 160,542 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
n = int(input())
if n % 2 == 0:
print('-1')
else:
a = range(n)
print(' '.join(map(str, a)))
print(' '.join(map(str, a)))
print(' '.join(map(lambda x: str((x[0] + x[1]) % n), zip(a, a))))
``` | output | 1 | 80,271 | 12 | 160,543 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists. | instruction | 0 | 80,272 | 12 | 160,544 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
n = int(input())
if n % 2 == 0:
print(-1)
else:
lista = list(range(n))
saida = list(map(lambda x: (2*x)%n, lista))
print(' '.join(map(str, lista)))
print(' '.join(map(str, lista)))
print(' '.join(map(str, saida)))
``` | output | 1 | 80,272 | 12 | 160,545 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists.
Submitted Solution:
```
n=int(input())
arr=[i for i in range(n)]
#print(arr)
ans=[]
for i in range(n):
ans.append((2*i)%n)
if sorted(ans)!=arr:print(-1)
else:print(*arr);print(*arr);print(*ans)
``` | instruction | 0 | 80,273 | 12 | 160,546 |
Yes | output | 1 | 80,273 | 12 | 160,547 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists.
Submitted Solution:
```
n=int(input())
if (n%2==0): print(-1)
else:
for i in range(n): print(i,end=' ')
print('')
for i in range(n): print(i,end=' ')
print('')
for i in range(n): print((2*i)%n,end=' ')
``` | instruction | 0 | 80,274 | 12 | 160,548 |
Yes | output | 1 | 80,274 | 12 | 160,549 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists.
Submitted Solution:
```
r = int(input())
if r % 2 == 0:
print (-1)
elif r == 1:
print(0)
print(0)
print(0)
else:
for i in range(r):
print(i,end=" ")
print()
i = r - 1
while i >= 0:
print(i,end=" ")
i -= 2
i = r - 2
while i >= 0:
print(i,end=" ")
i -= 2
print()
for i in range(r - 1, -1, -1):
print(i,end=" ")
``` | instruction | 0 | 80,275 | 12 | 160,550 |
Yes | output | 1 | 80,275 | 12 | 160,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists.
Submitted Solution:
```
n = int(input())
if n % 2 == 0:
print(-1)
else:
print(*list(range(n)))
print(*list(range(n)))
a = list()
for i in range(n):
a.append((i + i) % n)
print(*a)
``` | instruction | 0 | 80,276 | 12 | 160,552 |
Yes | output | 1 | 80,276 | 12 | 160,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists.
Submitted Solution:
```
import math
n=int(input())
#n,m = map(int, input().strip().split(' '))
#lst = list(map(int, input().strip().split(' ')))
l=[]
l1=[]
if n==2:
print(-1)
else:
for j in range(n):
l.append(j)
l1.append(j+1)
print(j,end=" ")
print()
l1[-1]=0
for j in range(n):
print(l1[j],end=" ")
print()
for j in range(n):
k=(l[j]+l1[j])%n
print(k,end=" ")
``` | instruction | 0 | 80,277 | 12 | 160,554 |
No | output | 1 | 80,277 | 12 | 160,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists.
Submitted Solution:
```
a=int(input())
if a<3 or a%2==0:print(-1)
else:
z=[*range(a-1,-1,-1)]
z1=[a-2]+[*range(a-3,-1,-1)]+[a-1]
print(*z);print(*z1)
for i in range(a):print((z[i]+z1[i])%a,end=' ')
``` | instruction | 0 | 80,278 | 12 | 160,556 |
No | output | 1 | 80,278 | 12 | 160,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists.
Submitted Solution:
```
import math
a = int (input())
b = int(math.log(a,2))
print (a-(2**(b))+1)
``` | instruction | 0 | 80,279 | 12 | 160,558 |
No | output | 1 | 80,279 | 12 | 160,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.
A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.
Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?
Input
The first line contains a single integer n (1 ≤ n ≤ 105).
Output
If no Lucky Permutation Triple of length n exists print -1.
Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c.
If there are multiple solutions, print any of them.
Examples
Input
5
Output
1 4 3 2 0
1 0 2 4 3
2 4 0 1 3
Input
2
Output
-1
Note
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:
* <image>;
* <image>;
* <image>;
* <image>;
* <image>.
In Sample 2, you can easily notice that no lucky permutation triple exists.
Submitted Solution:
```
n = int(input())
if n % 2 == 1:
for i in range(n):
print(i, end=" ")
print()
for i in range(n):
print(i + 1, end=" ")
print()
for i in range(n):
print(i + i + 1, end=" ")
else:
print(-1)
``` | instruction | 0 | 80,280 | 12 | 160,560 |
No | output | 1 | 80,280 | 12 | 160,561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that the memory limit in this problem is less than usual.
Let's consider an array consisting of positive integers, some positions of which contain gaps.
We have a collection of numbers that can be used to fill the gaps. Each number from the given collection can be used at most once.
Your task is to determine such way of filling gaps that the longest increasing subsequence in the formed array has a maximum size.
Input
The first line contains a single integer n — the length of the array (1 ≤ n ≤ 105).
The second line contains n space-separated integers — the elements of the sequence. A gap is marked as "-1". The elements that are not gaps are positive integers not exceeding 109. It is guaranteed that the sequence contains 0 ≤ k ≤ 1000 gaps.
The third line contains a single positive integer m — the number of elements to fill the gaps (k ≤ m ≤ 105).
The fourth line contains m positive integers — the numbers to fill gaps. Each number is a positive integer not exceeding 109. Some numbers may be equal.
Output
Print n space-separated numbers in a single line — the resulting sequence. If there are multiple possible answers, print any of them.
Examples
Input
3
1 2 3
1
10
Output
1 2 3
Input
3
1 -1 3
3
1 2 3
Output
1 2 3
Input
2
-1 2
2
2 4
Output
2 2
Input
3
-1 -1 -1
5
1 1 1 1 2
Output
1 1 2
Input
4
-1 -1 -1 2
4
1 1 2 2
Output
1 2 1 2
Note
In the first sample there are no gaps, so the correct answer is the initial sequence.
In the second sample there is only one way to get an increasing subsequence of length 3.
In the third sample answer "4 2" would also be correct. Note that only strictly increasing subsequences are considered.
In the fifth sample the answer "1 1 1 2" is not considered correct, as number 1 can be used in replacing only two times.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
back=a.copy()
m=int(input())
b=list(map(int,input().split()))
b.sort()
max_l=0
max_pos=0
l=0
pos=0
import collections
left=collections.deque(b.copy())
setuped=True
for i in range(0,n):
if a[i]==-1:
if i!=0:
j=0
setuped=True
while (len(left)!=0)and(a[i-1]>=left[j]):
left.popleft()
if len(left)<=j:
if l>max_l:
max_l=l
max_pos=pos
l=0
pos=i
left=collections.deque(b.copy())
setuped=False
if (setuped):
a[i]=left.popleft()
l+=1
else:
a[i]=left.popleft()
else:
a[i]=left.popleft()
elif (i!=0):
if (a[i]>a[i-1]):
l+=1
else:
if l>max_l:
max_l=l
max_pos=pos
l=0
pos=i
left=collections.deque(b.copy())
if l>max_l:
max_l=l
max_pos=pos
#print(max_l,max_pos)
#print (a)
j=0
left=collections.deque(b.copy())
b=left.copy()
a1=back[max_pos:max_pos+max_l+1].copy()
#print (a1)
for i in range(len(a1)):
if a1[i]==-1:
if i!=0:
setuped=True
while (len(left)!=0)and(a1[i-1]>=left[j]):
left.popleft()
if len(left)<=j:
if l>max_l:
max_l=l
max_pos=pos
l=0
pos=i
left=collections.deque(b.copy())
setuped=False
if setuped==False:
a1[i]=b.popleft()
#print('from b')
elif setuped:
#print('left',left)
a1[i]=left.popleft()
#print('aI',a1[i])
else:
a1[i]=left.popleft()
a[max_pos:max_pos+max_l+1]=a1
s=''
for i in a:
s+=str(i)+' '
print(s[:-1])
``` | instruction | 0 | 80,420 | 12 | 160,840 |
No | output | 1 | 80,420 | 12 | 160,841 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that the memory limit in this problem is less than usual.
Let's consider an array consisting of positive integers, some positions of which contain gaps.
We have a collection of numbers that can be used to fill the gaps. Each number from the given collection can be used at most once.
Your task is to determine such way of filling gaps that the longest increasing subsequence in the formed array has a maximum size.
Input
The first line contains a single integer n — the length of the array (1 ≤ n ≤ 105).
The second line contains n space-separated integers — the elements of the sequence. A gap is marked as "-1". The elements that are not gaps are positive integers not exceeding 109. It is guaranteed that the sequence contains 0 ≤ k ≤ 1000 gaps.
The third line contains a single positive integer m — the number of elements to fill the gaps (k ≤ m ≤ 105).
The fourth line contains m positive integers — the numbers to fill gaps. Each number is a positive integer not exceeding 109. Some numbers may be equal.
Output
Print n space-separated numbers in a single line — the resulting sequence. If there are multiple possible answers, print any of them.
Examples
Input
3
1 2 3
1
10
Output
1 2 3
Input
3
1 -1 3
3
1 2 3
Output
1 2 3
Input
2
-1 2
2
2 4
Output
2 2
Input
3
-1 -1 -1
5
1 1 1 1 2
Output
1 1 2
Input
4
-1 -1 -1 2
4
1 1 2 2
Output
1 2 1 2
Note
In the first sample there are no gaps, so the correct answer is the initial sequence.
In the second sample there is only one way to get an increasing subsequence of length 3.
In the third sample answer "4 2" would also be correct. Note that only strictly increasing subsequences are considered.
In the fifth sample the answer "1 1 1 2" is not considered correct, as number 1 can be used in replacing only two times.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
back=a.copy()
m=int(input())
b=list(map(int,input().split()))
b.sort()
max_l=0
max_pos=0
l=0
pos=0
import collections
left=collections.deque(b.copy())
setuped=True
for i in range(0,n):
if a[i]==-1:
if i!=0:
j=0
setuped=True
while (len(left)!=0)and(a[i-1]>=left[j]):
left.popleft()
if len(left)<=j:
if l>max_l:
max_l=l
max_pos=pos
l=0
pos=i
left=collections.deque(b.copy())
setuped=False
if (setuped):
a[i]=left.popleft()
l+=1
else:
a[i]=left.popleft()
else:
a[i]=left.popleft()
elif (i!=0):
if (a[i]>a[i-1]):
l+=1
else:
if l>max_l:
max_l=l
max_pos=pos
l=0
pos=i
left=collections.deque(b.copy())
if l>max_l:
max_l=l
max_pos=pos
#print(max_l,max_pos)
#print (a)
j=0
left=collections.deque(b.copy())
b=left.copy()
a1=back[max_pos:max_pos+max_l+1].copy()
#print (a1)
for i in range(len(a1)):
if a1[i]==-1:
if i!=0:
setuped=True
while (len(left)!=0)and(a1[i-1]>=left[j]):
left.popleft()
if len(left)<=j:
if l>max_l:
max_l=l
max_pos=pos
l=0
pos=i
left=collections.deque(b.copy())
setuped=False
if setuped==False:
a1[i]=b.popleft()
#print('from b')
elif setuped:
#print('left',left)
a1[i]=left.popleft()
#print('aI',a1[i])
else:
a1[i]=left.popleft()
a[max_pos:max_pos+max_l+1]=a1
print(a)
``` | instruction | 0 | 80,421 | 12 | 160,842 |
No | output | 1 | 80,421 | 12 | 160,843 |
Provide tags and a correct Python 3 solution for this coding contest problem.
a is an array of n positive integers, all of which are not greater than n.
You have to process q queries to this array. Each query is represented by two numbers p and k. Several operations are performed in each query; each operation changes p to p + ap + k. There operations are applied until p becomes greater than n. The answer to the query is the number of performed operations.
Input
The first line contains one integer n (1 ≤ n ≤ 100000).
The second line contains n integers — elements of a (1 ≤ ai ≤ n for each i from 1 to n).
The third line containts one integer q (1 ≤ q ≤ 100000).
Then q lines follow. Each line contains the values of p and k for corresponding query (1 ≤ p, k ≤ n).
Output
Print q integers, ith integer must be equal to the answer to ith query.
Example
Input
3
1 1 1
3
1 1
2 1
3 1
Output
2
1
1
Note
Consider first example:
In first query after first operation p = 3, after second operation p = 5.
In next two queries p is greater than n after the first operation. | instruction | 0 | 80,504 | 12 | 161,008 |
Tags: brute force, data structures, dp
Correct Solution:
```
import sys
n = int(sys.stdin.buffer.readline().decode('utf-8'))
a = [0] + list(map(int, sys.stdin.buffer.readline().decode('utf-8').split()))
q = int(sys.stdin.buffer.readline().decode('utf-8'))
dp_size = 200
dp = [[1]*(n+1) for _ in range(dp_size+1)]
for k in range(1, dp_size+1):
for i in range(n, 0, -1):
if i+a[i]+k <= n:
dp[k][i] = dp[k][i+a[i]+k]+1
ans = [0]*q
for i, (p, k) in enumerate(map(int, line.decode('utf-8').split()) for line in sys.stdin.buffer):
if k <= dp_size:
ans[i] = dp[k][p]
else:
while p <= n:
p += a[p] + k
ans[i] += 1
sys.stdout.buffer.write('\n'.join(map(str, ans)).encode('utf-8'))
``` | output | 1 | 80,504 | 12 | 161,009 |
Provide tags and a correct Python 3 solution for this coding contest problem.
a is an array of n positive integers, all of which are not greater than n.
You have to process q queries to this array. Each query is represented by two numbers p and k. Several operations are performed in each query; each operation changes p to p + ap + k. There operations are applied until p becomes greater than n. The answer to the query is the number of performed operations.
Input
The first line contains one integer n (1 ≤ n ≤ 100000).
The second line contains n integers — elements of a (1 ≤ ai ≤ n for each i from 1 to n).
The third line containts one integer q (1 ≤ q ≤ 100000).
Then q lines follow. Each line contains the values of p and k for corresponding query (1 ≤ p, k ≤ n).
Output
Print q integers, ith integer must be equal to the answer to ith query.
Example
Input
3
1 1 1
3
1 1
2 1
3 1
Output
2
1
1
Note
Consider first example:
In first query after first operation p = 3, after second operation p = 5.
In next two queries p is greater than n after the first operation. | instruction | 0 | 80,505 | 12 | 161,010 |
Tags: brute force, data structures, dp
Correct Solution:
```
from sys import stdin, stdout, setrecursionlimit
input = stdin.readline
# import string
# characters = string.ascii_lowercase
# digits = string.digits
# setrecursionlimit(int(1e5))
# dir = [-1,0,1,0,-1]
# moves = 'NESW'
inf = float('inf')
from functools import cmp_to_key
from collections import defaultdict as dd
from collections import Counter, deque
from heapq import *
import math
from math import floor, ceil, sqrt
def geti(): return map(int, input().strip().split())
def getl(): return list(map(int, input().strip().split()))
def getis(): return map(str, input().strip().split())
def getls(): return list(map(str, input().strip().split()))
def gets(): return input().strip()
def geta(): return int(input())
def print_s(s): stdout.write(s+'\n')
def solve():
n = geta()
a = getl()
limit = floor(sqrt(n)) + 1
dp = [[0 for i in range(limit)] for _ in range(n+1)]
for p in range(n-1, -1, -1):
for k in range(1, limit):
new = a[p] + k + p + 1
if new > n:
dp[p+1][k] = 1
break
else:
dp[p+1][k] = max(1, dp[new][k]) + 1
for i in range(geta()):
p, k = geti()
if k < limit:
print(max(1, dp[p][k]))
else:
ans = 0
while p <= n:
p = a[p-1] + p + k
ans += 1
print(ans)
if __name__=='__main__':
solve()
``` | output | 1 | 80,505 | 12 | 161,011 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.
More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti ≠ - 1, then valuenexti > valuei.
You are given the number of elements in the list n, the index of the first element start, and the integer x.
You can make up to 2000 queries of the following two types:
* ? i (1 ≤ i ≤ n) — ask the values valuei and nexti,
* ! ans — give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem.
Input
The first line contains three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109) — the number of elements in the list, the index of the first element and the integer x.
Output
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.
Interaction
To make a query of the first type, print ? i (1 ≤ i ≤ n), where i is the index of element you want to know information about.
After each query of type ? read two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
It is guaranteed that if nexti ≠ - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element.
Note that you can't ask more than 1999 queries of the type ?.
If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.
Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer.
To flush you can use (just after printing a query and line end):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* For other languages see documentation.
Hacks format
For hacks, use the following format:
In the first line print three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109).
In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend.
Example
Input
5 3 80
97 -1
58 5
16 2
81 1
79 4
Output
? 1
? 2
? 3
? 4
? 5
! 81
Note
You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list>
The illustration for the first sample case. Start and finish elements are marked dark. <image> | instruction | 0 | 80,526 | 12 | 161,052 |
Tags: brute force, interactive, probabilities
Correct Solution:
```
import sys
import random
n, start, x = map(int, input().split())
visited = [False] * (n + 2)
left = 2000
print("? " + str(start))
left -= 1
sys.stdout.flush()
data = []
val, nex = map(int, input().split())
data.append((val, nex))
visited[start] = True
if(val >= x):
print("! " + str(val))
sys.stdout.flush()
sys.exit()
safe = 10**20
for i in range(1000):
cell = random.randint(1, n)
if(visited[cell]):
continue
print("? " + str(cell))
left -= 1
visited[cell] = True
sys.stdout.flush()
val, nex = map(int, input().split())
if(val > x):
safe = min(safe, val)
continue
if(val == x):
print("! " + str(x))
sys.stdout.flush()
sys.exit()
if(val < x):
data.append((val, nex))
if(len(data) == 0):
print("! " + str(safe))
sys.stdout.flush()
sys.exit()
data.sort()
currentval = data[-1][0]
currentnext = data[-1][1]
while True:
if(currentnext == -1):
print("! -1")
sys.stdout.flush()
sys.exit()
if(left == 1):
print("! -1")
sys.stdout.flush()
sys.exit()
print("? " + str(currentnext))
left -= 1
sys.stdout.flush()
currentval, currentnext = map(int, input().split())
if(currentval == -1 and currentnext == -1):
print("! -1")
sys.stdout.flush()
sys.exit()
if(currentval >= x):
print("! " + str(currentval))
sys.stdout.flush()
sys.exit()
``` | output | 1 | 80,526 | 12 | 161,053 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.
More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti ≠ - 1, then valuenexti > valuei.
You are given the number of elements in the list n, the index of the first element start, and the integer x.
You can make up to 2000 queries of the following two types:
* ? i (1 ≤ i ≤ n) — ask the values valuei and nexti,
* ! ans — give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem.
Input
The first line contains three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109) — the number of elements in the list, the index of the first element and the integer x.
Output
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.
Interaction
To make a query of the first type, print ? i (1 ≤ i ≤ n), where i is the index of element you want to know information about.
After each query of type ? read two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
It is guaranteed that if nexti ≠ - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element.
Note that you can't ask more than 1999 queries of the type ?.
If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.
Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer.
To flush you can use (just after printing a query and line end):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* For other languages see documentation.
Hacks format
For hacks, use the following format:
In the first line print three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109).
In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend.
Example
Input
5 3 80
97 -1
58 5
16 2
81 1
79 4
Output
? 1
? 2
? 3
? 4
? 5
! 81
Note
You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list>
The illustration for the first sample case. Start and finish elements are marked dark. <image> | instruction | 0 | 80,527 | 12 | 161,054 |
Tags: brute force, interactive, probabilities
Correct Solution:
```
from random import sample
SIZE = 950
def R():
return map(int, input().split())
def ask(i):
print('?', i, flush=True)
v, nxt = R()
if v < 0:
exit()
return v, nxt
def ans(v):
print('!', v)
exit()
n, s, x = R()
v, nxt = ask(s)
if v >= x:
ans(v)
q = range(1, n + 1)
if n > SIZE:
q = sample(q, SIZE)
try :
v, nxt = max(filter(lambda t: t[0] < x, map(ask, q)))
except ValueError:
pass
while v < x and nxt >= 1:
v, nxt = ask(nxt)
ans(v if v >= x else -1)
``` | output | 1 | 80,527 | 12 | 161,055 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.
More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti ≠ - 1, then valuenexti > valuei.
You are given the number of elements in the list n, the index of the first element start, and the integer x.
You can make up to 2000 queries of the following two types:
* ? i (1 ≤ i ≤ n) — ask the values valuei and nexti,
* ! ans — give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem.
Input
The first line contains three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109) — the number of elements in the list, the index of the first element and the integer x.
Output
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.
Interaction
To make a query of the first type, print ? i (1 ≤ i ≤ n), where i is the index of element you want to know information about.
After each query of type ? read two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
It is guaranteed that if nexti ≠ - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element.
Note that you can't ask more than 1999 queries of the type ?.
If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.
Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer.
To flush you can use (just after printing a query and line end):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* For other languages see documentation.
Hacks format
For hacks, use the following format:
In the first line print three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109).
In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend.
Example
Input
5 3 80
97 -1
58 5
16 2
81 1
79 4
Output
? 1
? 2
? 3
? 4
? 5
! 81
Note
You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list>
The illustration for the first sample case. Start and finish elements are marked dark. <image> | instruction | 0 | 80,528 | 12 | 161,056 |
Tags: brute force, interactive, probabilities
Correct Solution:
```
import random
import sys
random.seed(a=5000)
n,start,x=map(int,input().split())
visited=[0]*(n+1)
bestans=10**10
tmp=0
bestbefore=start
out=['?',str(start)]
print(' '.join(out),flush=True)
value,next=map(int,input().split())
bestbeforeval=value
if value>=x:
print(' '.join(['!',str(value)]))
sys.exit()
tmp=0
for i in range(1000):
if tmp==n:
break
while True:
k=random.randint(1, n)
if visited[k]==0:
break
out=['?',str(k)]
print(' '.join(out),flush=True)
value,nxt=map(int,input().split())
visited[k]=1
tmp+=1
if visited[nxt]==0:
visited[nxt]=1
tmp+=1
if value>=x:
bestans=min(value,bestans)
else:
if bestbeforeval<value:
bestbeforeval=value
bestbefore=k
curr=bestbefore
while True:
out=['?',str(curr)]
print(' '.join(out),flush=True)
value,nxt=map(int,input().split())
if nxt==-1:
if value==-1:
abdacadabra
sys.exit()
else:
if value>=x:
out=['!',str(value)]
print(' '.join(out))
sys.exit()
else:
print('! -1')
sys.exit()
if value>=x:
out=['!',str(value)]
print(' '.join(out))
sys.exit()
curr=nxt
``` | output | 1 | 80,528 | 12 | 161,057 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.
More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti ≠ - 1, then valuenexti > valuei.
You are given the number of elements in the list n, the index of the first element start, and the integer x.
You can make up to 2000 queries of the following two types:
* ? i (1 ≤ i ≤ n) — ask the values valuei and nexti,
* ! ans — give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem.
Input
The first line contains three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109) — the number of elements in the list, the index of the first element and the integer x.
Output
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.
Interaction
To make a query of the first type, print ? i (1 ≤ i ≤ n), where i is the index of element you want to know information about.
After each query of type ? read two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
It is guaranteed that if nexti ≠ - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element.
Note that you can't ask more than 1999 queries of the type ?.
If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.
Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer.
To flush you can use (just after printing a query and line end):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* For other languages see documentation.
Hacks format
For hacks, use the following format:
In the first line print three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109).
In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend.
Example
Input
5 3 80
97 -1
58 5
16 2
81 1
79 4
Output
? 1
? 2
? 3
? 4
? 5
! 81
Note
You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list>
The illustration for the first sample case. Start and finish elements are marked dark. <image> | instruction | 0 | 80,529 | 12 | 161,058 |
Tags: brute force, interactive, probabilities
Correct Solution:
```
from sys import stdout
import random
n, start, x = map(int, input().split())
not_vis = set(range(1, n+1))
print('? ' + str(start))
stdout.flush()
val, nxt = map(int, input().split())
vis = set([start])
while 1:
if nxt == -1 and val == -1:
break
if val >= x:
print('! ' + str(val))
stdout.flush()
break
if nxt == -1:
print('! -1')
break
print('? ' + str(nxt))
stdout.flush()
v1, n1 = map(int, input().split())
vis.add(nxt)
if len(not_vis) > 0:
rnd = random.sample(not_vis, 1)[0]
not_vis.remove(rnd)
else:
rnd = 1
print('? ' + str(rnd))
stdout.flush()
v2, n2 = map(int, input().split())
vis.add(rnd)
if v2 > v1 and v2 <= x:
val = v2
nxt = n2
else:
val = v1
nxt = n1
``` | output | 1 | 80,529 | 12 | 161,059 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.
More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti ≠ - 1, then valuenexti > valuei.
You are given the number of elements in the list n, the index of the first element start, and the integer x.
You can make up to 2000 queries of the following two types:
* ? i (1 ≤ i ≤ n) — ask the values valuei and nexti,
* ! ans — give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem.
Input
The first line contains three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109) — the number of elements in the list, the index of the first element and the integer x.
Output
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.
Interaction
To make a query of the first type, print ? i (1 ≤ i ≤ n), where i is the index of element you want to know information about.
After each query of type ? read two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
It is guaranteed that if nexti ≠ - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element.
Note that you can't ask more than 1999 queries of the type ?.
If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.
Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer.
To flush you can use (just after printing a query and line end):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* For other languages see documentation.
Hacks format
For hacks, use the following format:
In the first line print three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109).
In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend.
Example
Input
5 3 80
97 -1
58 5
16 2
81 1
79 4
Output
? 1
? 2
? 3
? 4
? 5
! 81
Note
You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list>
The illustration for the first sample case. Start and finish elements are marked dark. <image> | instruction | 0 | 80,530 | 12 | 161,060 |
Tags: brute force, interactive, probabilities
Correct Solution:
```
from random import sample
def R():
return map(int, input().split())
def ask(i):
print('?', i, flush=True)
v, nxt = R()
if v < 0:
exit()
return v, nxt
def ans(v):
print('!', v)
exit()
n, s, x = R()
d = [None] * (n + 1)
f = [False] * (n + 1)
mv = -1
i = s
count = 950
q = range(1, n + 1)
if n > count:
q = sample(q, count)
q = [s] + [x for x in q if x != s]
for i in q:
if not f[i]:
f[i] = True
v, nxt = ask(i)
if v == x or i == s and v > x:
ans(v)
if nxt != -1:
f[nxt] = True
elif v < x:
ans(-1)
if v < x:
nv = d[nxt]
if nv is None:
if v > mv:
mv, mnxt = v, nxt
elif nv > x:
ans(nv)
d[i] = v
while mv < x and mnxt >= 1:
mv, mnxt = ask(mnxt)
ans(mv if mv >= x else -1)
``` | output | 1 | 80,530 | 12 | 161,061 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.
More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti ≠ - 1, then valuenexti > valuei.
You are given the number of elements in the list n, the index of the first element start, and the integer x.
You can make up to 2000 queries of the following two types:
* ? i (1 ≤ i ≤ n) — ask the values valuei and nexti,
* ! ans — give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem.
Input
The first line contains three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109) — the number of elements in the list, the index of the first element and the integer x.
Output
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.
Interaction
To make a query of the first type, print ? i (1 ≤ i ≤ n), where i is the index of element you want to know information about.
After each query of type ? read two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
It is guaranteed that if nexti ≠ - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element.
Note that you can't ask more than 1999 queries of the type ?.
If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.
Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer.
To flush you can use (just after printing a query and line end):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* For other languages see documentation.
Hacks format
For hacks, use the following format:
In the first line print three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109).
In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend.
Example
Input
5 3 80
97 -1
58 5
16 2
81 1
79 4
Output
? 1
? 2
? 3
? 4
? 5
! 81
Note
You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list>
The illustration for the first sample case. Start and finish elements are marked dark. <image> | instruction | 0 | 80,531 | 12 | 161,062 |
Tags: brute force, interactive, probabilities
Correct Solution:
```
import sys
n, start, x = map(int, input().split())
C = min(n, 998)
def get(i):
sys.stdout.write("? %d\n" % i)
sys.stdout.flush()
return list(map(int, sys.stdin.readline().split()))
def answer(a):
print("! %d" % a)
exit(0)
m = get(start)[0]
if m >= x:
answer(m)
from random import sample
queries = [get(i) for i in sample(range(1, n+1), C)] + [get(start)]
u = max([q for q in queries if q[0] < x])[1]
while u > 0:
q = get(u)
if q[0] >= x: answer(q[0])
u = q[1]
answer(-1)
``` | output | 1 | 80,531 | 12 | 161,063 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.
More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti ≠ - 1, then valuenexti > valuei.
You are given the number of elements in the list n, the index of the first element start, and the integer x.
You can make up to 2000 queries of the following two types:
* ? i (1 ≤ i ≤ n) — ask the values valuei and nexti,
* ! ans — give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem.
Input
The first line contains three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109) — the number of elements in the list, the index of the first element and the integer x.
Output
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.
Interaction
To make a query of the first type, print ? i (1 ≤ i ≤ n), where i is the index of element you want to know information about.
After each query of type ? read two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
It is guaranteed that if nexti ≠ - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element.
Note that you can't ask more than 1999 queries of the type ?.
If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.
Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer.
To flush you can use (just after printing a query and line end):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* For other languages see documentation.
Hacks format
For hacks, use the following format:
In the first line print three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109).
In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend.
Example
Input
5 3 80
97 -1
58 5
16 2
81 1
79 4
Output
? 1
? 2
? 3
? 4
? 5
! 81
Note
You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list>
The illustration for the first sample case. Start and finish elements are marked dark. <image> | instruction | 0 | 80,532 | 12 | 161,064 |
Tags: brute force, interactive, probabilities
Correct Solution:
```
from random import sample
from sys import stdout
read=lambda :map(int,input().split())
def ask(x):
print('? {}'.format(x))
stdout.flush()
return tuple(read())
def answer(x):
print('! {}'.format(x))
stdout.flush()
exit(0)
n,st,x=read()
a=[ask(st)]
if a[0][0]>=x: answer(a[0][0])
for i in sample(range(1,n+1),min(n,999)):
b=ask(i)
if b[0]<x: a.append(b)
elif b[0]==x: answer(x)
b=max(a)
while True:
if b[0]>=x: answer(b[0])
if b[1]==-1: break
else: b=ask(b[1])
answer(-1)
``` | output | 1 | 80,532 | 12 | 161,065 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.
More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti ≠ - 1, then valuenexti > valuei.
You are given the number of elements in the list n, the index of the first element start, and the integer x.
You can make up to 2000 queries of the following two types:
* ? i (1 ≤ i ≤ n) — ask the values valuei and nexti,
* ! ans — give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem.
Input
The first line contains three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109) — the number of elements in the list, the index of the first element and the integer x.
Output
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.
Interaction
To make a query of the first type, print ? i (1 ≤ i ≤ n), where i is the index of element you want to know information about.
After each query of type ? read two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
It is guaranteed that if nexti ≠ - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element.
Note that you can't ask more than 1999 queries of the type ?.
If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.
Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer.
To flush you can use (just after printing a query and line end):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* For other languages see documentation.
Hacks format
For hacks, use the following format:
In the first line print three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109).
In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend.
Example
Input
5 3 80
97 -1
58 5
16 2
81 1
79 4
Output
? 1
? 2
? 3
? 4
? 5
! 81
Note
You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list>
The illustration for the first sample case. Start and finish elements are marked dark. <image> | instruction | 0 | 80,533 | 12 | 161,066 |
Tags: brute force, interactive, probabilities
Correct Solution:
```
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
"""
created by shhuan at 2017/10/20 11:12
"""
N, X, S = map(int, input().split())
print("?", X)
V, ni = map(int, input().split())
q = {random.randint(1, N) for _ in range(min(N, 999))}
if N > 1000:
while len(q) < 999:
q.add(random.randint(1, N))
for i in q:
print('?', i)
sys.stdout.flush()
a, b = map(int, input().split())
if V < a <= S:
V = a
ni = b
while V < S and ni > 0:
print("?", ni)
sys.stdout.flush()
a, b = map(int, input().split())
V = a
ni = b
if V >= S:
print("!", V)
else:
print("!", -1)
``` | output | 1 | 80,533 | 12 | 161,067 |
Provide a correct Python 3 solution for this coding contest problem.
We will define the median of a sequence b of length M, as follows:
* Let b' be the sequence obtained by sorting b in non-decreasing order. Then, the value of the (M / 2 + 1)-th element of b' is the median of b. Here, / is integer division, rounding down.
For example, the median of (10, 30, 20) is 20; the median of (10, 30, 20, 40) is 30; the median of (10, 10, 10, 20, 30) is 10.
Snuke comes up with the following problem.
You are given a sequence a of length N. For each pair (l, r) (1 \leq l \leq r \leq N), let m_{l, r} be the median of the contiguous subsequence (a_l, a_{l + 1}, ..., a_r) of a. We will list m_{l, r} for all pairs (l, r) to create a new sequence m. Find the median of m.
Constraints
* 1 \leq N \leq 10^5
* a_i is an integer.
* 1 \leq a_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the median of m.
Examples
Input
3
10 30 20
Output
30
Input
1
10
Output
10
Input
10
5 9 5 9 8 9 3 5 4 3
Output
8 | instruction | 0 | 80,670 | 12 | 161,340 |
"Correct Solution:
```
def cumsum(li):
ret = []
sum = 0
for i in li:
sum += i
ret.append(sum)
return ret
class BinaryIndexedTree:
# http://hos.ac/slides/20140319_bit.pdf
def __init__(self, size):
"""
:param int size:
"""
self._bit = [0 for _ in range(size)]
self._size = size
def add(self, i, w):
"""
i 番目に w を加える
:param int i:
:param int w:
:return:
"""
x = i + 1
while x <= self._size:
self._bit[x - 1] += w
x += x & -x
def sum(self, i):
"""
0 番目から i 番目までの合計
:param int i:
:return:
"""
ret = 0
x = i + 1
while x > 0:
ret += self._bit[x - 1]
x -= x & -x
return ret
def __len__(self):
return self._size
def count_inversions(li, max=None):
"""
リストから転倒数 (li[i] > li[j] (i < j) となる (i, j) の組み合わせ数) を返す
バブルソートするときに反転する必要がある数。
:param numpy.ndarray | list of int li:
すべての要素が 0 以上の int である配列。
BIT を使うので、マイナスを含んだり最大値が大きい場合は np.argsort の結果を指定
ただしリストに重複を含む場合は np.argsort は unstable なので別の方法使うこと
https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html
:param int max: li の最大値。わかる場合は指定
:rtype: int
"""
if not max:
max = __builtins__.max(li)
bit = BinaryIndexedTree(size=max + 1)
ret = 0
for i in range(len(li)):
ret += i - bit.sum(li[i])
bit.add(li[i], 1)
return ret
def bisect_left_callable(fn, x, lo, hi):
"""
lo から hi-1 のうち、fn の結果が x 以上となる、最も左の値
bisect.bisect_left と同じ
https://docs.python.org/ja/3/library/bisect.html
:param callable fn:
:param x:
:param int lo: 最小値
:param int hi: 最大値 + 1
:return: lo <= ret <= hi
"""
while lo < hi:
mid = (lo + hi) // 2
if fn(mid) < x:
lo = mid + 1
else:
hi = mid
return lo
N = int(input())
series = list(map(int, input().split()))
series_sorted = sorted(series)
# L と r の選び方の組み合わせ数。1 から N までの和に等しい
lr_size = (N + 1) * N / 2
# cumsum[L] <= cumsum[r] となる (L, r) の組み合わせ数を数える
# 転倒数を数えて lr_size から引けばいい
# それが (lr_size + 1) // 2 以上である一番大きい x を探す
def solve(i):
x = series_sorted[i]
# series を x 以上かどうかで 1 と 0 に置換し累積和を取る
cumsum_li = [0] + cumsum([1 if s >= x else -1 for s in series])
# 転倒数 (cumsum にマイナスを含むので最小でも 0 になるよう N を加算)
inversions = count_inversions([N + i for i in cumsum_li], max=N + N)
if lr_size - inversions >= (lr_size + 1) // 2:
return i
else:
return float('inf')
ans_i = bisect_left_callable(solve, float('inf'), lo=0, hi=N)
print(series_sorted[max(0, min(N - 1, ans_i - 1))])
``` | output | 1 | 80,670 | 12 | 161,341 |
Provide a correct Python 3 solution for this coding contest problem.
We will define the median of a sequence b of length M, as follows:
* Let b' be the sequence obtained by sorting b in non-decreasing order. Then, the value of the (M / 2 + 1)-th element of b' is the median of b. Here, / is integer division, rounding down.
For example, the median of (10, 30, 20) is 20; the median of (10, 30, 20, 40) is 30; the median of (10, 10, 10, 20, 30) is 10.
Snuke comes up with the following problem.
You are given a sequence a of length N. For each pair (l, r) (1 \leq l \leq r \leq N), let m_{l, r} be the median of the contiguous subsequence (a_l, a_{l + 1}, ..., a_r) of a. We will list m_{l, r} for all pairs (l, r) to create a new sequence m. Find the median of m.
Constraints
* 1 \leq N \leq 10^5
* a_i is an integer.
* 1 \leq a_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the median of m.
Examples
Input
3
10 30 20
Output
30
Input
1
10
Output
10
Input
10
5 9 5 9 8 9 3 5 4 3
Output
8 | instruction | 0 | 80,671 | 12 | 161,342 |
"Correct Solution:
```
import math
class Bit:
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i <= self.size:
self.tree[i] += x
i += i & -i
get_ints = lambda: [int(st) for st in input().split()]
N=int(input())
inarr=get_ints()
sorted_in = sorted(inarr)
# print(sorted_in)
comb = N*(N+1) // 2
def binsearch(lft, rgt):
half = (lft + rgt) // 2
# print("investigate: lft {} rgt {} half {} value {}".format(lft, rgt, half, sorted_in[half]))
if lft >= half:
return half
summed = 0
res = 0
bit = Bit(2*N+1)
bit.add(N, 1)
for i, x in enumerate(inarr):
value = 1 if x >= sorted_in[half] else -1
summed += value
# print("summed: {}, bit: {}".format(summed, bit.sum(N + summed)))
count = bit.sum(N + summed)
bit.add(N + summed, 1)
res += count
# print("num {} cnt {} judge {}".format(sorted_in[half], res, math.ceil(comb/2)))
if res >= math.ceil(comb/2):
return binsearch(half, rgt)
else:
return binsearch(lft, half)
# print(sorted_in)
res = binsearch(0, N)
# print(res, sorted_in[res])
print(sorted_in[res])
``` | output | 1 | 80,671 | 12 | 161,343 |
Provide a correct Python 3 solution for this coding contest problem.
We will define the median of a sequence b of length M, as follows:
* Let b' be the sequence obtained by sorting b in non-decreasing order. Then, the value of the (M / 2 + 1)-th element of b' is the median of b. Here, / is integer division, rounding down.
For example, the median of (10, 30, 20) is 20; the median of (10, 30, 20, 40) is 30; the median of (10, 10, 10, 20, 30) is 10.
Snuke comes up with the following problem.
You are given a sequence a of length N. For each pair (l, r) (1 \leq l \leq r \leq N), let m_{l, r} be the median of the contiguous subsequence (a_l, a_{l + 1}, ..., a_r) of a. We will list m_{l, r} for all pairs (l, r) to create a new sequence m. Find the median of m.
Constraints
* 1 \leq N \leq 10^5
* a_i is an integer.
* 1 \leq a_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the median of m.
Examples
Input
3
10 30 20
Output
30
Input
1
10
Output
10
Input
10
5 9 5 9 8 9 3 5 4 3
Output
8 | instruction | 0 | 80,672 | 12 | 161,344 |
"Correct Solution:
```
n = int(input())
A = list(map(int, input().split()))
def search(x):
b = n
r = 0
y = 0
D = [0]*(2*n+1)
for i in range(n):
D[b] += 1
if A[i] < x:
r += D[b]
b += 1
else:
b -= 1
r -= D[b]
y += r
return y
S = sorted(A)
l = 0
r = n
m = n // 2
c = n * (n + 1) // 2
while True:
if search(S[m]) <= c // 2:
if m == n - 1:
break
elif search(S[m + 1]) > c // 2:
break
else:
l = m
m = (m + r) // 2
else:
r = m + 1
m = (m + l) // 2
print(S[m])
``` | output | 1 | 80,672 | 12 | 161,345 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.