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.
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.
Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:
<image>
You have to write a program that allows you to determine what number will be in the cell with index x (1 β€ x β€ n) after Dima's algorithm finishes.
Input
The first line contains two integers n and q (1 β€ n β€ 1018, 1 β€ q β€ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.
Next q lines contain integers xi (1 β€ xi β€ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.
Output
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
Examples
Input
4 3
2
3
4
Output
3
2
4
Input
13 4
10
5
4
8
Output
13
3
8
9
Note
The first example is shown in the picture.
In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
Submitted Solution:
```
s = input().split()
n = int(s[0])
q = int(s[1])
cl = []
st = 1
pos = 1
def rev(a, k, lena):
x = a[lena-1]
for i in range(lena-k, lena):
y = a[i]
a[i] = x
x = y
for i in range(n*2-1):
if st==1:
cl.append(pos)
pos+=1
st = 2
else:
cl.append(0)
st = 1
t = []
d = []
k = n//2
for i in range(k):
d.append(i)
t.append(n-k+1+i)
for i in range(1, n//2+1):
rev(d, i, k)
nd = []
r = n-1-n//2
for i in range(k):
nd.append(d[(i+r)%k])
for i in range(k):
cl[2*i+1]=t[nd[i]]
for i in range(q):
print(cl[int(input())-1])
``` | instruction | 0 | 27,110 | 12 | 54,220 |
No | output | 1 | 27,110 | 12 | 54,221 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.
Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:
<image>
You have to write a program that allows you to determine what number will be in the cell with index x (1 β€ x β€ n) after Dima's algorithm finishes.
Input
The first line contains two integers n and q (1 β€ n β€ 1018, 1 β€ q β€ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.
Next q lines contain integers xi (1 β€ xi β€ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.
Output
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
Examples
Input
4 3
2
3
4
Output
3
2
4
Input
13 4
10
5
4
8
Output
13
3
8
9
Note
The first example is shown in the picture.
In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
Submitted Solution:
```
def step(n):
fix = n[-1]-1
length = len(n) -1
point = length -1
for i in range(fix):
n[length], n[point] = n[point], n[length]
length -=1
point -= 2
return n[:fix+1]
``` | instruction | 0 | 27,111 | 12 | 54,222 |
No | output | 1 | 27,111 | 12 | 54,223 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6]. | instruction | 0 | 27,477 | 12 | 54,954 |
Tags: constructive algorithms, sortings
Correct Solution:
```
n = int(input())
p = [i for i in map(int, input().split())]
actions = []
def swap(i, j):
p[i-1], p[j-1] = p[j-1], p[i-1]
actions.append(f"{i} {j}")
# print(i, j)
# print_(p)
def print_(p):
print("\t".join(map(str, p)))
i = 0
while i<n:
# print("current focus", i)
ind = i+1
v = p[i]
distance = abs(ind - v)
if ind == v:
i += 1
elif 2*distance >= n:
swap(ind, v)
elif ind <= n/2 and v <= n/2:
pivot = n
swap(ind, pivot)
swap(v, pivot)
swap(ind, pivot)
elif ind > n/2 and v >= n/2:
pivot = 1
swap(ind, pivot)
swap(v, pivot)
swap(ind, pivot)
else:
left = min(ind, v)
right = max(ind, v)
l_piv = 1
r_piv = n
swap(left, r_piv)
swap(right, l_piv)
swap(r_piv, l_piv)
swap(left, r_piv)
swap(right, l_piv)
print(len(actions))
print("\n".join(actions))
``` | output | 1 | 27,477 | 12 | 54,955 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6]. | instruction | 0 | 27,478 | 12 | 54,956 |
Tags: constructive algorithms, sortings
Correct Solution:
```
n = int(input())
p = [*map(int, input().split())]
p = [i - 1 for i in p]
#print(p)
pos = {}
ans = []
for i, j in enumerate(p):
pos[j] = i
def swap(i, j):
ans.append((i + 1, j + 1))
pos[p[i]], pos[p[j]] = pos[p[j]], pos[p[i]]
p[i], p[j] = p[j], p[i]
def do(i):
j=pos[i]
if j<n//2:
u=n-1
v=0
else:
u=0
v=n-1
swap(j, u)
if abs(u - i) >= n//2:
swap(u, i)
else:
swap(u, v)
swap(v, i)
for i in range(1,n-1):
if p[i] != i:
do(i)
if p[0]!=0:
swap(0, n - 1)
print(len(ans))
print('\n'.join([' '.join(map(str, [*i])) for i in ans]))
######
``` | output | 1 | 27,478 | 12 | 54,957 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6]. | instruction | 0 | 27,479 | 12 | 54,958 |
Tags: constructive algorithms, sortings
Correct Solution:
```
n=int(input())
a,b=[],[0]*n
for i,e in enumerate(input().split()):
a.append(int(e))
b[int(e)-1]=i
qu,ans=[],0
for i in range(n):
if a[i]==i+1:continue
j=b[i]
if j-i>=n//2:
qu.append((i+1,j+1))
ans+=1
elif j-i<n//2 and j+1<=n//2:
qu.append((j+1,n))
qu.append((i + 1, n))
qu.append((j + 1, n))
ans+=3
elif j-i<n//2 and i+1>n//2:
qu.append((j + 1, 1))
qu.append((i + 1, 1))
qu.append((j + 1, 1))
ans += 3
else:
qu.append((i + 1, n))
qu.append((1, n))
qu.append(( 1, j+1))
qu.append((1,n))
qu.append((i + 1, n))
ans += 5
b[i],b[a[i]-1]=b[a[i]-1],b[i]
a[i],a[j]=a[j],a[i]
print(ans)
for i in qu:
print(*i)
``` | output | 1 | 27,479 | 12 | 54,959 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6]. | instruction | 0 | 27,480 | 12 | 54,960 |
Tags: constructive algorithms, sortings
Correct Solution:
```
input()
p=[int(x)-1 for x in input().split()]
pos=[0] * len(p)
for i, x in enumerate(p):
pos[x]=i
# print(pos)
ans = []
for i, x in enumerate(p):
# print('i, x', i, x)
if i == x:
continue
# find x
i_pos = pos[i]
# print('i, i_pos', i, i_pos)
if 2 * abs(i_pos - i) < len(p):
if 2 * i_pos >= len(p) and 2 * i >= len(p):
# swap with first
# print('swap with first')
ans.append((0, i_pos))
ans.append((0, i))
ans.append((0, i_pos))
elif 2 * ((len(p) - 1) - i_pos) >= len(p) and 2 * ((len(p) - 1) - i) >= len(p):
# swap with last
# print('swap with last')
ans.append((len(p) - 1, i_pos))
ans.append((len(p) - 1, i))
ans.append((len(p) - 1, i_pos))
else:
# 5 swaps
# print('5 swaps')
fi = min(i_pos, i)
la = max(i_pos, i)
ans.append((fi, len(p) - 1))
ans.append((0, len(p) - 1))
ans.append((0, la))
ans.append((0, len(p) - 1))
ans.append((fi, len(p) - 1))
else:
ans.append((i, i_pos))
p[i_pos], p[i] = p[i], p[i_pos]
pos[p[i_pos]], pos[p[i]] = pos[p[i]], pos[p[i_pos]]
# print(ans)
print(len(ans))
for a, b in ans:
print(a+1,b+1)
assert sorted(p) == p
``` | output | 1 | 27,480 | 12 | 54,961 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6]. | instruction | 0 | 27,481 | 12 | 54,962 |
Tags: constructive algorithms, sortings
Correct Solution:
```
import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
# sys.setrecursionlimit(200000)
# mod = 10**9+7
# mod = 998244353
decimal.getcontext().prec = 46
def primeFactors(n):
prime = set()
while n % 2 == 0:
prime.add(2)
n = n//2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
prime.add(i)
n = n//i
if n > 2:
prime.add(n)
return list(prime)
def getFactors(n) :
factors = []
i = 1
while i <= math.sqrt(n):
if (n % i == 0) :
if (n // i == i) :
factors.append(i)
else :
factors.append(i)
factors.append(n//i)
i = i + 1
return factors
def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
num = []
for p in range(2, n+1):
if prime[p]:
num.append(p)
return num
def lcm(a,b):
return (a*b)//math.gcd(a,b)
def sort_dict(key_value):
return sorted(key_value.items(), key = lambda kv:(kv[1], kv[0]))
def list_input():
return list(map(int,input().split()))
def num_input():
return map(int,input().split())
def string_list():
return list(input())
def decimalToBinary(n):
return bin(n).replace("0b", "")
def binaryToDecimal(n):
return int(n,2)
def DFS(n,s,adj):
visited = [False for i in range(n+1)]
stack = []
stack.append(s)
while (len(stack)):
s = stack[-1]
stack.pop()
if (not visited[s]):
visited[s] = True
for node in adj[s]:
if (not visited[node]):
stack.append(node)
def condition_satisfied(ans,position,arr,pos,wrong_num,right_num,i):
ans.append((i+1,pos+1))
position[wrong_num] = pos
position[right_num] = i
arr[pos],arr[i] = wrong_num,right_num
return ans,arr,position
def solve():
n = int(input())
arr = list_input()
position = {}
ans = []
for i in range(n):
position[arr[i]] = i
for i in range(n):
if arr[i] == i+1:
continue
pos = position[i+1]
wrong_num,right_num = arr[i],i+1
if abs(i-pos) >= n//2:
ans,arr,position = condition_satisfied(ans,position,arr,pos,wrong_num,right_num,i)
else:
if abs(n-pos-1) >= n//2:
ans.append((n,pos+1))
position[right_num],position[arr[-1]] = n-1,pos
arr[pos],arr[-1] = arr[-1],arr[pos]
pos = n-1
if abs(i-pos) >= n//2:
ans,arr,position = condition_satisfied(ans,position,arr,pos,wrong_num,right_num,i)
else:
ans.append((1,n))
position[right_num],position[arr[0]] = 0,pos
arr[0],arr[-1] = arr[-1],arr[0]
pos = 0
ans,arr,position = condition_satisfied(ans,position,arr,pos,wrong_num,right_num,i)
else:
ans.append((1,pos+1))
position[right_num],position[arr[0]] = 0,pos
arr[0],arr[pos] = arr[pos],arr[0]
pos = 0
if abs(i-pos) >= n//2:
ans,arr,position = condition_satisfied(ans,position,arr,pos,wrong_num,right_num,i)
else:
ans.append((1,n))
position[right_num],position[arr[-1]] = n-1,pos
arr[0],arr[-1] = arr[-1],arr[0]
pos = n-1
ans,arr,position = condition_satisfied(ans,position,arr,pos,wrong_num,right_num,i)
print(len(ans))
#print(*arr)
for u,v in ans:
print(u,v)
t = 1
#t = int(input())
for _ in range(t):
solve()
``` | output | 1 | 27,481 | 12 | 54,963 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6]. | instruction | 0 | 27,482 | 12 | 54,964 |
Tags: constructive algorithms, sortings
Correct Solution:
```
def fastio():
import sys
from io import StringIO
from atexit import register
global input
sys.stdin = StringIO(sys.stdin.read())
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.stdout = StringIO()
register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
from math import gcd
from collections import defaultdict as dd, Counter as C
from bisect import bisect_left as bl
n, = I()
a = [i-1 for i in I()]
ans = []
pos = [0] * n
for i in range(n):
pos[a[i]] = i
mid = n//2
def swap(i, j):
x, y = a[i], a[j]
a[j], a[i] = x, y
pos[x], pos[y] = j, i
ans.append([i + 1, j + 1])
if n % 2:
if pos[mid] > mid:
swap(0, pos[mid])
swap(0, mid)
else:
swap(n-1, pos[mid])
swap(n-1, mid)
mid += 1
for i in range(1, n//2):
if pos[i] <= mid:
swap(pos[i], n-1)
else:
swap(pos[i], 0)
swap(0, n-1)
swap(pos[i], i)
if pos[mid+i-1] >= mid:
swap(pos[mid+i-1], 0)
else:
swap(n-1, pos[mid+i-1])
swap(n-1, 0)
swap(pos[mid+i-1], mid+i-1)
if a[0] != 0:
swap(0, pos[0])
print(len(ans))
for i in ans:
print(*i)
``` | output | 1 | 27,482 | 12 | 54,965 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6]. | instruction | 0 | 27,483 | 12 | 54,966 |
Tags: constructive algorithms, sortings
Correct Solution:
```
n=int(input())
l=list(map(int,input().split()))
ll=[-1]*(n+1)
for i in range(n):
ll[l[i]]=i
res=[]
#print(ll)
for k in range(1,n+1):
i=k
j=ll[k]
#print(i,j+1)
if(2*abs((j+1)-i)>=n):
res.append((i,j+1))
l[i-1],l[j]=l[j],l[i-1]
ll[l[i-1]],ll[l[j]]=ll[l[j]],ll[l[i-1]]
elif(i!=(j+1)):
if(2*abs(j+1-n)>=n and 2*abs(i-n)>=n):
res.append((i,n))
l[i-1],l[n-1]=l[n-1],l[i-1]
ll[l[i-1]],ll[l[n-1]]=ll[l[n-1]],ll[l[i-1]]
res.append((j+1,n))
l[j],l[n-1]=l[n-1],l[j]
ll[l[j]],ll[l[n-1]]=ll[l[n-1]],ll[l[j]]
res.append((i,n))
l[i-1],l[n-1]=l[n-1],l[i-1]
ll[l[i-1]],ll[l[n-1]]=ll[l[n-1]],ll[l[i-1]]
elif(2*abs(j+1-1)>=n and 2*abs(i-1)>=n):
res.append((i,1))
l[i-1],l[1-1]=l[1-1],l[i-1]
ll[l[i-1]],ll[l[1-1]]=ll[l[1-1]],ll[l[i-1]]
res.append((j+1,1))
l[j],l[1-1]=l[1-1],l[j]
ll[l[j]],ll[l[1-1]]=ll[l[1-1]],ll[l[j]]
res.append((i,1))
l[i-1],l[1-1]=l[1-1],l[i-1]
ll[l[i-1]],ll[l[1-1]]=ll[l[1-1]],ll[l[i-1]]
else:
#print("lol")
if(i>(j+1)):
i,j=j+1,i-1
res.append((i,n))
l[i-1],l[n-1]=l[n-1],l[i-1]
ll[l[i-1]],ll[l[n-1]]=ll[l[n-1]],ll[l[i-1]]
res.append((j+1,1))
l[j],l[1-1]=l[1-1],l[j]
ll[l[j]],ll[l[1-1]]=ll[l[1-1]],ll[l[j]]
res.append((n,1))
l[n-1],l[1-1]=l[1-1],l[n-1]
ll[l[n-1]],ll[l[1-1]]=ll[l[1-1]],ll[l[n-1]]
res.append((i,n))
l[i-1],l[n-1]=l[n-1],l[i-1]
ll[l[i-1]],ll[l[n-1]]=ll[l[n-1]],ll[l[i-1]]
res.append((j+1,1))
l[j],l[1-1]=l[1-1],l[j]
ll[l[j]],ll[l[1-1]]=ll[l[1-1]],ll[l[j]]
#if(l3==l):
#break
#print(l,ll,sep="\n")
print(len(res))
for i in res:
print(i[0],i[1])
#print(l)
``` | output | 1 | 27,483 | 12 | 54,967 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6]. | instruction | 0 | 27,484 | 12 | 54,968 |
Tags: constructive algorithms, sortings
Correct Solution:
```
from sys import stdin
input = stdin.readline
n = int(input())
a = list(map(int, input().split()))
a = [0] + a
cnt = 0
res = []
def swap(i: int, j: int):
a[i], a[j] = a[j], a[i]
pos = [0] * (n + 1)
for i in range(1, n + 1):
pos[a[i]] = i # the position of a number in a
for i in range(1, n + 1):
p = pos[i] # the position of i in current a
if p == i:
continue
elif abs(p - i) * 2 >= n:
swap(i, p)
cnt += 1
res.append(f'{i} {p}')
elif (i - 1) * 2 >= n:
swap(i, p)
cnt += 3
res.extend([f'1 {i}', f'1 {p}', f'1 {i}'])
elif (p - 1) * 2 < n:
swap(i, p)
cnt += 3
res.extend([f'{p} {n}', f'{i} {n}', f'{p} {n}'])
else:
swap(i, p)
cnt += 5
res.extend(
[f'{i} {n}', f'{1} {p}', f'{1} {n}', f'{i} {n}', f'{1} {p}'])
pos[a[p]] = p
print(cnt)
print('\n'.join(res))
``` | output | 1 | 27,484 | 12 | 54,969 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6].
Submitted Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def main():
n = I()
a = LI_()
d = {}
for i in range(n):
d[a[i]] = i
r = []
def sw(t,u):
c = a[t]
e = a[u]
d[c] = u
d[e] = t
a[t] = e
a[u] = c
r.append('{} {}'.format(t+1,u+1))
n2 = n // 2
for i in range(1,n-1):
if a[i] == i:
continue
t = d[i]
u = a[i]
if abs(t-i) < n2:
p = 0
if abs(t-p) < n2:
p = n-1
sw(t,p)
if abs(p-i) < n2:
p = n - 1 - p
sw(0, n-1)
sw(i,p)
else:
sw(t,i)
if a[0] != 0:
sw(0,n-1)
return '{}\n{}'.format(len(r),'\n'.join(r))
print(main())
``` | instruction | 0 | 27,485 | 12 | 54,970 |
Yes | output | 1 | 27,485 | 12 | 54,971 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6].
Submitted Solution:
```
N = int(input())
M = N//2
A = [int(a)-1 for a in input().split()]
B = [0] * (N)
ANS = []
for i in range(N):
B[A[i]] = i
def calc(i):
j = B[i]
if j == i:
return 0
if abs(j - i) >= M:
ANS.append(str(i+1)+" "+str(j+1))
elif i < M and j < M:
ANS.append(str(i+1)+" "+str(N))
ANS.append(str(j+1)+" "+str(N))
ANS.append(str(i+1)+" "+str(N))
elif i >= M and j >= M:
ANS.append(str(1)+" "+str(i+1))
ANS.append(str(1)+" "+str(j+1))
ANS.append(str(1)+" "+str(i+1))
elif i < M:
ANS.append(str(i+1)+" "+str(N))
ANS.append(str(1)+" "+str(N))
ANS.append(str(1)+" "+str(j+1))
ANS.append(str(1)+" "+str(N))
ANS.append(str(i+1)+" "+str(N))
else:
ANS.append(str(j+1)+" "+str(N))
ANS.append(str(1)+" "+str(N))
ANS.append(str(1)+" "+str(i+1))
ANS.append(str(1)+" "+str(N))
ANS.append(str(j+1)+" "+str(N))
m = A[i]
A[i], A[B[i]] = i, A[i]
B[m] = B[i]
B[i] = i
for i in range(N):
calc(i)
print(len(ANS))
print("\n".join(ANS))
``` | instruction | 0 | 27,486 | 12 | 54,972 |
Yes | output | 1 | 27,486 | 12 | 54,973 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6].
Submitted Solution:
```
n = int(input())
A = list(map(int, input().split()))
Swaps = []
def swap(i, j):
#print("aaaaa", i, j)
if i >= n // 2 and j < n // 2:
swap_in_both_parts(j, i)
#print(1)
elif j >= n // 2 and i < n // 2:
swap_in_both_parts(i, j)
#print(2)
elif j < n // 2 and i < n // 2:
swap_in_left_part(i, j)
#print(3)
elif j >= n // 2 and i >= n // 2:
swap_in_right_part(i, j)
#print(4)
else:
print("Whattt?????")
def swap_in_left_part(f, s):
Swaps.append((f, n - 1))
Swaps.append((s, n - 1))
Swaps.append((f, n - 1))
A[f], A[s] = A[s], A[f]
def swap_in_right_part(f, s):
Swaps.append((0, f))
Swaps.append((0, s))
Swaps.append((0, f))
A[f], A[s] = A[s], A[f]
def swap_in_both_parts(f, s):
if f == 0 or s == n - 1:
Swaps.append((f, s))
else:
Swaps.append((0, s))
Swaps.append((f, n - 1))
Swaps.append((0, n - 1))
Swaps.append((0, s))
Swaps.append((f, n - 1))
A[f], A[s] = A[s], A[f]
i = 0
while i != n:
if i != A[i] - 1:
st, fin = i, A[i] - 1
swap(st, fin)
else:
i += 1
print(len(Swaps))
for i in range(len(Swaps)):
print(Swaps[i][0] + 1, Swaps[i][1] + 1)
``` | instruction | 0 | 27,487 | 12 | 54,974 |
Yes | output | 1 | 27,487 | 12 | 54,975 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6].
Submitted Solution:
```
# from bisect import bisect_left
# (n) = (int(x) for x in input().split())
n = int(input())
a = [int(_) - 1 for _ in input().split()]
b = [0 for _ in a]
for id, v in enumerate(a):
b[v] = id
ans = []
for i in range(n):
pos = b[i]
target = i
old_v = a[target]
if pos == target:
continue
# do swap
b[old_v] = pos
b[i] = i
a[target] = i
a[pos] = old_v
if (abs(pos - target) * 2 >= n):
ans.append((pos, target))
continue
elif max(pos, target) < n // 2:
helper = n - 1
ans.append((pos, helper))
ans.append((target, helper))
ans.append((pos, helper))
elif min(pos, target) >= n // 2:
helper = 0
ans.append((pos, helper))
ans.append((target, helper))
ans.append((pos, helper))
else:
L = 0
R = n - 1
if pos > target:
(pos, target) = (target, pos)
ans.append((pos, R))
ans.append((L, R))
ans.append((L, target))
ans.append((L, R))
ans.append((pos, R))
print(len(ans))
for i in ans:
print(i[0] + 1, i[1] + 1)
``` | instruction | 0 | 27,488 | 12 | 54,976 |
Yes | output | 1 | 27,488 | 12 | 54,977 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6].
Submitted Solution:
```
#!/usr/bin/python
import sys
commands = []
def swap_x_y(array_, x, y):
temp = array_[x - 1]
array_[x - 1] = array_[y - 1]
array_[y - 1] = temp
def mega_swap(array_,x):
n = len(array_)
h = n // 2 + 1
y = array_[x - 1]
first = 1
last = 1
if x < h:
first = n
if y < h:
last = n
commands.append((x,first,))
swap_x_y(array_,x,first)
if first != last:
commands.append((first,last,))
swap_x_y(array_,first,last)
commands.append((last,y,))
swap_x_y(array_,last,y)
n = int(input())
seq = list(map(int,input().split(' ')))
for loop in range(30):
for ind in range(1,n-1):
if ind + 1 != seq[ind]:
mega_swap(seq, ind + 1)
#print(seq)
print(len(commands))
for a,b in commands:
print(a,b)
``` | instruction | 0 | 27,489 | 12 | 54,978 |
No | output | 1 | 27,489 | 12 | 54,979 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6].
Submitted Solution:
```
import sys
from bisect import bisect_left
# gcd
# from fractions import gcd
# from math import ceil, floor
# from copy import deepcopy
# from itertools import accumulate
# l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a']
# print(S.most_common(2)) # [('b', 5), ('c', 4)]
# print(S.keys()) # dict_keys(['a', 'b', 'c'])
# print(S.values()) # dict_values([3, 5, 4])
# print(S.items()) # dict_items([('a', 3), ('b', 5), ('c', 4)])
# from collections import Counter
# import math
# from functools import reduce
#
# fin = open('in_1.txt', 'r')
# sys.stdin = fin
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template
if __name__ == '__main__':
# write code
n = ii()
p = lmi()
# a = [i+1 for i in range(n)]
lastans = []
for i in range(n):
if p[i] != i+1:
if abs(p[i] - i - 1) >= n // 2:
ans = [(p[i], i + 1)]
for an in ans:
p[an[0]-1],p[an[1]-1] = p[an[1]-1],p[an[0]-1]
lastans.append(ans)
elif p[i] < n // 2 and i+1 < n // 2:
ans = [(i + 1, n), (p[i], n)]
for an in ans:
p[an[0]-1],p[an[1]-1] = p[an[1]-1],p[an[0]-1]
lastans.append(ans)
elif p[i] >= n // 2 and i+1 >= n // 2:
ans = [(i+1, 1), (p[i], 1)]
for an in ans:
p[an[0]-1],p[an[1]-1] = p[an[1]-1],p[an[0]-1]
lastans.append(ans)
# elif p[i] < i + 1:
# ans = [(p[i], n), (i + 1, 1), (1, n), (i + 1, 1), (p[i], n)]
# for an in ans:
# p[an[0]-1],p[an[1]-1] = p[an[1]-1],p[an[0]-1]
# lastans.append(ans)
elif p[i] > i + 1:
ans = [(i + 1, n), (p[i], 1), (1, n), (p[i], 1), (i + 1, n)]
for an in ans:
p[an[0]-1],p[an[1]-1] = p[an[1]-1],p[an[0]-1]
lastans.append(ans)
# print(p)
cnt = len([_ for ans in lastans for _ in ans])
print(cnt)
for ans in lastans:
for an in ans:
print("{} {}".format(an[0], an[1]))
``` | instruction | 0 | 27,490 | 12 | 54,980 |
No | output | 1 | 27,490 | 12 | 54,981 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6].
Submitted Solution:
```
def get(list,n):
l=[]
i,z=0,True
while z:
if list[i]==i+1:
i+=1
else:
if abs(list[i]-(i+1))>=n//2:
a=list[list[i]-1]
temp=list[i]
list[temp-1]=temp
list[i]=a
l.append([min(i+1,temp),max(i+1,temp)])
if list[i]==i+1:
i+=1
else:
if n-(i+1)>=n//2 and n-list[i]>=n//2:
a=list[list[i]-1]
b=list[i]
c=list[n-1]
list[i]=c
list[n-1]=a
list[b-1]=b
l.append([i+1,n])
l.append([b,n])
elif i>=n//2 and list[i]-1>=n//2:
a=list[list[i]-1]
b=list[i]
c=list[0]
list[i]=c
list[0]=a
list[b-1]=b
l.append([1,i+1])
l.append([1,b])
i=0
else:
j=i
x=True
while j<n and x:
if list[j]==i+1:
x=False
else:
j+=1
l.append([i+1,j+1])
c=list[i]
list[i]=i+1
list[j]=c
i+=1
if check(list):
z=False
return l
def check(list):
i,z=0,True
while i<len(list) and z:
if list[i]==i+1:
pass
else:
z=False
i+=1
return z
n=int(input())
l=list(map(int,input().strip().split()))
m=get(l,n)
print(len(m))
for i in range(len(m)):
print(" ".join(str(x) for x in m[i]))
``` | instruction | 0 | 27,491 | 12 | 54,982 |
No | output | 1 | 27,491 | 12 | 54,983 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a permutation p of integers from 1 to n, where n is an even number.
Your goal is to sort the permutation. To do so, you can perform zero or more operations of the following type:
* take two indices i and j such that 2 β
|i - j| β₯ n and swap p_i and p_j.
There is no need to minimize the number of operations, however you should use no more than 5 β
n operations. One can show that it is always possible to do that.
Input
The first line contains a single integer n (2 β€ n β€ 3 β
10^5, n is even) β the length of the permutation.
The second line contains n distinct integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n) β the given permutation.
Output
On the first line print m (0 β€ m β€ 5 β
n) β the number of swaps to perform.
Each of the following m lines should contain integers a_i, b_i (1 β€ a_i, b_i β€ n, |a_i - b_i| β₯ n/2) β the indices that should be swapped in the corresponding swap.
Note that there is no need to minimize the number of operations. We can show that an answer always exists.
Examples
Input
2
2 1
Output
1
1 2
Input
4
3 4 1 2
Output
4
1 4
1 4
1 3
2 4
Input
6
2 5 3 1 4 6
Output
3
1 5
2 5
1 4
Note
In the first example, when one swap elements on positions 1 and 2, the array becomes sorted.
In the second example, pay attention that there is no need to minimize number of swaps.
In the third example, after swapping elements on positions 1 and 5 the array becomes: [4, 5, 3, 1, 2, 6]. After swapping elements on positions 2 and 5 the array becomes [4, 2, 3, 1, 5, 6] and finally after swapping elements on positions 1 and 4 the array becomes sorted: [1, 2, 3, 4, 5, 6].
Submitted Solution:
```
def fastio():
import sys
from io import StringIO
from atexit import register
global input
sys.stdin = StringIO(sys.stdin.read())
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.stdout = StringIO()
register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
from math import gcd
from collections import defaultdict as dd, Counter as C
from bisect import bisect_left as bl
n, = I()
a = [i-1 for i in I()]
ans = []
pos = [0] * n
for i in range(n):
pos[a[i]] = i
mid = n//2
def swap(i, j):
x, y = a[i], a[j]
a[j], a[i] = x, y
pos[x], pos[y] = j, i
ans.append([i + 1, j + 1])
if n % 2:
if pos[mid] > mid:
swap(0, pos[mid])
swap(0, mid)
else:
swap(n-1, pos[mid])
swap(n-1, mid)
mid += 1
for i in range(1, n//2):
if pos[i] <= mid:
swap(pos[i], n-1)
swap(pos[i], i)
if pos[mid+i-1] >= mid:
swap(pos[mid+i-1], 0)
swap(pos[mid+i-1], mid+i-1)
if a[0] != 0:
swap(0, pos[0])
print(len(ans))
for i in ans:
print(*i)
# print(a)
``` | instruction | 0 | 27,492 | 12 | 54,984 |
No | output | 1 | 27,492 | 12 | 54,985 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given a sequence of integers a of length n, a tuple (i,j,k) is called monotone triples if
* 1 β€ i<j<kβ€ n;
* a_i β€ a_j β€ a_k or a_i β₯ a_j β₯ a_k is satisfied.
For example, a=[5,3,4,5], then (2,3,4) is monotone triples for sequence a while (1,3,4) is not.
Bob is given a sequence of integers a of length n in a math exam. The exams itself contains questions of form L, R, for each of them he is asked to find any subsequence b with size greater than 2 (i.e. |b| β₯ 3) of sequence a_L, a_{L+1},β¦, a_{R}.
Recall that an sequence b is a subsequence of sequence a if b can be obtained by deletion of several (possibly zero, or all) elements.
However, he hates monotone stuff, and he wants to find a subsequence free from monotone triples. Besides, he wants to find one subsequence with the largest length among all subsequences free from monotone triples for every query.
Please help Bob find out subsequences meeting the above constraints.
Input
The first line contains two integers n, q (3 β€ n β€ 2 β
10^5, 1 β€ q β€ 2 β
10^5) β the length of sequence a and the number of queries.
The second line contains n integers a_1,a_2,β¦, a_n (1 β€ a_i β€ 10^{9}), representing the sequence a.
Then each of the following q lines contains two integers L, R (1 β€ L,R β€ n, R-Lβ₯ 2).
Output
For each query, output 0 if there is no subsequence b satisfying the constraints mentioned in the legend. You can print the empty line after but that's not mandatory.
Otherwise, output one integer k (k > 2) denoting the length of sequence b, then output k integers i_1, i_2, β¦, i_k (L β€ i_1 < i_2<β¦<i_kβ€ R) satisfying that b_j = a_{i_j} for 1 β€ j β€ k.
If there are multiple answers with the maximum length, print any of them.
Example
Input
6 2
3 1 4 1 5 9
1 3
4 6
Output
3
1 2 3
0
Note
For the first query, the given sequence itself is monotone triples free.
For the second query, it can be shown that there is no subsequence b with length greater than 2 such that b is monotone triples free. | instruction | 0 | 27,563 | 12 | 55,126 |
Tags: data structures
Correct Solution:
```
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from operator import itemgetter
import bisect
n,q=map(int,input().split())
A=[-1]+list(map(int,input().split()))
Q=[list(map(int,input().split()))+[i] for i in range(q)]
Q.sort(key=itemgetter(1))
Q_ind=0
ANS1=[-1000,-1000,-1000,-1000]
ANS2=[-1000,-1000,-1000]
ANS3=[-1000,-1000,-1000]
ANS=[[0]]*q
Increase=[-1]
Decrease=[-1]
Increase2=[-1]
Decrease2=[-1]
No_inclast=[-1]*(n+1)
No_declast=[-1]*(n+1)
Inc_next=[-1]*(n+1)
Dec_next=[-1]*(n+1)
LEN=n
BIT=[0]*(LEN+1)
def update(v,w):
while v<=LEN:
BIT[v]+=w
v+=(v&(-v))
def getvalue(v):
ANS=0
while v!=0:
ANS+=BIT[v]
v-=(v&(-v))
return ANS
def bisect_on_BIT(x):
if x<=0:
return 0
ANS=0
h=1<<(LEN.bit_length()-1)
while h>0:
if ANS+h<=LEN and BIT[ANS+h]<x:
x-=BIT[ANS+h]
ANS+=h
h//=2
return ANS+1
No_incdeclist=[0]*n
for i in range(1,n+1):
No_inc=-1
No_dec=-1
while Increase[-1]!=-1 and A[i]<A[Increase[-1]]:
ind=Increase.pop()
if Increase2[-1]==ind:
Increase2.pop()
No_incdeclist[ind]+=1
if No_incdeclist[ind]==2:
update(ind,1)
if No_inc==-1:
No_inc=ind
while Increase2[-1]!=-1 and A[i]==A[Increase2[-1]]:
Increase2.pop()
Increase.append(i)
Increase2.append(i)
if No_inc!=-1:
No_inclast[i]=No_inc
if Inc_next[No_inc]==-1:
Inc_next[No_inc]=i
else:
No_inclast[i]=No_inclast[i-1]
while Decrease[-1]!=-1 and A[i]>A[Decrease[-1]]:
ind=Decrease.pop()
if Decrease2[-1]==ind:
Decrease2.pop()
No_incdeclist[ind]+=1
if No_incdeclist[ind]==2:
update(ind,1)
if No_dec==-1:
No_dec=ind
while Decrease2[-1]!=-1 and A[i]==A[Decrease2[-1]]:
Decrease2.pop()
Decrease.append(i)
Decrease2.append(i)
if No_dec!=-1:
No_declast[i]=No_dec
if Dec_next[No_dec]==-1:
Dec_next[No_dec]=i
else:
No_declast[i]=No_declast[i-1]
MININD=min(Increase2[-2],Decrease2[-2])
if MININD>1:
MIN=bisect_on_BIT(getvalue(MININD))
x=Increase[bisect.bisect_left(Increase,MIN)]
y=Decrease[bisect.bisect_left(Decrease,MIN)]
if MIN>0 and ANS1[0]<MIN and A[x]<A[i] and A[y]>A[i]:
if x>y:
x,y=y,x
ANS1=[MIN,x,y,i]
n_inc=No_inclast[i]
mid=Inc_next[n_inc]
if n_inc>0 and A[mid]<A[i] and ANS2[0]<n_inc:
ANS2=[n_inc,mid,i]
n_dec=No_declast[i]
mid=Dec_next[n_dec]
if n_dec>0 and A[mid]>A[i] and ANS3[0]<n_dec:
ANS3=[n_dec,mid,i]
while Q_ind<q and Q[Q_ind][1]==i:
l,r,qu=Q[Q_ind]
if ANS1[0]>=l:
ANS[qu]=ANS1
elif ANS2[0]>=l:
ANS[qu]=ANS2
elif ANS3[0]>=l:
ANS[qu]=ANS3
Q_ind+=1
#print(Increase,Decrease,inclast,declast,No_inclast,No_declast)
#print(ANS1,ANS2,ANS3)
#print()
for x in ANS:
if x==[0]:
sys.stdout.write(str(0)+"\n")
else:
sys.stdout.write(str(len(x))+"\n")
sys.stdout.write(" ".join(map(str,x))+"\n")
``` | output | 1 | 27,563 | 12 | 55,127 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given a sequence of integers a of length n, a tuple (i,j,k) is called monotone triples if
* 1 β€ i<j<kβ€ n;
* a_i β€ a_j β€ a_k or a_i β₯ a_j β₯ a_k is satisfied.
For example, a=[5,3,4,5], then (2,3,4) is monotone triples for sequence a while (1,3,4) is not.
Bob is given a sequence of integers a of length n in a math exam. The exams itself contains questions of form L, R, for each of them he is asked to find any subsequence b with size greater than 2 (i.e. |b| β₯ 3) of sequence a_L, a_{L+1},β¦, a_{R}.
Recall that an sequence b is a subsequence of sequence a if b can be obtained by deletion of several (possibly zero, or all) elements.
However, he hates monotone stuff, and he wants to find a subsequence free from monotone triples. Besides, he wants to find one subsequence with the largest length among all subsequences free from monotone triples for every query.
Please help Bob find out subsequences meeting the above constraints.
Input
The first line contains two integers n, q (3 β€ n β€ 2 β
10^5, 1 β€ q β€ 2 β
10^5) β the length of sequence a and the number of queries.
The second line contains n integers a_1,a_2,β¦, a_n (1 β€ a_i β€ 10^{9}), representing the sequence a.
Then each of the following q lines contains two integers L, R (1 β€ L,R β€ n, R-Lβ₯ 2).
Output
For each query, output 0 if there is no subsequence b satisfying the constraints mentioned in the legend. You can print the empty line after but that's not mandatory.
Otherwise, output one integer k (k > 2) denoting the length of sequence b, then output k integers i_1, i_2, β¦, i_k (L β€ i_1 < i_2<β¦<i_kβ€ R) satisfying that b_j = a_{i_j} for 1 β€ j β€ k.
If there are multiple answers with the maximum length, print any of them.
Example
Input
6 2
3 1 4 1 5 9
1 3
4 6
Output
3
1 2 3
0
Note
For the first query, the given sequence itself is monotone triples free.
For the second query, it can be shown that there is no subsequence b with length greater than 2 such that b is monotone triples free.
Submitted Solution:
```
def find_seq(seq,index):
sub=seq[index[0]:index[1]+1]
stack=[]
print(sub)
for a in range(len(sub)):
for b in range(a+1,len(sub)):
if sub[b]>sub[a]:
for c in range(b+1,len(sub)):
if sub[b]>=sub[c] :
stack.append([a,b,c])
for d in range(c+1,len(sub)):
if sub[d]>=sub[c] and sub[b]>=sub[d] and sub[c]<=sub[a]:
stack.append([a,b,c,d])
elif sub[b]<sub[a]:
for c in range(b+1,len(sub)):
if sub[b]<=sub[c] :
stack.append([a,b,c])
for d in range(c+1,len(sub)):
if sub[d]<=sub[c] and sub[b]<=sub[d] and sub[c]>=sub[a]:
stack.append([a,b,c,d])
k=[]
print(stack)
for ele in stack:
k=ele
if len(k)==4:
return k
return k
n,q=input().split()
n=int(n)
q=int(q)
temp=input()
seq=[int(x) for x in temp.split(" ",n)]
indexes=[]
for i in range(q):
t=input()
indexes.append([int(x) for x in t.split()])
seq.insert(0,-1)
for i in range(q):
subseq=find_seq(seq,indexes[i])
lnth=len(subseq)
if lnth>2:
string=""
for ele in subseq:
ele+=indexes[i][0]
string=string+str(ele)+" "
print(lnth)
print(string)
else:
print(0)
``` | instruction | 0 | 27,564 | 12 | 55,128 |
No | output | 1 | 27,564 | 12 | 55,129 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given a sequence of integers a of length n, a tuple (i,j,k) is called monotone triples if
* 1 β€ i<j<kβ€ n;
* a_i β€ a_j β€ a_k or a_i β₯ a_j β₯ a_k is satisfied.
For example, a=[5,3,4,5], then (2,3,4) is monotone triples for sequence a while (1,3,4) is not.
Bob is given a sequence of integers a of length n in a math exam. The exams itself contains questions of form L, R, for each of them he is asked to find any subsequence b with size greater than 2 (i.e. |b| β₯ 3) of sequence a_L, a_{L+1},β¦, a_{R}.
Recall that an sequence b is a subsequence of sequence a if b can be obtained by deletion of several (possibly zero, or all) elements.
However, he hates monotone stuff, and he wants to find a subsequence free from monotone triples. Besides, he wants to find one subsequence with the largest length among all subsequences free from monotone triples for every query.
Please help Bob find out subsequences meeting the above constraints.
Input
The first line contains two integers n, q (3 β€ n β€ 2 β
10^5, 1 β€ q β€ 2 β
10^5) β the length of sequence a and the number of queries.
The second line contains n integers a_1,a_2,β¦, a_n (1 β€ a_i β€ 10^{9}), representing the sequence a.
Then each of the following q lines contains two integers L, R (1 β€ L,R β€ n, R-Lβ₯ 2).
Output
For each query, output 0 if there is no subsequence b satisfying the constraints mentioned in the legend. You can print the empty line after but that's not mandatory.
Otherwise, output one integer k (k > 2) denoting the length of sequence b, then output k integers i_1, i_2, β¦, i_k (L β€ i_1 < i_2<β¦<i_kβ€ R) satisfying that b_j = a_{i_j} for 1 β€ j β€ k.
If there are multiple answers with the maximum length, print any of them.
Example
Input
6 2
3 1 4 1 5 9
1 3
4 6
Output
3
1 2 3
0
Note
For the first query, the given sequence itself is monotone triples free.
For the second query, it can be shown that there is no subsequence b with length greater than 2 such that b is monotone triples free.
Submitted Solution:
```
def isMonotone(l):
i=1
n=len(l)
while i<n:
if l[i]<l[i-1]:
break
i+=1
else:
return True
i=1
while(i<n):
if l[i]>l[i-1]:
return False
i+=1
return True
def sortlen(finalmat):
n=len(finalmat)
for i in range(n):
for j in range(n-i-1):
if len(finalmat[j])<len(finalmat[j+1]):
finalmat[j], finalmat[j+1] = finalmat[j+1], finalmat[j]
return finalmat
n,q=map(int,input().split())
s=list(map(int,input().split()))
for _ in range(q):
L,R=map(int,input().split())
m=R-L+1
arr=s[L-1:R]
finalmat=[]
for i in range(1,m**2):
l=[]
for j in range(m):
if (1<<j)&i:
l.append(arr[j])
if len(l)>2 and not isMonotone(l):
finalmat.append(l.copy())
if len(finalmat)==0:
print(0)
else:
finalmat=sortlen(finalmat)
outsub=[]
for i in finalmat[0]:
outsub.append(arr.index(i)+1)
for i in outsub:
print(i,end=' ')
print()
``` | instruction | 0 | 27,565 | 12 | 55,130 |
No | output | 1 | 27,565 | 12 | 55,131 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given a sequence of integers a of length n, a tuple (i,j,k) is called monotone triples if
* 1 β€ i<j<kβ€ n;
* a_i β€ a_j β€ a_k or a_i β₯ a_j β₯ a_k is satisfied.
For example, a=[5,3,4,5], then (2,3,4) is monotone triples for sequence a while (1,3,4) is not.
Bob is given a sequence of integers a of length n in a math exam. The exams itself contains questions of form L, R, for each of them he is asked to find any subsequence b with size greater than 2 (i.e. |b| β₯ 3) of sequence a_L, a_{L+1},β¦, a_{R}.
Recall that an sequence b is a subsequence of sequence a if b can be obtained by deletion of several (possibly zero, or all) elements.
However, he hates monotone stuff, and he wants to find a subsequence free from monotone triples. Besides, he wants to find one subsequence with the largest length among all subsequences free from monotone triples for every query.
Please help Bob find out subsequences meeting the above constraints.
Input
The first line contains two integers n, q (3 β€ n β€ 2 β
10^5, 1 β€ q β€ 2 β
10^5) β the length of sequence a and the number of queries.
The second line contains n integers a_1,a_2,β¦, a_n (1 β€ a_i β€ 10^{9}), representing the sequence a.
Then each of the following q lines contains two integers L, R (1 β€ L,R β€ n, R-Lβ₯ 2).
Output
For each query, output 0 if there is no subsequence b satisfying the constraints mentioned in the legend. You can print the empty line after but that's not mandatory.
Otherwise, output one integer k (k > 2) denoting the length of sequence b, then output k integers i_1, i_2, β¦, i_k (L β€ i_1 < i_2<β¦<i_kβ€ R) satisfying that b_j = a_{i_j} for 1 β€ j β€ k.
If there are multiple answers with the maximum length, print any of them.
Example
Input
6 2
3 1 4 1 5 9
1 3
4 6
Output
3
1 2 3
0
Note
For the first query, the given sequence itself is monotone triples free.
For the second query, it can be shown that there is no subsequence b with length greater than 2 such that b is monotone triples free.
Submitted Solution:
```
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from operator import itemgetter
import bisect
n,q=map(int,input().split())
A=list(map(int,input().split()))
Q=[list(map(int,input().split()))+[i] for i in range(q)]
Q.sort(key=itemgetter(1))
Q_ind=0
ANS1=[-1000,-1000,-1000,-1000]
ANS2=[-1000,-1000,-1000,-1000]
ANS3=[-1000,-1000,-1000,-1000]
ANS=[0]*q
Increase=[(-1,-1<<30)]
Decrease=[(-1,1<<30)]
I_start=0
D_start=0
No_inclast=[-1]*n
No_declast=[-1]*n
LEN=n
BIT=[0]*(LEN+1)
def update(v,w):
while v<=LEN:
BIT[v]+=w
v+=(v&(-v))
def getvalue(v):
ANS=0
while v!=0:
ANS+=BIT[v]
v-=(v&(-v))
return ANS
def bisect_on_BIT(x):
if x<=0:
return 0
ANS=0
h=1<<(LEN.bit_length()-1)
while h>0:
if ANS+h<=LEN and BIT[ANS+h]<x:
x-=BIT[ANS+h]
ANS+=h
h//=2
return ANS+1
No_incdeclist=[0]*n
for i in range(n):
No_inc=-1
No_dec=-1
while A[i]<=Increase[-1][1]:
ind,a=Increase.pop()
No_incdeclist[ind]+=1
if No_incdeclist[ind]==2:
update(ind+1,1)
if No_inc==-1:
No_inc=ind
Increase.append((i,A[i]))
if No_inc!=-1:
No_inclast[i]=No_inc
else:
No_inclast[i]=No_inclast[i-1]
while A[i]>=Decrease[-1][1]:
ind,a=Decrease.pop()
No_incdeclist[ind]+=1
if No_incdeclist[ind]==2:
update(ind+1,1)
if No_dec==-1:
No_dec=ind
Decrease.append((i,A[i]))
if No_dec!=-1:
No_declast[i]=No_dec
else:
No_declast[i]=No_declast[i-1]
inclast=Increase[-2][0]
declast=Decrease[-2][0]
if inclast!=-1 and declast!=-1:
MIN=bisect_on_BIT(getvalue(min(inclast,declast)+1))-1
#print(Increase,Decrease,i,MIN)
x=Increase[bisect.bisect_left(Increase,(MIN,0))][0]
y=Decrease[bisect.bisect_left(Decrease,(MIN,0))][0]
if x>y:
x,y=y,x
if ANS1[0]<MIN:
ANS1=[MIN+1,x+1,y+1,i+1]
if inclast!=-1 and No_inclast[inclast]!=-1:
if ANS2[0]<No_inclast[inclast]:
ANS2=[No_inclast[inclast]+1,Increase[bisect.bisect_left(Increase,(No_inclast[inclast],0))][0]+1,i+1]
if declast!=-1 and No_declast[declast]!=-1:
if ANS3[0]<No_declast[declast]:
ANS3=[No_declast[declast]+1,Decrease[bisect.bisect_left(Decrease,(No_declast[declast],0))][0]+1,i+1]
while Q_ind<q and Q[Q_ind][1]-1==i:
l,r,qu=Q[Q_ind]
if ANS1[0]>=l:
ANS[qu]=ANS1
elif ANS2[0]>=l:
ANS[qu]=ANS2
elif ANS3[0]>=l:
ANS[qu]=ANS3
Q_ind+=1
#print(Increase,Decrease,No_inclast,No_declast)
#print(ANS1,ANS2,ANS3)
#print()
for x in ANS:
if x==0:
sys.stdout.write(str(0)+"\n")
else:
sys.stdout.write(str(len(x))+"\n")
sys.stdout.write(" ".join(map(str,x))+"\n")
``` | instruction | 0 | 27,566 | 12 | 55,132 |
No | output | 1 | 27,566 | 12 | 55,133 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given a sequence of integers a of length n, a tuple (i,j,k) is called monotone triples if
* 1 β€ i<j<kβ€ n;
* a_i β€ a_j β€ a_k or a_i β₯ a_j β₯ a_k is satisfied.
For example, a=[5,3,4,5], then (2,3,4) is monotone triples for sequence a while (1,3,4) is not.
Bob is given a sequence of integers a of length n in a math exam. The exams itself contains questions of form L, R, for each of them he is asked to find any subsequence b with size greater than 2 (i.e. |b| β₯ 3) of sequence a_L, a_{L+1},β¦, a_{R}.
Recall that an sequence b is a subsequence of sequence a if b can be obtained by deletion of several (possibly zero, or all) elements.
However, he hates monotone stuff, and he wants to find a subsequence free from monotone triples. Besides, he wants to find one subsequence with the largest length among all subsequences free from monotone triples for every query.
Please help Bob find out subsequences meeting the above constraints.
Input
The first line contains two integers n, q (3 β€ n β€ 2 β
10^5, 1 β€ q β€ 2 β
10^5) β the length of sequence a and the number of queries.
The second line contains n integers a_1,a_2,β¦, a_n (1 β€ a_i β€ 10^{9}), representing the sequence a.
Then each of the following q lines contains two integers L, R (1 β€ L,R β€ n, R-Lβ₯ 2).
Output
For each query, output 0 if there is no subsequence b satisfying the constraints mentioned in the legend. You can print the empty line after but that's not mandatory.
Otherwise, output one integer k (k > 2) denoting the length of sequence b, then output k integers i_1, i_2, β¦, i_k (L β€ i_1 < i_2<β¦<i_kβ€ R) satisfying that b_j = a_{i_j} for 1 β€ j β€ k.
If there are multiple answers with the maximum length, print any of them.
Example
Input
6 2
3 1 4 1 5 9
1 3
4 6
Output
3
1 2 3
0
Note
For the first query, the given sequence itself is monotone triples free.
For the second query, it can be shown that there is no subsequence b with length greater than 2 such that b is monotone triples free.
Submitted Solution:
```
#Input
line = input("")
l = line.split(" ")
n = int(l[0])
q = int(l[1])
line = input("")
nums = line.split(" ")
queries = []
for _ in range(q):
line = input("")
l = line.split(" ")
queries.append(l)
#Output
for query in queries:
largest_subsequence_len = curr_subsequence_len = 0
largest_subsequence_start = curr_subsequence_start = -1
largest_subsequence_end = -1
for i in range(int(query[0]) + 1, int(query[1])):
if nums[i - 2] <= nums[i - 1] <= nums[i] or nums[i - 2] >= nums[i - 1] >= nums[i]:
curr_subsequence_len = 0
curr_subsequence_start = -1
continue
if curr_subsequence_start == -1:
curr_subsequence_start = i - 2
curr_subsequence_len += 1
if curr_subsequence_len > largest_subsequence_len:
largest_subsequence_len = curr_subsequence_len
largest_subsequence_start = curr_subsequence_start
largest_subsequence_end = i
if largest_subsequence_len == 0:
print("0")
else:
print(str(largest_subsequence_len + 2))
for i in range(largest_subsequence_start + 1, largest_subsequence_end + 2):
print(str(i), end=" ")
print("")
``` | instruction | 0 | 27,567 | 12 | 55,134 |
No | output | 1 | 27,567 | 12 | 55,135 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6 | instruction | 0 | 27,601 | 12 | 55,202 |
Tags: constructive algorithms, greedy, number theory
Correct Solution:
```
import sys
n = int(input())
ar = [int(i) for i in input().split()]
if n == 1:
print(1, 1)
print(-ar[0])
print(1, 1)
print(0)
print(1,1)
print(0)
sys.exit(0)
print(1, n-1)
for i in range(n-1):
print(ar[i]*(n-1), end=' ')
ar[i] = ar[i]*n
print('')
print(n, n)
print(-ar[n-1])
ar[n-1] = 0
print(1, n)
for i in range(n):
print(-ar[i], end=' ')
``` | output | 1 | 27,601 | 12 | 55,203 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6 | instruction | 0 | 27,602 | 12 | 55,204 |
Tags: constructive algorithms, greedy, number theory
Correct Solution:
```
# cook your dish here
n=int(input())
a=list(map(int,input().split()))
if(n==1):
print(1,1)
print(a[0]*-1)
print(1,1)
print(0)
print(1,1)
print(0)
else:
print(1,n)
ans=[]
for i in range(n):
ans.append(a[i]*-1*n)
print(*ans)
print(1,n-1)
res=[]
for i in range(n-1):
res.append(a[i]*(n-1))
print(*res)
print(n,n)
print(a[-1]*(n-1))
``` | output | 1 | 27,602 | 12 | 55,205 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6 | instruction | 0 | 27,603 | 12 | 55,206 |
Tags: constructive algorithms, greedy, number theory
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
print("1 1")
print(-a[0])
if n != 1:
print(2, n)
for i in range(1, n):
print((n-1) * a[i], end=" ")
print("")
print(1, n)
print(0, end=" ")
for i in range(1, n):
print(-n * a[i], end=" ")
else:
print("1 1\n0\n1 1\n0")
``` | output | 1 | 27,603 | 12 | 55,207 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6 | instruction | 0 | 27,604 | 12 | 55,208 |
Tags: constructive algorithms, greedy, number theory
Correct Solution:
```
from collections import defaultdict as dc
import math
import sys
input=sys.stdin.readline
n=int(input())
l=list(map(int,input().split()))
if n>1:
p=[0]*n
q=[0]*(n-1)
r=[0]*1
for i in range(n-1):
p[i]=-1*(l[i])*n
q[i]=l[i]*(n-1)
r[0]=-l[n-1]-n
p[n-1]=n
print(1,n-1)
for i in range(n-2):
print(q[i], end=" ")
print(q[-1])
print(1,n)
for i in range(n-1):
print(p[i],end=" ")
print(p[-1])
print(n,n)
print(r[0])
else:
print(1,1)
print(0)
print(1,1)
print(0)
print(1,1)
print(-l[0])
``` | output | 1 | 27,604 | 12 | 55,209 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6 | instruction | 0 | 27,605 | 12 | 55,210 |
Tags: constructive algorithms, greedy, number theory
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
n = int(input())
a = list(map(int,input().split()))
if n == 1:
print(1,1)
print(0)
print(1,1)
print(0)
print(1,1)
print(-a[0])
else:
print(1,1)
print(-a[0])
print(2,n)
b = [0]*(n-1)
for i in range(1,n):
b[i-1] = a[i]*(n-1)
print(*b)
print(1,n)
b = [0]*(n)
for i in range(1,n):
b[i] = -a[i]*n
print(*b)
``` | output | 1 | 27,605 | 12 | 55,211 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6 | instruction | 0 | 27,606 | 12 | 55,212 |
Tags: constructive algorithms, greedy, number theory
Correct Solution:
```
n = int(input())
a = [int(a) for a in input().split()]
if n == 1:
print(f"1 1\n{-a[0]}\n1 1\n0\n1 1\n0")
else:
output = f'1 {n}\n'
for i in range(n):
to_sum = a[i]%(n-1)*n
if (a[i] + to_sum) % (n-1) == 0:
a[i] += to_sum
output += f"{to_sum} "
else:
a[i] -= to_sum
output += f"-{to_sum} "
output += f'\n1 {n-1}\n'
for i in range(n-1):
output += str(-a[i]) + ' '
output += f'\n{n} {n}\n {-a[n-1]}'
print(output)
``` | output | 1 | 27,606 | 12 | 55,213 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6 | instruction | 0 | 27,607 | 12 | 55,214 |
Tags: constructive algorithms, greedy, number theory
Correct Solution:
```
# function to convert a list of strings to space-separated string
def convert(lst):
return ' '.join(lst)
arr_size = int(input())
#read a string of arr_size integers separated by spaces
arr = list(map(int, input().split()))
#get rid of the first element
print("1 1")
print(-arr[0])
arr[0] = 0
if arr_size > 1:
#make the rest of the elements multiples of arr_size
print("2 " + str(arr_size))
to_add_list = [i * (arr_size - 1) for i in arr]
to_add_list = [str(i) for i in to_add_list]
print(convert(to_add_list[-(arr_size-1):]))
arr = [i * arr_size for i in arr]
#get rid of the rest of the elements
print("1 " + str(arr_size))
to_add_list = [i * ( - 1) for i in arr]
to_add_list = [str(i) for i in to_add_list]
print(convert(to_add_list))
else:
print("1 1")
print(0)
print("1 1")
print(0)
``` | output | 1 | 27,607 | 12 | 55,215 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6
Submitted Solution:
```
N = int(input())
a = list(map(int,input().split()))
if N ==1:
print(1,1)
print(-a[0])
print(1,1)
print(0)
print(1,1)
print(0)
exit()
print(1,1)
print(-a[0])
a[0]=0
print(2,N)
ans = []
for i in range(1,N):
ans.append((a[i]%N)*(N-1))
a[i]=a[i]+(a[i]%N)*(N-1)
print(" ".join(list(map(str,ans))))
for i in range(N):
a[i]=-a[i]
print(1,N)
print(" ".join(list(map(str,a))))
``` | instruction | 0 | 27,609 | 12 | 55,218 |
Yes | output | 1 | 27,609 | 12 | 55,219 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6
Submitted Solution:
```
n=int(input())
arr=list(map(int,input().split()))
if n==1:
print("1 1")
print(-1*arr[0])
print("1 1")
print(0)
print("1 1")
print(0)
else:
print("1 1")
print(-1 * arr[0])
ans=[]
print("2 "+str(n))
for i in range(1,n):
print((n-1)*(arr[i]),end=" ")
ans.append((n)*arr[i])
print()
print("1 "+str(n))
print(0,end=" ")
for d in ans:
print(-1*d,end=" ")
print()
``` | instruction | 0 | 27,610 | 12 | 55,220 |
Yes | output | 1 | 27,610 | 12 | 55,221 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6
Submitted Solution:
```
from sys import stdin
from math import ceil, gcd
# Input data
#stdin = open("input", "r")
def func():
return
n = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
if n == 1:
print(1, 1)
print(-arr[0])
print(1, 1)
print(0)
print(1, 1)
print(0)
else:
print(1, n - 1)
ans = []
for i in range(n - 1):
if arr[i] % n == 0:
ans.append(0)
else:
ans.append((n - 1) * arr[i])
arr[i] += (n - 1) * arr[i]
print(*ans)
print(2, n)
ans = []
for i in range(1, n):
if arr[i] % n == 0:
ans.append(0)
else:
ans.append((n - 1) * arr[i])
arr[i] += (n - 1) * arr[i]
print(*ans)
print(1, n)
ans = []
for i in range(n):
ans.append(-arr[i])
print(*ans)
``` | instruction | 0 | 27,611 | 12 | 55,222 |
Yes | output | 1 | 27,611 | 12 | 55,223 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6
Submitted Solution:
```
n=int(input())
a=list(map(int, input().split()))
if n>1:
print(1,n)
for i in a:
print(-1*i*n,end=' ')
print('')
print(1,n-1)
for i in range(n-1):
print(a[i]*((n-1)),end=' ')
print('')
print(n, n)
print(-a[n - 1]+n*a[n-1])
else:
print(n, n)
print(-a[n - 1])
print(n,n)
print(0)
print(n, n)
print(0)
``` | instruction | 0 | 27,612 | 12 | 55,224 |
Yes | output | 1 | 27,612 | 12 | 55,225 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6
Submitted Solution:
```
n=int(input())
l=list(map(int,input().split()))
r=[]
for i in l:
r.append(n-(i%n))
ans=[]
print(1,n-1)
for i in range(n-1):
if(l[i]>=0):
ans.append(l[i]+(n-1)*(n-r[i]))
print((n - 1) * (n - r[i]),end=" ")
else:
ans.append(l[i]-(n-1)*(n-r[i]))
print(-(n-1)*(n-r[i]),end=" ")
print()
print(n,n)
print(-l[n-1])
print(1,n)
ans.append(0)
print(*ans)
``` | instruction | 0 | 27,613 | 12 | 55,226 |
No | output | 1 | 27,613 | 12 | 55,227 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6
Submitted Solution:
```
n=int(input())
arr = list(map(int,input().split()))
if n==1:
print(1,1)
print(0)
print(1,1)
print(0)
print(1,1)
print(-1*arr[0])
else:
print(1,1)
print(-1*arr[0])
arr[0]=0
print(2,n)
ans = []
for i in range(1,n):
x = arr[i]%(n)
if arr[i]<n-1:
ans.append(arr[i]%(n))
elif arr[i]>n-1:
ans.append(-1*(arr[i]%(n)))
else:
ans.append(0)
# for i in ans:
# print (i,end=' ')
for i in range(len(ans)):
print(ans[i],end=' ')
arr[i+1]+=ans[i]
print()
print(2,n)
for i in range(n):
print(-1*arr[i],end=' ')
``` | instruction | 0 | 27,614 | 12 | 55,228 |
No | output | 1 | 27,614 | 12 | 55,229 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6
Submitted Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
# import time,random,resource
# sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def IF(c, t, f): return t if c else f
def YES(c): return IF(c, "YES", "NO")
def Yes(c): return IF(c, "Yes", "No")
def main():
t = 1
rr = []
for _ in range(t):
n = I()
a = LI()
if n == 1:
rr.append("1 1")
rr.append(n)
rr.append("1 1")
rr.append(0)
rr.append("1 1")
rr.append(0)
continue
rr.append("1 1")
rr.append(-a[0])
l = n - 1
rr.append(JA([1,n], " "))
r = [0]
for i in range(1,n):
c = a[i]
m = c % l
t = -m * n
r.append(t)
a[i] += t
rr.append(JA(r, " "))
rr.append(JA([2,n], " "))
r = []
for i in range(1,n):
r.append(-a[i])
rr.append(JA(r, " "))
return JA(rr, "\n")
print(main())
``` | instruction | 0 | 27,615 | 12 | 55,230 |
No | output | 1 | 27,615 | 12 | 55,231 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of n integers.
You want to make all elements of a equal to zero by doing the following operation exactly three times:
* Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).
It can be proven that it is always possible to make all elements of a equal to zero.
Input
The first line contains one integer n (1 β€ n β€ 100 000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a_1, a_2, ..., a_n (-10^9 β€ a_i β€ 10^9).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
* The first line contains two integers l, r (1 β€ l β€ r β€ n): the bounds of the selected segment.
* The second line contains r-l+1 integers b_l, b_{l+1}, ..., b_r (-10^{18} β€ b_i β€ 10^{18}): the numbers to add to a_l, a_{l+1}, β¦, a_r, respectively; b_i should be divisible by r - l + 1.
Example
Input
4
1 3 2 4
Output
1 1
-1
3 4
4 2
2 4
-3 -6 -6
Submitted Solution:
```
import math
n = int(input())
ints = list(map(int, input().split()))
print(1, n - 1)
res = [None for i in range(n - 1)]
for i in range(n - 1):
val = abs(ints[i] % n) * (n - 1)
res[i] = int(math.copysign(val, ints[i]))
ints[i] += res[i]
print(" ".join(map(str, res)))
print(n, n)
print(n - ints[n - 1])
ints[n - 1] += n - ints[n - 1]
print(1, n)
for i in range(n):
ints[i] *= -1
print(" ".join(map(str, ints)), end=" ")
``` | instruction | 0 | 27,616 | 12 | 55,232 |
No | output | 1 | 27,616 | 12 | 55,233 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
* 2 β _{i = 1}^{n} |a_i - b_i| β€ S.
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2 β€ n β€ 50).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
Output
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
Input
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
Output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3 | instruction | 0 | 27,649 | 12 | 55,298 |
Tags: bitmasks, constructive algorithms, greedy
Correct Solution:
```
import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
import functools
from operator import ixor
try:
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
except:
pass
input = lambda: sys.stdin.readline().rstrip()
for _ in range(int(input())):
q=int(input())
a=list(map(int,input().split()))
b=[0]*q
w=0
e=0
for i in range(q):
if i%2==0:
w+=a[i]
else:
e+=a[i]
if w>e:
for i in range(q):
if i%2==0:
b[i]=a[i]
else:
b[i]=1
else:
for i in range(q):
if i%2!=0:
b[i]=a[i]
else:
b[i]=1
print(*b)
``` | output | 1 | 27,649 | 12 | 55,299 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
* 2 β _{i = 1}^{n} |a_i - b_i| β€ S.
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2 β€ n β€ 50).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
Output
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
Input
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
Output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3 | instruction | 0 | 27,650 | 12 | 55,300 |
Tags: bitmasks, constructive algorithms, greedy
Correct Solution:
```
def main():
for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
cur = [0, 0]
for i in range(n):
cur[i % 2] += a[i]-1
for j in range(2):
if 2 * cur[j] > s:
continue
for i in range(n):
if i % 2 == j:
a[i] = 1
break
print(*a)
main()
``` | output | 1 | 27,650 | 12 | 55,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
* 2 β _{i = 1}^{n} |a_i - b_i| β€ S.
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2 β€ n β€ 50).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
Output
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
Input
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
Output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3 | instruction | 0 | 27,651 | 12 | 55,302 |
Tags: bitmasks, constructive algorithms, greedy
Correct Solution:
```
from sys import stdin
q = int(input())
for j in range(q):
n = int(input())
# a,b,c = map(int,input().split())
c = list(map(int,input().split()))
# s = list(map(int,input()))
s = [0,0]
for i in range(n):
s[i%2]+=c[i]
if s[0]>s[1]:
for i in range(n):
if i%2:
c[i]=1
else:
for i in range(n):
if i%2==0:
c[i]=1
print(*c)
``` | output | 1 | 27,651 | 12 | 55,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
* 2 β _{i = 1}^{n} |a_i - b_i| β€ S.
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2 β€ n β€ 50).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
Output
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
Input
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
Output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3 | instruction | 0 | 27,652 | 12 | 55,304 |
Tags: bitmasks, constructive algorithms, greedy
Correct Solution:
```
import sys
from array import array # noqa: F401
import typing as Tp # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
def output(*args):
sys.stdout.buffer.write(
('\n'.join(map(str, args)) + '\n').encode('utf-8')
)
def main():
t = int(input())
ans_a = ['NO'] * t
for ti in range(t):
n = int(input())
a = list(map(int, input().split()))
ans_a[ti] = ' '.join(map(str, [1 << (len(bin(x)) - 3) for x in a]))
output(*ans_a)
if __name__ == '__main__':
main()
``` | output | 1 | 27,652 | 12 | 55,305 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
* 2 β _{i = 1}^{n} |a_i - b_i| β€ S.
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2 β€ n β€ 50).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
Output
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
Input
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
Output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3 | instruction | 0 | 27,653 | 12 | 55,306 |
Tags: bitmasks, constructive algorithms, greedy
Correct Solution:
```
def find(b,a):
s=0
s1=sum(b)
for i in range(len(b)):
s+=abs(a[i]-b[i])
if 2*s<=s1:
return True
return False
for i in range(int(input())):
n=int(input())
a=[int(x) for x in input().split()]
b=[1]*n
p= [1]*n
for j in range(0,n,2):
b[j]=a[j]
for j in range(1,n,2):
p[j]=a[j]
if find(a,b):
print(*b)
else:
print(*p)
``` | output | 1 | 27,653 | 12 | 55,307 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
* 2 β _{i = 1}^{n} |a_i - b_i| β€ S.
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2 β€ n β€ 50).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
Output
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
Input
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
Output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3 | instruction | 0 | 27,654 | 12 | 55,308 |
Tags: bitmasks, constructive algorithms, greedy
Correct Solution:
```
from math import log2
t=int(input())
for _ in range(t):
n=int(input())
l=list(map(int,input().split()))
for i in l:
z=int(log2(i))
print(2**z,end=' ')
print()
``` | output | 1 | 27,654 | 12 | 55,309 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
* 2 β _{i = 1}^{n} |a_i - b_i| β€ S.
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2 β€ n β€ 50).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
Output
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
Input
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
Output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3 | instruction | 0 | 27,655 | 12 | 55,310 |
Tags: bitmasks, constructive algorithms, greedy
Correct Solution:
```
##########################################################
import sys
input = sys.stdin.readline
print = sys.stdout.write
def ii():return int(input())
def mii():return map(int,input().rstrip().split())
def lmii():return list(map(int,input().rstrip().split()))
##########################################################
t = ii()
for _ in range(t):
n = ii()
A = lmii()
for i in range(n):
tmp = 1
while tmp <= A[i]:
tmp*=2
print(str(tmp//2)+" ")
print('\n')
``` | output | 1 | 27,655 | 12 | 55,311 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
* 2 β _{i = 1}^{n} |a_i - b_i| β€ S.
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2 β€ n β€ 50).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
Output
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
Input
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
Output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3 | instruction | 0 | 27,656 | 12 | 55,312 |
Tags: bitmasks, constructive algorithms, greedy
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
a = [x for x in map(int,input().split())]
odd= 0;even=0
for i in range(n):
if i%2==0:
odd+=abs(a[i]-1)
else:
even +=abs(a[i]-1)
if odd>even:
for i in range(n):
if i%2==1:
print(1 ,end =' ')
else:
print(a[i],end= ' ')
else:
for i in range(n):
if i%2==1:
print(a[i],end=' ')
else:
print(1 ,end=' ')
#print(odd,even)
print()
``` | output | 1 | 27,656 | 12 | 55,313 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
* 2 β _{i = 1}^{n} |a_i - b_i| β€ S.
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2 β€ n β€ 50).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
Output
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
Input
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
Output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3
Submitted Solution:
```
"""
pppppppppppppppppppp
ppppp ppppppppppppppppppp
ppppppp ppppppppppppppppppppp
pppppppp pppppppppppppppppppppp
pppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppp
pppppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppp
pppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppp
pppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppp
pppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp
pppppppppppppppppppppppppppppppp
pppppppppppppppppppppp pppppppp
ppppppppppppppppppppp ppppppp
ppppppppppppppppppp ppppp
pppppppppppppppppppp
"""
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
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
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
from copy import deepcopy
# sys.setrecursionlimit(pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
# mod = 10 ** 9 + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var, end="\n"): sys.stdout.write(str(var)+end)
def outa(*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)]
def check():
return 2 * sum([abs(answer[i] - arr[i]) for i in range(n)]) <= sum(arr)
for _ in range(int(data())):
n = int(data())
arr = l()
answer = [1] * n
for i in range(0, n, 2):
answer[i] = arr[i]
if check():
outa(*answer)
continue
answer = [1] * n
for i in range(1, n, 2):
answer[i] = arr[i]
outa(*answer)
``` | instruction | 0 | 27,657 | 12 | 55,314 |
Yes | output | 1 | 27,657 | 12 | 55,315 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
* 2 β _{i = 1}^{n} |a_i - b_i| β€ S.
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2 β€ n β€ 50).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
Output
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
Input
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
Output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3
Submitted Solution:
```
from collections import defaultdict,deque
import sys
import bisect
input=sys.stdin.readline
mod=1000000007
def calc(num):
ret=0
for i in range(n):
ret+=abs(a[i]-num[i])
return(ret)
t=int(input())
for ii in range(t):
n=int(input())
a=[int(i) for i in input().split()]
ans,ans1=[],[]
for i in range(n):
if i&1:
ans.append(1)
ans1.append(a[i])
else:
ans.append(a[i])
ans1.append(1)
one,two=calc(ans),calc(ans1)
#print(ans,ans1)
if sum(a)-2*one>=0:
print(*ans)
else:
print(*ans1)
``` | instruction | 0 | 27,658 | 12 | 55,316 |
Yes | output | 1 | 27,658 | 12 | 55,317 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
* 2 β _{i = 1}^{n} |a_i - b_i| β€ S.
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2 β€ n β€ 50).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
Output
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
Input
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
Output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3
Submitted Solution:
```
import math
for _ in range(int(input())):
n = int(input())
a = [*map(int, input().split())]
o, e = 0, 0
for i in range(n):
if i % 2:
o += a[i]
else:
e += a[i]
if o > e:
for i in range(n):
if i % 2 == 0:
a[i] = 1
else:
for i in range(n):
if i % 2:
a[i] = 1
print(*a, sep = " ")
``` | instruction | 0 | 27,659 | 12 | 55,318 |
Yes | output | 1 | 27,659 | 12 | 55,319 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array [a_1, a_2, ..., a_n] such that 1 β€ a_i β€ 10^9. Let S be the sum of all elements of the array a.
Let's call an array b of n integers beautiful if:
* 1 β€ b_i β€ 10^9 for each i from 1 to n;
* for every pair of adjacent integers from the array (b_i, b_{i + 1}), either b_i divides b_{i + 1}, or b_{i + 1} divides b_i (or both);
* 2 β _{i = 1}^{n} |a_i - b_i| β€ S.
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2 β€ n β€ 50).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
Output
For each test case, print the beautiful array b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
Input
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
Output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3
Submitted Solution:
```
tests = int(input())
for t in range(tests):
n = int(input())
ls = list(map(int, input().split()))
# ls_1 = sum(ls)//len(ls)
ls_others = [2**i for i in range(0,30)]
ans = []
for item in ls:
for sel_idx in range(len(ls_others)):
if sel_idx < (len(ls_others)-1):
if abs(item-ls_others[sel_idx]) <= abs(item-ls_others[sel_idx+1]):
ans.append(ls_others[sel_idx])
break
else:
ans.append(ls_others[sel_idx])
print(' ' .join([str(item) for item in ans]))
# print(sum(ls))
# print(sum([abs(ans[i]-ls[i]) for i in range(len(ans))]))
``` | instruction | 0 | 27,660 | 12 | 55,320 |
Yes | output | 1 | 27,660 | 12 | 55,321 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.