message stringlengths 2 433k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn.
Nickolas adores permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfect permutation is such permutation p that for any i (1 β€ i β€ n) (n is the permutation size) the following equations hold ppi = i and pi β i. Nickolas asks you to print any perfect permutation of size n for the given n.
Input
A single line contains a single integer n (1 β€ n β€ 100) β the permutation size.
Output
If a perfect permutation of size n doesn't exist, print a single integer -1. Otherwise print n distinct integers from 1 to n, p1, p2, ..., pn β permutation p, that is perfect. Separate printed numbers by whitespaces.
Examples
Input
1
Output
-1
Input
2
Output
2 1
Input
4
Output
2 1 4 3
Submitted Solution:
```
n=int(input())
c=[]
if(n%2==0):
for i in range(2,n+1,2):
c.append(i)
c.append(i-1)
a=" ".join(str(i) for i in c)
print(a)
else:
print(-1)
``` | instruction | 0 | 101,480 | 12 | 202,960 |
Yes | output | 1 | 101,480 | 12 | 202,961 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn.
Nickolas adores permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfect permutation is such permutation p that for any i (1 β€ i β€ n) (n is the permutation size) the following equations hold ppi = i and pi β i. Nickolas asks you to print any perfect permutation of size n for the given n.
Input
A single line contains a single integer n (1 β€ n β€ 100) β the permutation size.
Output
If a perfect permutation of size n doesn't exist, print a single integer -1. Otherwise print n distinct integers from 1 to n, p1, p2, ..., pn β permutation p, that is perfect. Separate printed numbers by whitespaces.
Examples
Input
1
Output
-1
Input
2
Output
2 1
Input
4
Output
2 1 4 3
Submitted Solution:
```
n= int(input())
a=[]
for i in range(n):
a.append(i+1)
if n==1:
print("-1")
else:
k=a.pop()
a.insert(0,k)
print(a)
``` | instruction | 0 | 101,481 | 12 | 202,962 |
No | output | 1 | 101,481 | 12 | 202,963 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn.
Nickolas adores permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfect permutation is such permutation p that for any i (1 β€ i β€ n) (n is the permutation size) the following equations hold ppi = i and pi β i. Nickolas asks you to print any perfect permutation of size n for the given n.
Input
A single line contains a single integer n (1 β€ n β€ 100) β the permutation size.
Output
If a perfect permutation of size n doesn't exist, print a single integer -1. Otherwise print n distinct integers from 1 to n, p1, p2, ..., pn β permutation p, that is perfect. Separate printed numbers by whitespaces.
Examples
Input
1
Output
-1
Input
2
Output
2 1
Input
4
Output
2 1 4 3
Submitted Solution:
```
# import os
n = int(input())
if n % 2 == 0:
a = [i for i in range(n//2, 0,-1)]
b = [i for i in range(n, n//2, -1)]
print(' '.join(map(str, a+b)))
else:
print(-1)
# 03/01 - 1
# 04/01 - 21
# 05/01 - 27
# 06/01 - 3
``` | instruction | 0 | 101,482 | 12 | 202,964 |
No | output | 1 | 101,482 | 12 | 202,965 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn.
Nickolas adores permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfect permutation is such permutation p that for any i (1 β€ i β€ n) (n is the permutation size) the following equations hold ppi = i and pi β i. Nickolas asks you to print any perfect permutation of size n for the given n.
Input
A single line contains a single integer n (1 β€ n β€ 100) β the permutation size.
Output
If a perfect permutation of size n doesn't exist, print a single integer -1. Otherwise print n distinct integers from 1 to n, p1, p2, ..., pn β permutation p, that is perfect. Separate printed numbers by whitespaces.
Examples
Input
1
Output
-1
Input
2
Output
2 1
Input
4
Output
2 1 4 3
Submitted Solution:
```
n = int(input())
result = [i for i in range(1,n+1)]
if n ==1:
print(-1)
else:
for i in range(n-1):
if (i+1)%2 != 0:
result[i],result[i+1] = result[i+1],result[i]
for i in result:
print(i,end="")
``` | instruction | 0 | 101,483 | 12 | 202,966 |
No | output | 1 | 101,483 | 12 | 202,967 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn.
Nickolas adores permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfect permutation is such permutation p that for any i (1 β€ i β€ n) (n is the permutation size) the following equations hold ppi = i and pi β i. Nickolas asks you to print any perfect permutation of size n for the given n.
Input
A single line contains a single integer n (1 β€ n β€ 100) β the permutation size.
Output
If a perfect permutation of size n doesn't exist, print a single integer -1. Otherwise print n distinct integers from 1 to n, p1, p2, ..., pn β permutation p, that is perfect. Separate printed numbers by whitespaces.
Examples
Input
1
Output
-1
Input
2
Output
2 1
Input
4
Output
2 1 4 3
Submitted Solution:
```
n = 4
if n % 2 != 0:
print(-1)
else:
m = list(range(1, n + 1))
for i in m[::2]:
print(m[i], m[i - 1], end=" ")
``` | instruction | 0 | 101,484 | 12 | 202,968 |
No | output | 1 | 101,484 | 12 | 202,969 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1].
This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of elements in permutation p.
The second line contains n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of permutation p.
Output
If there is no permutation q such that q2 = p print the number "-1".
If the answer exists print it. The only line should contain n different integers qi (1 β€ qi β€ n) β the elements of the permutation q. If there are several solutions print any of them.
Examples
Input
4
2 1 4 3
Output
3 4 2 1
Input
4
2 1 3 4
Output
-1
Input
5
2 3 4 5 1
Output
4 5 1 2 3 | instruction | 0 | 101,637 | 12 | 203,274 |
Tags: combinatorics, constructive algorithms, dfs and similar, graphs, math
Correct Solution:
```
import math
import sys
input = sys.stdin.readline
n = int(input())
a = [int(_) - 1 for _ in input().split()]
vis = [False] * n
cycles = [[] for _ in range(n + 1)]
for i in range(n):
if vis[i]: continue
cur = i
cycle = []
while not vis[cur]:
vis[cur] = True
cycle.append(cur)
cur = a[cur]
cycles[len(cycle)].append(cycle)
p = [0] * n
for i in range(n + 1):
if i % 2 == 1:
for j in cycles[i]:
for k in range(i):
p[j[k]] = j[(k + (i + 1) // 2) % i]
else:
if len(cycles[i]) % 2 == 1:
print(-1)
exit(0)
for j in range(0, len(cycles[i]), 2):
for k in range(i):
p[cycles[i][j][k]] = cycles[i][j + 1][k]
p[cycles[i][j + 1][k]] = cycles[i][j][(k + 1) % i]
print(' '.join(map(lambda i : str(i + 1), p)))
``` | output | 1 | 101,637 | 12 | 203,275 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1].
This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of elements in permutation p.
The second line contains n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of permutation p.
Output
If there is no permutation q such that q2 = p print the number "-1".
If the answer exists print it. The only line should contain n different integers qi (1 β€ qi β€ n) β the elements of the permutation q. If there are several solutions print any of them.
Examples
Input
4
2 1 4 3
Output
3 4 2 1
Input
4
2 1 3 4
Output
-1
Input
5
2 3 4 5 1
Output
4 5 1 2 3 | instruction | 0 | 101,638 | 12 | 203,276 |
Tags: combinatorics, constructive algorithms, dfs and similar, graphs, math
Correct Solution:
```
import sys
import bisect
from bisect import bisect_left as lb
input_=lambda: sys.stdin.readline().strip("\r\n")
from math import log
from math import gcd
from math import atan2,acos
from random import randint
sa=lambda :input_()
sb=lambda:int(input_())
sc=lambda:input_().split()
sd=lambda:list(map(int,input_().split()))
se=lambda:float(input_())
sf=lambda:list(input_())
flsh=lambda: sys.stdout.flush()
#sys.setrecursionlimit(10**6)
mod=10**9+7
gp=[]
cost=[]
dp=[]
mx=[]
ans1=[]
ans2=[]
special=[]
specnode=[]
a=0
kthpar=[]
def dfs(root,par):
if par!=-1:
dp[root]=dp[par]+1
for i in range(1,20):
if kthpar[root][i-1]!=-1:
kthpar[root][i]=kthpar[kthpar[root][i-1]][i-1]
for child in gp[root]:
if child==par:continue
kthpar[child][0]=root
dfs(child,root)
def hnbhai():
n=sb()
a=[0]+sd()
ans=[-1]*(n+1)
d={}
visited=[0]*(n+1)
for i in range(1,n+1):
if visited[i]==0:
g=[]
abe=i
while not visited[abe]:
visited[abe]=1
g.append(abe)
abe=a[abe]
if len(g)%2:
mid=(len(g)+1)//2
for i in range(len(g)):
ans[g[i]]=g[(i+mid)%len(g)]
else:
if d.get(len(g)):
temp=d[len(g)]
for i in range(len(g)):
ans[g[i]]=temp[(i+1)%len(g)]
ans[temp[i]]=g[i]
del d[len(g)]
else:
d[len(g)]=g
if len(d):
print(-1)
return
print(*ans[1:])
for _ in range(1):
hnbhai()
``` | output | 1 | 101,638 | 12 | 203,277 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1].
This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of elements in permutation p.
The second line contains n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of permutation p.
Output
If there is no permutation q such that q2 = p print the number "-1".
If the answer exists print it. The only line should contain n different integers qi (1 β€ qi β€ n) β the elements of the permutation q. If there are several solutions print any of them.
Examples
Input
4
2 1 4 3
Output
3 4 2 1
Input
4
2 1 3 4
Output
-1
Input
5
2 3 4 5 1
Output
4 5 1 2 3 | instruction | 0 | 101,639 | 12 | 203,278 |
Tags: combinatorics, constructive algorithms, dfs and similar, graphs, math
Correct Solution:
```
#### IMPORTANT LIBRARY ####
############################
### DO NOT USE import random --> 250ms to load the library
############################
### In case of extra libraries: https://github.com/cheran-senthil/PyRival
######################
####### IMPORT #######
######################
from functools import cmp_to_key
from collections import deque, Counter
from heapq import heappush, heappop
from math import log, ceil
######################
#### STANDARD I/O ####
######################
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def print(*args, **kwargs):
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
def inp():
return sys.stdin.readline().rstrip("\r\n") # for fast input
def ii():
return int(inp())
def si():
return str(inp())
def li(lag = 0):
l = list(map(int, inp().split()))
if lag != 0:
for i in range(len(l)):
l[i] += lag
return l
def mi(lag = 0):
matrix = list()
for i in range(n):
matrix.append(li(lag))
return matrix
def lsi(): #string list
return list(map(str, inp().split()))
def print_list(lista, space = " "):
print(space.join(map(str, lista)))
######################
### BISECT METHODS ###
######################
def bisect_left(a, x):
"""i tale che a[i] >= x e a[i-1] < x"""
left = 0
right = len(a)
while left < right:
mid = (left+right)//2
if a[mid] < x:
left = mid+1
else:
right = mid
return left
def bisect_right(a, x):
"""i tale che a[i] > x e a[i-1] <= x"""
left = 0
right = len(a)
while left < right:
mid = (left+right)//2
if a[mid] > x:
right = mid
else:
left = mid+1
return left
def bisect_elements(a, x):
"""elementi pari a x nell'Γ‘rray sortato"""
return bisect_right(a, x) - bisect_left(a, x)
######################
### MOD OPERATION ####
######################
MOD = 10**9 + 7
maxN = 5
FACT = [0] * maxN
INV_FACT = [0] * maxN
def add(x, y):
return (x+y) % MOD
def multiply(x, y):
return (x*y) % MOD
def power(x, y):
if y == 0:
return 1
elif y % 2:
return multiply(x, power(x, y-1))
else:
a = power(x, y//2)
return multiply(a, a)
def inverse(x):
return power(x, MOD-2)
def divide(x, y):
return multiply(x, inverse(y))
def allFactorials():
FACT[0] = 1
for i in range(1, maxN):
FACT[i] = multiply(i, FACT[i-1])
def inverseFactorials():
n = len(INV_FACT)
INV_FACT[n-1] = inverse(FACT[n-1])
for i in range(n-2, -1, -1):
INV_FACT[i] = multiply(INV_FACT[i+1], i+1)
def coeffBinom(n, k):
if n < k:
return 0
return multiply(FACT[n], multiply(INV_FACT[k], INV_FACT[n-k]))
######################
#### GRAPH ALGOS #####
######################
# ZERO BASED GRAPH
def create_graph(n, m, undirected = 1, unweighted = 1):
graph = [[] for i in range(n)]
if unweighted:
for i in range(m):
[x, y] = li(lag = -1)
graph[x].append(y)
if undirected:
graph[y].append(x)
else:
for i in range(m):
[x, y, w] = li(lag = -1)
w += 1
graph[x].append([y,w])
if undirected:
graph[y].append([x,w])
return graph
def create_tree(n, unweighted = 1):
children = [[] for i in range(n)]
if unweighted:
for i in range(n-1):
[x, y] = li(lag = -1)
children[x].append(y)
children[y].append(x)
else:
for i in range(n-1):
[x, y, w] = li(lag = -1)
w += 1
children[x].append([y, w])
children[y].append([x, w])
return children
def dist(tree, n, A, B = -1):
s = [[A, 0]]
massimo, massimo_nodo = 0, 0
distanza = -1
v = [-1] * n
while s:
el, dis = s.pop()
if dis > massimo:
massimo = dis
massimo_nodo = el
if el == B:
distanza = dis
for child in tree[el]:
if v[child] == -1:
v[child] = 1
s.append([child, dis+1])
return massimo, massimo_nodo, distanza
def diameter(tree):
_, foglia, _ = dist(tree, n, 0)
diam, _, _ = dist(tree, n, foglia)
return diam
def dfs(graph, n, A):
v = [-1] * n
s = [[A, 0]]
v[A] = 0
while s:
el, dis = s.pop()
for child in graph[el]:
if v[child] == -1:
v[child] = dis + 1
s.append([child, dis + 1])
return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges
def bfs(graph, n, A):
v = [-1] * n
s = deque()
s.append([A, 0])
v[A] = 0
while s:
el, dis = s.popleft()
for child in graph[el]:
if v[child] == -1:
v[child] = dis + 1
s.append([child, dis + 1])
return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges
#FROM A GIVEN ROOT, RECOVER THE STRUCTURE
def parents_children_root_unrooted_tree(tree, n, root = 0):
q = deque()
visited = [0] * n
parent = [-1] * n
children = [[] for i in range(n)]
q.append(root)
while q:
all_done = 1
visited[q[0]] = 1
for child in tree[q[0]]:
if not visited[child]:
all_done = 0
q.appendleft(child)
if all_done:
for child in tree[q[0]]:
if parent[child] == -1:
parent[q[0]] = child
children[child].append(q[0])
q.popleft()
return parent, children
# CALCULATING LONGEST PATH FOR ALL THE NODES
def all_longest_path_passing_from_node(parent, children, n):
q = deque()
visited = [len(children[i]) for i in range(n)]
downwards = [[0,0] for i in range(n)]
upward = [1] * n
longest_path = [1] * n
for i in range(n):
if not visited[i]:
q.append(i)
downwards[i] = [1,0]
while q:
node = q.popleft()
if parent[node] != -1:
visited[parent[node]] -= 1
if not visited[parent[node]]:
q.append(parent[node])
else:
root = node
for child in children[node]:
downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse = True)[0:2]
s = [node]
while s:
node = s.pop()
if parent[node] != -1:
if downwards[parent[node]][0] == downwards[node][0] + 1:
upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1])
else:
upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0])
longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1
for child in children[node]:
s.append(child)
return longest_path
### TBD SUCCESSOR GRAPH 7.5
### TBD TREE QUERIES 10.2 da 2 a 4
### TBD ADVANCED TREE 10.3
### TBD GRAPHS AND MATRICES 11.3.3 e 11.4.3 e 11.5.3 (ON GAMES)
######################
## END OF LIBRARIES ##
######################
def f(n, a):
visited = [0] * n
cicli = []
for i in range(n):
ciclo = []
j = i
if visited[j] == 0:
visited[j] = 1
while a[j] != i:
ciclo.append(j)
j = a[j]
visited[j] = 1
ciclo.append(j)
cicli.append(ciclo)
q = [0] * n
tot = {}
for c in cicli:
if len(c) % 2 == 1:
i = len(c) // 2
j = len(c) - 1
while q[c[j]] == 0:
q[c[j]] = c[i] + 1
i += 1
j += 1
i %= len(c)
j %= len(c)
else:
k = len(c)
if k not in tot:
tot[k] = []
tot[k].append(c)
for k in tot:
if len(tot[k]) % 2 == 1:
return [-1]
else:
for i in range(0, len(tot[k])//2):
p1 = tot[k][2*i]
p2 = tot[k][2*i+1]
for i in range(len(p1)):
q[p1[i]] = p2[i] + 1
q[p2[i]] = p1[(i+1) % len(p1)] + 1
return q
n = ii()
a = li(-1)
print_list(f(n, a))
``` | output | 1 | 101,639 | 12 | 203,279 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1].
This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of elements in permutation p.
The second line contains n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of permutation p.
Output
If there is no permutation q such that q2 = p print the number "-1".
If the answer exists print it. The only line should contain n different integers qi (1 β€ qi β€ n) β the elements of the permutation q. If there are several solutions print any of them.
Examples
Input
4
2 1 4 3
Output
3 4 2 1
Input
4
2 1 3 4
Output
-1
Input
5
2 3 4 5 1
Output
4 5 1 2 3 | instruction | 0 | 101,640 | 12 | 203,280 |
Tags: combinatorics, constructive algorithms, dfs and similar, graphs, math
Correct Solution:
```
import sys
#import random
from bisect import bisect_right as rb
from collections import deque
#sys.setrecursionlimit(10**8)
from queue import PriorityQueue
from math import *
input_ = lambda: sys.stdin.readline().strip("\r\n")
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab,bc,cd : ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x : print(x)
prinT = lambda x : print(x)
f = lambda : sys.stdout.flush()
mod = 10**9 + 7
n = ii()
a = [0] + il()
def dfs (i) :
vis[i] = 1
g.append(i)
if (vis[a[i]] == 0) :
dfs(a[i])
g = []
d = {}
vis = [0 for i in range (n+2)]
ans = [0 for i in range (n+1)]
for i in range (1,n+1) :
if (vis[i] == 0) :
i1 = i
while True :
vis[i1] = 1
g.append(i1)
if (vis[a[i1]] == 0) :
i1 = a[i1]
else :
break
l = len(g)
if (l%2) :
x = (l+1)//2
for j in range (l) :
ans[g[j]] = g[(x+j)%l]
elif (d.get(l)) :
v = d[l]
for j in range (l) :
ans[g[j]] = v[(j+1)%l]
ans[v[j]] = g[j]
del d[l]
else :
d[l] = g
g = []
for i in d :
print(-1)
exit(0)
print(*ans[1:])
``` | output | 1 | 101,640 | 12 | 203,281 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1].
This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of elements in permutation p.
The second line contains n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of permutation p.
Output
If there is no permutation q such that q2 = p print the number "-1".
If the answer exists print it. The only line should contain n different integers qi (1 β€ qi β€ n) β the elements of the permutation q. If there are several solutions print any of them.
Examples
Input
4
2 1 4 3
Output
3 4 2 1
Input
4
2 1 3 4
Output
-1
Input
5
2 3 4 5 1
Output
4 5 1 2 3
Submitted Solution:
```
import sys
#import random
from bisect import bisect_right as rb
from collections import deque
#sys.setrecursionlimit(10**6)
from queue import PriorityQueue
from math import *
input_ = lambda: sys.stdin.readline().strip("\r\n")
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab,bc,cd : ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x : print(x)
prinT = lambda x : print(x)
f = lambda : sys.stdout.flush()
mod = 10**9 + 7
n = ii()
a = [0] + il()
def dfs (i) :
vis[i] = 1
g.append(i)
if (vis[a[i]] == 0) :
dfs(a[i])
g = []
d = {}
vis = [0 for i in range (n+2)]
ans = [0 for i in range (n+1)]
if (n != 10**4) :
for i in range (1,n+1) :
if (vis[i] == 0) :
dfs(i)
l = len(g)
if (l%2) :
x = (l+1)//2
for j in range (l) :
ans[g[j]] = g[(x+j)%l]
elif (d.get(l)) :
v = d[l]
for j in range (l) :
ans[g[j]] = v[(j+1)%l]
ans[v[j]] = g[j]
del d[l]
else :
d[l] = g
g = []
for i in d :
print(-1)
exit(0)
print(*ans[1:])
``` | instruction | 0 | 101,641 | 12 | 203,282 |
No | output | 1 | 101,641 | 12 | 203,283 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1].
This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of elements in permutation p.
The second line contains n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of permutation p.
Output
If there is no permutation q such that q2 = p print the number "-1".
If the answer exists print it. The only line should contain n different integers qi (1 β€ qi β€ n) β the elements of the permutation q. If there are several solutions print any of them.
Examples
Input
4
2 1 4 3
Output
3 4 2 1
Input
4
2 1 3 4
Output
-1
Input
5
2 3 4 5 1
Output
4 5 1 2 3
Submitted Solution:
```
n = int(input())
c = input()
c = c.split(" ")
c = [int(p) - 1 for p in c]
visited = [0 for p in range(0,n,1)]
cycle_st = []
for i in range(0,n,1):
if visited[i] != 1:
cycle = []
cycle.append([i,c[i]])
visited[i] = 1
d = c[i]
while visited[d] != 1:
cycle.append([d,c[d]])
visited[d] = 1
d = c[d]
cycle_st.append(cycle)
used = [ 1 for p in range(0,len(cycle_st),1)]
posssible = True
have_cycle = True
i = 0
j = 0
for i in range(len(cycle_st)):
if used[i]:
have_cycle = False
if(len(cycle_st[i]) % 2 == 1):
for j in range(len(cycle_st[i])):
if cycle_st[i][j][0] == cycle_st[i][j - 1][1]:
q = i-i//2
else:
q = -1
visited[cycle_st[i][j][0]] = cycle_st[i][j+q][1]
else:
for j in range(i + 1,len(cycle_st),1):
if used[j] and len(cycle_st[i]) == len(cycle_st[j]):
have_cycle = True
for k in range(len(cycle_st[i])):
visited[cycle_st[i][k][0]] = cycle_st[j][k - 1][0]
visited[cycle_st[j][k - 1][0]] = cycle_st[i][k - 1][0]
used[j] = 0
break
if not have_cycle:
posssible = False
break
if not posssible:
print(-1)
else:
for p in visited:
print(str(p + 1))
``` | instruction | 0 | 101,642 | 12 | 203,284 |
No | output | 1 | 101,642 | 12 | 203,285 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1].
This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of elements in permutation p.
The second line contains n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of permutation p.
Output
If there is no permutation q such that q2 = p print the number "-1".
If the answer exists print it. The only line should contain n different integers qi (1 β€ qi β€ n) β the elements of the permutation q. If there are several solutions print any of them.
Examples
Input
4
2 1 4 3
Output
3 4 2 1
Input
4
2 1 3 4
Output
-1
Input
5
2 3 4 5 1
Output
4 5 1 2 3
Submitted Solution:
```
n = int(input())
if n == 5:
print('4 5 1 2 3')
else:
c = input()
c = c.split(" ")
c = [int(p) - 1 for p in c]
visited = [0 for p in range(0,n,1)]
cycle_st = []
for i in range(0,n,1):
if visited[i] != 1:
cycle = []
cycle.append([i,c[i]])
visited[i] = 1
d = c[i]
while visited[d] != 1:
cycle.append([d,c[d]])
visited[d] = 1
d = c[d]
cycle_st.append(cycle)
used = [ 1 for p in range(0,len(cycle_st),1)]
posssible = True
have_cycle =True
i = 0
j = 0
for i in range(len(cycle_st)):
if used[i]:
have_cycle = False
if(len(cycle_st[i]) % 2 == 1):
for j in range(len(cycle_st[i])):
visited[cycle_st[i][j][0]] = cycle_st[i][j-1][1]
else:
for j in range(i+1,len(cycle_st),1):
if used[j] and len(cycle_st[i]) == len(cycle_st[j]):
have_cycle = True
for k in range(len(cycle_st[i])):
visited[cycle_st[i][k][0]] = cycle_st[j][k-1][0]
visited[cycle_st[j][k-1][0]] = cycle_st[i][k-1][0]
used[j] = 0
break
if not have_cycle:
posssible = False
break
if not posssible:
print(-1)
else:
for p in visited:
print(str(p+1))
``` | instruction | 0 | 101,643 | 12 | 203,286 |
No | output | 1 | 101,643 | 12 | 203,287 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1].
This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
Input
The first line contains integer n (1 β€ n β€ 106) β the number of elements in permutation p.
The second line contains n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of permutation p.
Output
If there is no permutation q such that q2 = p print the number "-1".
If the answer exists print it. The only line should contain n different integers qi (1 β€ qi β€ n) β the elements of the permutation q. If there are several solutions print any of them.
Examples
Input
4
2 1 4 3
Output
3 4 2 1
Input
4
2 1 3 4
Output
-1
Input
5
2 3 4 5 1
Output
4 5 1 2 3
Submitted Solution:
```
n = int(input())
c = input()
c = c.split(" ")
c = [int(p) - 1 for p in c]
visited = [0 for p in range(0,n,1)]
cycle_st = []
for i in range(0,n,1):
if visited[i] != 1:
cycle = []
cycle.append([i,c[i]])
visited[i] = 1
d = c[i]
while visited[d] != 1:
cycle.append([d,c[d]])
visited[d] = 1
d = c[d]
cycle_st.append(cycle)
used = [ 1 for p in range(0,len(cycle_st),1)]
posssible = True
have_cycle =True
i = 0
j = 0
for i in range(len(cycle_st)):
if used[i]:
have_cycle = False
if(len(cycle_st[i]) % 2 == 1):
for j in range(len(cycle_st[i])):
visited[cycle_st[i][j][0]] = cycle_st[i][(j+2)%len(cycle_st[i])][1]
else:
for j in range(i+1,len(cycle_st),1):
if used[j] and len(cycle_st[i]) == len(cycle_st[j]):
have_cycle = True
for k in range(len(cycle_st[i])):
visited[cycle_st[i][k][0]] = cycle_st[j][k-1][0]
visited[cycle_st[j][k-1][0]] = cycle_st[i][k-1][0]
used[j] = 0
break
if not have_cycle:
posssible = False
break
if not posssible:
print(-1)
else:
for p in visited:
print(str(p+1))
``` | instruction | 0 | 101,644 | 12 | 203,288 |
No | output | 1 | 101,644 | 12 | 203,289 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let's define the deviation of a permutation p as <image>.
Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.
Let's denote id k (0 β€ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:
* k = 0: shift p1, p2, ... pn,
* k = 1: shift pn, p1, ... pn - 1,
* ...,
* k = n - 1: shift p2, p3, ... pn, p1.
Input
First line contains single integer n (2 β€ n β€ 106) β the length of the permutation.
The second line contains n space-separated integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of the permutation. It is guaranteed that all elements are distinct.
Output
Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.
Examples
Input
3
1 2 3
Output
0 0
Input
3
2 3 1
Output
0 1
Input
3
3 2 1
Output
2 1
Note
In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.
In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift.
In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts. | instruction | 0 | 101,704 | 12 | 203,408 |
Tags: data structures, implementation, math
Correct Solution:
```
from sys import stdin
def main():
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
inf = [0] * (n + 1)
curr = 0
d = 0
for i in range(n):
curr += abs(i + 1 - a[i])
if a[i] > i + 1:
d += 1
inf[a[i] - i - 1] += 1
elif a[i] <= i + 1:
d -= 1
if a[i] == i + 1:
inf[0] += 1
else:
inf[a[i] + n - i - 1] += 1
best = curr
num = 0
for i in range(n):
curr -= d
curr -= 1
curr = curr - abs(a[n - i - 1] - n) + abs(a[n - i - 1] - 1)
d += 2
d -= inf[i + 1] * 2
if curr < best:
best = curr
num = i + 1
print(best, num)
main()
``` | output | 1 | 101,704 | 12 | 203,409 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let's define the deviation of a permutation p as <image>.
Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.
Let's denote id k (0 β€ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:
* k = 0: shift p1, p2, ... pn,
* k = 1: shift pn, p1, ... pn - 1,
* ...,
* k = n - 1: shift p2, p3, ... pn, p1.
Input
First line contains single integer n (2 β€ n β€ 106) β the length of the permutation.
The second line contains n space-separated integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of the permutation. It is guaranteed that all elements are distinct.
Output
Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.
Examples
Input
3
1 2 3
Output
0 0
Input
3
2 3 1
Output
0 1
Input
3
3 2 1
Output
2 1
Note
In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.
In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift.
In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts. | instruction | 0 | 101,705 | 12 | 203,410 |
Tags: data structures, implementation, math
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
t = [0] * 2 * n
s = 0
for i in range(n):
d = a[i] - i - 1
s += abs(d)
if d > 0: t[d] += 1
p = sum(t)
r = (s, 0)
for i in range(1, n):
d = a[n - i] - 1
s += d - p << 1
t[d + i] += d > 0
p += (d > 0) - t[i]
if s < r[0]: r = (s, i)
print(*r)
``` | output | 1 | 101,705 | 12 | 203,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let's define the deviation of a permutation p as <image>.
Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.
Let's denote id k (0 β€ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:
* k = 0: shift p1, p2, ... pn,
* k = 1: shift pn, p1, ... pn - 1,
* ...,
* k = n - 1: shift p2, p3, ... pn, p1.
Input
First line contains single integer n (2 β€ n β€ 106) β the length of the permutation.
The second line contains n space-separated integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of the permutation. It is guaranteed that all elements are distinct.
Output
Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.
Examples
Input
3
1 2 3
Output
0 0
Input
3
2 3 1
Output
0 1
Input
3
3 2 1
Output
2 1
Note
In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.
In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift.
In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts. | instruction | 0 | 101,706 | 12 | 203,412 |
Tags: data structures, implementation, math
Correct Solution:
```
n = int(input())
data = input().split()
#print(str(n) + " " + str(data))
data = list(map(lambda x: int(x), data))
res = 0
ires = 0
neg = 0
when = [0] * n
for i in range(n):
data[i] = i + 1 - data[i]
res += abs(data[i])
if data[i] <= 0:
neg += 1
a = -data[i]
if a < 0:
a = a + n
when[a] += 1
#print(when)
ares = res
#print(str(res) + " " + str(ires) + " " + str(neg))
for i in range(n):
neg -= when[i]
ares -= neg
ares += (n - neg)
x = data[n - i - 1] + i + 1
ares -= x
ares += n - x
#print(str(res) + " " + str(ires) + " " + str(ares) + " " + str(i) + " " + str(neg))
neg += 1
if ares < res:
res = ares
ires = i + 1
print(str(res) + " " + str(ires))
``` | output | 1 | 101,706 | 12 | 203,413 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let's define the deviation of a permutation p as <image>.
Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.
Let's denote id k (0 β€ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:
* k = 0: shift p1, p2, ... pn,
* k = 1: shift pn, p1, ... pn - 1,
* ...,
* k = n - 1: shift p2, p3, ... pn, p1.
Input
First line contains single integer n (2 β€ n β€ 106) β the length of the permutation.
The second line contains n space-separated integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of the permutation. It is guaranteed that all elements are distinct.
Output
Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.
Examples
Input
3
1 2 3
Output
0 0
Input
3
2 3 1
Output
0 1
Input
3
3 2 1
Output
2 1
Note
In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.
In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift.
In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts. | instruction | 0 | 101,707 | 12 | 203,414 |
Tags: data structures, implementation, math
Correct Solution:
```
def main():
n=int(input())
A=list(map(int,input().strip().split(' ')))
def brutal(A):
n=len(A)
for i in range(n):
temp=0
pos=0
neg=0
for j in range(n):
temp+=abs(A[j]-(j+i)%n)
if A[j]-(j+i)%n>0:
pos+=1
else:
neg+=1
print(temp,i,pos,neg,'ans,shift,+ve,-ve')
for i in range(len(A)):
A[i]-=1
ans=0
pos=0
neg=0
change=[0 for i in range(len(A))]
for i in range(len(A)):
ans+=abs(A[i]-i)
if A[i]-i>0:
pos+=1
else:
neg+=1
if A[i]-i>0:
change[i]=A[i]-i
elif A[i]==i:
change[i]=0
else:
if A[i]!=0:
change[i]=A[i]+n-i
else:
change[i]=0
MIN=ans
index=0
#print(ans)
collect=[[] for i in range(n)]
for x in range(len(change)):
collect[change[x]]+=[x]
#print(collect)
#print(ans,pos,neg)
for s in range(1,n):
ans-=abs(A[n-s]-n+1)
ans+=abs(A[n-s]-0)
neg-=1
ans-=pos
ans+=neg
if A[n-s]>0:
pos+=1
else:
neg+=1
pos-=len(collect[s])
neg+=len(collect[s])
#print(ans,pos,neg)
if ans<MIN:
MIN=ans
index=s
print(MIN,index)
#brutal(A)
main()
``` | output | 1 | 101,707 | 12 | 203,415 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let's define the deviation of a permutation p as <image>.
Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.
Let's denote id k (0 β€ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:
* k = 0: shift p1, p2, ... pn,
* k = 1: shift pn, p1, ... pn - 1,
* ...,
* k = n - 1: shift p2, p3, ... pn, p1.
Input
First line contains single integer n (2 β€ n β€ 106) β the length of the permutation.
The second line contains n space-separated integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of the permutation. It is guaranteed that all elements are distinct.
Output
Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.
Examples
Input
3
1 2 3
Output
0 0
Input
3
2 3 1
Output
0 1
Input
3
3 2 1
Output
2 1
Note
In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.
In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift.
In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts. | instruction | 0 | 101,708 | 12 | 203,416 |
Tags: data structures, implementation, math
Correct Solution:
```
n = int(input())
p = [int(i) - 1 for i in input().split()]
chd = [0] * (n + 2)
dev = 0
dd = 0
for i in range(n):
shi = p[i] - i
dev += abs(shi)
if p[i] <= i:
dd += 1
shi = n + shi
chd[shi] += 2
else:
dd -= 1
chd[shi] += 2
bdev = dev
bsh = 0
for i in range(1, n):
dev -= abs(p[-i] - (n - 1))
dev += abs(p[-i] - 0)
dd -= 1
dev += dd
dd -= 1
dd += chd[i]
if dev < bdev:
bsh = i
bdev = dev
print(bdev, bsh)
``` | output | 1 | 101,708 | 12 | 203,417 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let's define the deviation of a permutation p as <image>.
Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.
Let's denote id k (0 β€ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:
* k = 0: shift p1, p2, ... pn,
* k = 1: shift pn, p1, ... pn - 1,
* ...,
* k = n - 1: shift p2, p3, ... pn, p1.
Input
First line contains single integer n (2 β€ n β€ 106) β the length of the permutation.
The second line contains n space-separated integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of the permutation. It is guaranteed that all elements are distinct.
Output
Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.
Examples
Input
3
1 2 3
Output
0 0
Input
3
2 3 1
Output
0 1
Input
3
3 2 1
Output
2 1
Note
In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.
In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift.
In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts. | instruction | 0 | 101,709 | 12 | 203,418 |
Tags: data structures, implementation, math
Correct Solution:
```
def main():
n = int(input())
data = input().split()
#print(str(n) + " " + str(data))
data = list(map(lambda x: int(x), data))
res = 0
ires = 0
neg = 0
when = [0] * n
for i in range(n):
data[i] = i + 1 - data[i]
res += abs(data[i])
if data[i] <= 0:
neg += 1
a = -data[i]
if a < 0:
a = a + n
when[a] += 1
#print(when)
ares = res
#print(str(res) + " " + str(ires) + " " + str(neg))
for i in range(n):
neg -= when[i]
ares -= neg
ares += (n - neg)
x = data[n - i - 1] + i + 1
ares -= x
ares += n - x
#print(str(res) + " " + str(ires) + " " + str(ares) + " " + str(i) + " " + str(neg))
neg += 1
if ares < res:
res = ares
ires = i + 1
print(str(res) + " " + str(ires))
main()
``` | output | 1 | 101,709 | 12 | 203,419 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let's define the deviation of a permutation p as <image>.
Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.
Let's denote id k (0 β€ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:
* k = 0: shift p1, p2, ... pn,
* k = 1: shift pn, p1, ... pn - 1,
* ...,
* k = n - 1: shift p2, p3, ... pn, p1.
Input
First line contains single integer n (2 β€ n β€ 106) β the length of the permutation.
The second line contains n space-separated integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of the permutation. It is guaranteed that all elements are distinct.
Output
Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.
Examples
Input
3
1 2 3
Output
0 0
Input
3
2 3 1
Output
0 1
Input
3
3 2 1
Output
2 1
Note
In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.
In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift.
In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts.
Submitted Solution:
```
I = lambda : map(int, input().split())
n, = I()
if n == 975587:
print(316925125798, 610904)
exit(0)
P = [0] + list(I())
A = [0]
for i in range(1,n+1):
A.append(A[i-1] + P[i])
mn = -99999999999999999999999
ans = 0
for i in range(0,n):
if i*A[n-i] - (n-i)*(A[n] - A[n-i]) > mn:
mn = i*A[n-i] - (n-i)*(A[n] - A[n-i])
ans = i
dev = 0
for i in range(1,n+1):
indx = i+ans
if indx > n:
indx -= n
dev += abs(indx - P[i])
print(dev, ans)
``` | instruction | 0 | 101,710 | 12 | 203,420 |
No | output | 1 | 101,710 | 12 | 203,421 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let's define the deviation of a permutation p as <image>.
Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.
Let's denote id k (0 β€ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:
* k = 0: shift p1, p2, ... pn,
* k = 1: shift pn, p1, ... pn - 1,
* ...,
* k = n - 1: shift p2, p3, ... pn, p1.
Input
First line contains single integer n (2 β€ n β€ 106) β the length of the permutation.
The second line contains n space-separated integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of the permutation. It is guaranteed that all elements are distinct.
Output
Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.
Examples
Input
3
1 2 3
Output
0 0
Input
3
2 3 1
Output
0 1
Input
3
3 2 1
Output
2 1
Note
In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.
In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift.
In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts.
Submitted Solution:
```
I = lambda : map(int, input().split())
n, = I()
P = [0] + list(I())
A = [0]
for i in range(1,n+1):
A.append(A[i-1] + P[i])
mn = -99999999999999999999999
ans = 0
for i in range(0,n):
if i*A[n-i] - (n-i)*(A[n] - A[n-i]) > mn:
mn = i*A[n-i] - (n-i)*(A[n] - A[n-i])
ans = i
dev = 0
for i in range(1,n+1):
indx = i+ans
if indx > n:
indx -= n
dev += abs(indx - P[i])
print(dev, ans)
``` | instruction | 0 | 101,711 | 12 | 203,422 |
No | output | 1 | 101,711 | 12 | 203,423 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let's define the deviation of a permutation p as <image>.
Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.
Let's denote id k (0 β€ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:
* k = 0: shift p1, p2, ... pn,
* k = 1: shift pn, p1, ... pn - 1,
* ...,
* k = n - 1: shift p2, p3, ... pn, p1.
Input
First line contains single integer n (2 β€ n β€ 106) β the length of the permutation.
The second line contains n space-separated integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of the permutation. It is guaranteed that all elements are distinct.
Output
Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.
Examples
Input
3
1 2 3
Output
0 0
Input
3
2 3 1
Output
0 1
Input
3
3 2 1
Output
2 1
Note
In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.
In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift.
In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts.
Submitted Solution:
```
length = int(input())
string = input()
numbers = string.split(" ")
for x in range(length):
numbers[x] = int(numbers[x])
values = []
for x in range(length):
a = 0
for y in range(length):
z = (y + x) % length
a += abs(numbers[z] - (y + 1))
if x == 0:
minimum = a
b = x
elif a < minimum:
minimum = a
b = x
print("%d %d" % (minimum, b))
``` | instruction | 0 | 101,712 | 12 | 203,424 |
No | output | 1 | 101,712 | 12 | 203,425 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let's define the deviation of a permutation p as <image>.
Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.
Let's denote id k (0 β€ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:
* k = 0: shift p1, p2, ... pn,
* k = 1: shift pn, p1, ... pn - 1,
* ...,
* k = n - 1: shift p2, p3, ... pn, p1.
Input
First line contains single integer n (2 β€ n β€ 106) β the length of the permutation.
The second line contains n space-separated integers p1, p2, ..., pn (1 β€ pi β€ n) β the elements of the permutation. It is guaranteed that all elements are distinct.
Output
Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.
Examples
Input
3
1 2 3
Output
0 0
Input
3
2 3 1
Output
0 1
Input
3
3 2 1
Output
2 1
Note
In the first sample test the given permutation p is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.
In the second sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 2, 3) equals to 0, the deviation of the 2-nd cyclic shift (3, 1, 2) equals to 4, the optimal is the 1-st cyclic shift.
In the third sample test the deviation of p equals to 4, the deviation of the 1-st cyclic shift (1, 3, 2) equals to 2, the deviation of the 2-nd cyclic shift (2, 1, 3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts.
Submitted Solution:
```
#!/usr/bin/env python
def diff(nums):
sum_ = 0
for i in range(0, len(nums)):
sum_ += abs(nums[i] - i - 1)
return sum_
n = int(input())
numbers = [int(x) for x in input().split()]
min_p = diff(numbers)
if min_p == 0:
print("0 0")
quit()
min_n = 0
for i in range(1, n - 1):
numbers_shifted = numbers[-i:] + numbers[:-i]
d = diff(numbers_shifted)
if d < min_p:
min_p = d
min_n = i
print(str(min_p) + " " + str(min_n))
``` | instruction | 0 | 101,713 | 12 | 203,426 |
No | output | 1 | 101,713 | 12 | 203,427 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted. | instruction | 0 | 101,730 | 12 | 203,460 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
data = list(map(int, input().split()))
sorted_data = sorted(data)
ans = {}
for i in range(0, n):
ans[sorted_data[i]] = sorted_data[(i + 1) % n]
for v in data:
print(ans[v], end=' ')
``` | output | 1 | 101,730 | 12 | 203,461 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted. | instruction | 0 | 101,731 | 12 | 203,462 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
arr=sorted(a,reverse=True)
ans=[None for x in range(n)]
for i in range(n-1):
pos=a.index(arr[i])
ans[pos]=arr[i+1]
for i in range(n):
if ans[i]==None:
ans[i]=arr[0]
print(*ans)
``` | output | 1 | 101,731 | 12 | 203,463 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted. | instruction | 0 | 101,732 | 12 | 203,464 |
Tags: constructive algorithms, greedy
Correct Solution:
```
def solve():
n=int(input())
a=list(map(int,input().split()))
b=sorted(a)+[min(a)]
for i in range(n):
a[i]=str(b[b.index(a[i])+1])
print(' '.join(a))
return
solve()
``` | output | 1 | 101,732 | 12 | 203,465 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted. | instruction | 0 | 101,733 | 12 | 203,466 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
b=a.copy()
a.sort()
c=[]
for i in range(n):
c.append(a[(a.index(b[i])+1)%n])
print(*c)
``` | output | 1 | 101,733 | 12 | 203,467 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted. | instruction | 0 | 101,734 | 12 | 203,468 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
b = sorted(a) + [min(a)]
for i in range(n):print(b[b.index(a[i])+1],end=' ')
``` | output | 1 | 101,734 | 12 | 203,469 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted. | instruction | 0 | 101,735 | 12 | 203,470 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import sys, math
readline = sys.stdin.readline
n = int(readline())
inf = pow(10,10)
tmp = list(map(int,readline().split()))
tmp2 = [inf] * n
mai = 0
for i in range(n):
for j in range(n):
if tmp[i] < tmp[j]:
tmp2[i] = min(tmp2[i],tmp[j])
for i in range(n):
if tmp[i] == max(tmp):
mai = i
break
tmp2[mai] = min(tmp)
for x in tmp2:
print(x,end=' ')
``` | output | 1 | 101,735 | 12 | 203,471 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted. | instruction | 0 | 101,736 | 12 | 203,472 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
a = [int(i) for i in input().split()]
b = sorted(a)
s = {}
for i in range(-1, n-1): s[b[i+1]] = b[i]
print(' '.join([str(s[i]) for i in a]))
``` | output | 1 | 101,736 | 12 | 203,473 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted. | instruction | 0 | 101,737 | 12 | 203,474 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
a = list(map(int , input().split()))
ans = list()
for i in range(n):
ans.append(0)
position = dict()
for i in range(n):
position[a[i]] = i;
a = sorted(a)
ans[position[a[n - 1]]] = a[0];
for i in range(n - 1):
ans[position[a[i]]] = a[i + 1]
for i in range(n):
print(ans[i] , end = " ")
``` | output | 1 | 101,737 | 12 | 203,475 |
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 with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
Submitted Solution:
```
n = int(input())
A = list(map(int, input().split()))
C = []
for i, a in enumerate(A):
C.append((a, i))
C.sort()
B = [-1]*n
for i in range(n-1):
B[C[i+1][1]] = C[i][0]
B[C[0][1]] = C[-1][0]
print(*B)
``` | instruction | 0 | 101,738 | 12 | 203,476 |
Yes | output | 1 | 101,738 | 12 | 203,477 |
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 with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
Submitted Solution:
```
input()
t, s = zip(*sorted((int(q), i) for i, q in enumerate(input().split())))
for i, q in sorted((i, q) for q, i in zip(t[1:] + t[:1], s)): print(q)
``` | instruction | 0 | 101,739 | 12 | 203,478 |
Yes | output | 1 | 101,739 | 12 | 203,479 |
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 with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
Submitted Solution:
```
#!/usr/bin/env python3
n = int(input())
A = list(map(int,input().split()))
S = sorted(A) #sorted((A[i],i) for i in range(n))
P = {S[i]:S[(i+1)%n] for i in range(n)}
B = [P[a] for a in A]
print(' '.join(map(str,B)))
``` | instruction | 0 | 101,740 | 12 | 203,480 |
Yes | output | 1 | 101,740 | 12 | 203,481 |
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 with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
Submitted Solution:
```
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
"""
created by shhuan at 2017/11/17 22:54
"""
N = int(input())
A = [int(x) for x in input().split()]
wc = collections.Counter(A)
if any(v > 1 for v in wc.values()):
print(-1)
exit(0)
C = list(sorted(A))
NC = {C[i]: C[i+1] for i in range(N-1)}
NC[C[-1]] = C[0]
ans = []
for v in A:
ans.append(NC[v])
print(" ".join(map(str, ans)))
``` | instruction | 0 | 101,741 | 12 | 203,482 |
Yes | output | 1 | 101,741 | 12 | 203,483 |
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 with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
Submitted Solution:
```
def chec():
global m, a
s = 0
for i in range(len(m)):
m[i] = s + m[i]
s += m[i]
l = 0
r = len(m) - 1
while True:
if abs(m[r] - m[l]) == abs(a[r] - a[l]):
return False
l += 1
if l == r:
r -= 1
l = 0
if l == r:
return True
n = int(input())
m = list(map(int, input().split()))
a = [0] * len(m)
ans = [0] * len(m)
s = 0
for i in range(n):
a[i] = s + m[i]
s += a[i]
for i in range(n - 1):
m[i], m[i + 1] = m[i + 1], m[i]
for i in range(n):
ans[i] = m[i]
#if chec():
print(*ans)
#else:
# print(-1)
``` | instruction | 0 | 101,742 | 12 | 203,484 |
No | output | 1 | 101,742 | 12 | 203,485 |
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 with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
Submitted Solution:
```
# http://codeforces.com/problemset/problem/892/D
n = int(input())
a = input().split()
if n == 1:
print(-1)
else:
b = a[1:]+[a[0]]
print(' '.join(b))
``` | instruction | 0 | 101,743 | 12 | 203,486 |
No | output | 1 | 101,743 | 12 | 203,487 |
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 with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
Submitted Solution:
```
n = int(input())
if n == 1:
print(-1)
exit()
data = list(map(int, input().split()))
sorted_data = sorted(data)
ans = {}
for i in range(0, n):
ans[sorted_data[i]] = sorted_data[(i + 1) % n]
for v in data:
print(ans[v], end=' ')
``` | instruction | 0 | 101,744 | 12 | 203,488 |
No | output | 1 | 101,744 | 12 | 203,489 |
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 with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
<image>
Input
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.
The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
Output
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
Examples
Input
2
1 2
Output
2 1
Input
4
1000 100 10 1
Output
100 1 1000 10
Note
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
Submitted Solution:
```
n = int(input())
A = [int(a) for a in input().split(' ')]
S = set(A)
B = []
def diff(x, y):
return abs(x-y) * abs(A.index(x)-A.index(y))
def check():
for i in range(n):
for j in range(i, n):
if not (i == 0 and j == n-1) and sum(A[i:j+1]) == sum(B[i:j+1]):
return False
return True
for i in range(n):
s = max(S, key=lambda s: diff(A[i],s))
B.append(s)
S.remove(s)
#if check():
print(' '.join([str(b) for b in B]))
#else:
# print(-1)
``` | instruction | 0 | 101,745 | 12 | 203,490 |
No | output | 1 | 101,745 | 12 | 203,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Toad Mikhail has an array of 2^k integers a_1, a_2, β¦, a_{2^k}.
Find two permutations p and q of integers 0, 1, β¦, 2^k-1, such that a_i is equal to p_i β q_i for all possible i, or determine there are no such permutations. Here β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Input
The first line contains one integer k (2 β€ k β€ 12), denoting that the size of the array is 2^k.
The next line contains 2^k space-separated integers a_1, a_2, β¦, a_{2^k} (0 β€ a_i < 2^k) β the elements of the given array.
Output
If the given array can't be represented as element-wise XOR of two permutations of integers 0, 1, β¦, 2^k-1, print "Fou".
Otherwise, print "Shi" in the first line.
The next two lines should contain the description of two suitable permutations. The first of these lines should contain 2^k space-separated distinct integers p_{1}, p_{2}, β¦, p_{2^k}, and the second line should contain 2^k space-separated distinct integers q_{1}, q_{2}, β¦, q_{2^k}.
All elements of p and q should be between 0 and 2^k - 1, inclusive; p_i β q_i should be equal to a_i for all i such that 1 β€ i β€ 2^k. If there are several possible solutions, you can print any.
Examples
Input
2
0 1 2 3
Output
Shi
2 0 1 3
2 1 3 0
Input
2
0 0 0 0
Output
Shi
0 1 2 3
0 1 2 3
Input
2
0 1 2 2
Output
Fou
Submitted Solution:
```
k=int(input(""))
t=list(map(int,input("").split(" ")))
a=[]
b=[]
for i in t:
found=0
for j in range(2**k):
if j in b:
continue
for h in range(2**k):
e=j^h
if e==i:
if h in a:
continue
found=1
a.append(h)
b.append(j)
break
if found==1:
break
if len(a)==len(t):
print("Shi")
for f in a:
print(f,sep=" ",end=" ")
print()
for g in b:
print(g,sep=" ",end=" ")
else:
print("Fou")
``` | instruction | 0 | 102,070 | 12 | 204,140 |
No | output | 1 | 102,070 | 12 | 204,141 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Toad Mikhail has an array of 2^k integers a_1, a_2, β¦, a_{2^k}.
Find two permutations p and q of integers 0, 1, β¦, 2^k-1, such that a_i is equal to p_i β q_i for all possible i, or determine there are no such permutations. Here β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Input
The first line contains one integer k (2 β€ k β€ 12), denoting that the size of the array is 2^k.
The next line contains 2^k space-separated integers a_1, a_2, β¦, a_{2^k} (0 β€ a_i < 2^k) β the elements of the given array.
Output
If the given array can't be represented as element-wise XOR of two permutations of integers 0, 1, β¦, 2^k-1, print "Fou".
Otherwise, print "Shi" in the first line.
The next two lines should contain the description of two suitable permutations. The first of these lines should contain 2^k space-separated distinct integers p_{1}, p_{2}, β¦, p_{2^k}, and the second line should contain 2^k space-separated distinct integers q_{1}, q_{2}, β¦, q_{2^k}.
All elements of p and q should be between 0 and 2^k - 1, inclusive; p_i β q_i should be equal to a_i for all i such that 1 β€ i β€ 2^k. If there are several possible solutions, you can print any.
Examples
Input
2
0 1 2 3
Output
Shi
2 0 1 3
2 1 3 0
Input
2
0 0 0 0
Output
Shi
0 1 2 3
0 1 2 3
Input
2
0 1 2 2
Output
Fou
Submitted Solution:
```
k=int(input(""))
t=list(map(int,input("").split(" ")))
a=[]
b=[]
for i in t:
found=0
for j in range(2**k):
if j in b:
continue
for h in range(2**k):
e=j^h
if e==i:
if h in a:
continue
found=1
a.append(h)
b.append(j)
break
if found==1:
break
if len(a)==len(t):
print("shi")
for f in a:
print(f,sep=" ",end=" ")
print()
for g in b:
print(g,sep=" ",end=" ")
else:
print("Fou")
``` | instruction | 0 | 102,071 | 12 | 204,142 |
No | output | 1 | 102,071 | 12 | 204,143 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array A, consisting of n positive integers a_1, a_2, ..., a_n, and an array B, consisting of m positive integers b_1, b_2, ..., b_m.
Choose some element a of A and some element b of B such that a+b doesn't belong to A and doesn't belong to B.
For example, if A = [2, 1, 7] and B = [1, 3, 4], we can choose 1 from A and 4 from B, as number 5 = 1 + 4 doesn't belong to A and doesn't belong to B. However, we can't choose 2 from A and 1 from B, as 3 = 2 + 1 belongs to B.
It can be shown that such a pair exists. If there are multiple answers, print any.
Choose and print any such two numbers.
Input
The first line contains one integer n (1β€ n β€ 100) β the number of elements of A.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 200) β the elements of A.
The third line contains one integer m (1β€ m β€ 100) β the number of elements of B.
The fourth line contains m different integers b_1, b_2, ..., b_m (1 β€ b_i β€ 200) β the elements of B.
It can be shown that the answer always exists.
Output
Output two numbers a and b such that a belongs to A, b belongs to B, but a+b doesn't belong to nor A neither B.
If there are multiple answers, print any.
Examples
Input
1
20
2
10 20
Output
20 20
Input
3
3 2 2
5
1 5 7 7 9
Output
3 1
Input
4
1 3 5 7
4
7 5 3 1
Output
1 1
Note
In the first example, we can choose 20 from array [20] and 20 from array [10, 20]. Number 40 = 20 + 20 doesn't belong to any of those arrays. However, it is possible to choose 10 from the second array too.
In the second example, we can choose 3 from array [3, 2, 2] and 1 from array [1, 5, 7, 7, 9]. Number 4 = 3 + 1 doesn't belong to any of those arrays.
In the third example, we can choose 1 from array [1, 3, 5, 7] and 1 from array [7, 5, 3, 1]. Number 2 = 1 + 1 doesn't belong to any of those arrays. | instruction | 0 | 102,088 | 12 | 204,176 |
Tags: math, sortings
Correct Solution:
```
I = lambda: max(map(int, input().split()))
input()
a = I()
input()
print(a, I())
``` | output | 1 | 102,088 | 12 | 204,177 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array A, consisting of n positive integers a_1, a_2, ..., a_n, and an array B, consisting of m positive integers b_1, b_2, ..., b_m.
Choose some element a of A and some element b of B such that a+b doesn't belong to A and doesn't belong to B.
For example, if A = [2, 1, 7] and B = [1, 3, 4], we can choose 1 from A and 4 from B, as number 5 = 1 + 4 doesn't belong to A and doesn't belong to B. However, we can't choose 2 from A and 1 from B, as 3 = 2 + 1 belongs to B.
It can be shown that such a pair exists. If there are multiple answers, print any.
Choose and print any such two numbers.
Input
The first line contains one integer n (1β€ n β€ 100) β the number of elements of A.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 200) β the elements of A.
The third line contains one integer m (1β€ m β€ 100) β the number of elements of B.
The fourth line contains m different integers b_1, b_2, ..., b_m (1 β€ b_i β€ 200) β the elements of B.
It can be shown that the answer always exists.
Output
Output two numbers a and b such that a belongs to A, b belongs to B, but a+b doesn't belong to nor A neither B.
If there are multiple answers, print any.
Examples
Input
1
20
2
10 20
Output
20 20
Input
3
3 2 2
5
1 5 7 7 9
Output
3 1
Input
4
1 3 5 7
4
7 5 3 1
Output
1 1
Note
In the first example, we can choose 20 from array [20] and 20 from array [10, 20]. Number 40 = 20 + 20 doesn't belong to any of those arrays. However, it is possible to choose 10 from the second array too.
In the second example, we can choose 3 from array [3, 2, 2] and 1 from array [1, 5, 7, 7, 9]. Number 4 = 3 + 1 doesn't belong to any of those arrays.
In the third example, we can choose 1 from array [1, 3, 5, 7] and 1 from array [7, 5, 3, 1]. Number 2 = 1 + 1 doesn't belong to any of those arrays. | instruction | 0 | 102,089 | 12 | 204,178 |
Tags: math, sortings
Correct Solution:
```
na=int(input())
a=list(map(int,input().split()))
da={}
for i in a:
da[i]=0
nb=int(input())
b=list(map(int,input().split()))
db={}
for i in b:
db[i]=0
z=1
for i in range(na):
for j in range(nb):
su=a[i]+b[j]
try:
l1=da[su]
except KeyError:
try:
l2=db[su]
except KeyError:
print(a[i],b[j])
z=0
break
if z==0:
break
if z==0:
break
``` | output | 1 | 102,089 | 12 | 204,179 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array A, consisting of n positive integers a_1, a_2, ..., a_n, and an array B, consisting of m positive integers b_1, b_2, ..., b_m.
Choose some element a of A and some element b of B such that a+b doesn't belong to A and doesn't belong to B.
For example, if A = [2, 1, 7] and B = [1, 3, 4], we can choose 1 from A and 4 from B, as number 5 = 1 + 4 doesn't belong to A and doesn't belong to B. However, we can't choose 2 from A and 1 from B, as 3 = 2 + 1 belongs to B.
It can be shown that such a pair exists. If there are multiple answers, print any.
Choose and print any such two numbers.
Input
The first line contains one integer n (1β€ n β€ 100) β the number of elements of A.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 200) β the elements of A.
The third line contains one integer m (1β€ m β€ 100) β the number of elements of B.
The fourth line contains m different integers b_1, b_2, ..., b_m (1 β€ b_i β€ 200) β the elements of B.
It can be shown that the answer always exists.
Output
Output two numbers a and b such that a belongs to A, b belongs to B, but a+b doesn't belong to nor A neither B.
If there are multiple answers, print any.
Examples
Input
1
20
2
10 20
Output
20 20
Input
3
3 2 2
5
1 5 7 7 9
Output
3 1
Input
4
1 3 5 7
4
7 5 3 1
Output
1 1
Note
In the first example, we can choose 20 from array [20] and 20 from array [10, 20]. Number 40 = 20 + 20 doesn't belong to any of those arrays. However, it is possible to choose 10 from the second array too.
In the second example, we can choose 3 from array [3, 2, 2] and 1 from array [1, 5, 7, 7, 9]. Number 4 = 3 + 1 doesn't belong to any of those arrays.
In the third example, we can choose 1 from array [1, 3, 5, 7] and 1 from array [7, 5, 3, 1]. Number 2 = 1 + 1 doesn't belong to any of those arrays. | instruction | 0 | 102,090 | 12 | 204,180 |
Tags: math, sortings
Correct Solution:
```
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
n = int(input())
a = I()
m, = I()
b = I()
a.sort()
b.sort()
print(a[-1], b[-1])
``` | output | 1 | 102,090 | 12 | 204,181 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array A, consisting of n positive integers a_1, a_2, ..., a_n, and an array B, consisting of m positive integers b_1, b_2, ..., b_m.
Choose some element a of A and some element b of B such that a+b doesn't belong to A and doesn't belong to B.
For example, if A = [2, 1, 7] and B = [1, 3, 4], we can choose 1 from A and 4 from B, as number 5 = 1 + 4 doesn't belong to A and doesn't belong to B. However, we can't choose 2 from A and 1 from B, as 3 = 2 + 1 belongs to B.
It can be shown that such a pair exists. If there are multiple answers, print any.
Choose and print any such two numbers.
Input
The first line contains one integer n (1β€ n β€ 100) β the number of elements of A.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 200) β the elements of A.
The third line contains one integer m (1β€ m β€ 100) β the number of elements of B.
The fourth line contains m different integers b_1, b_2, ..., b_m (1 β€ b_i β€ 200) β the elements of B.
It can be shown that the answer always exists.
Output
Output two numbers a and b such that a belongs to A, b belongs to B, but a+b doesn't belong to nor A neither B.
If there are multiple answers, print any.
Examples
Input
1
20
2
10 20
Output
20 20
Input
3
3 2 2
5
1 5 7 7 9
Output
3 1
Input
4
1 3 5 7
4
7 5 3 1
Output
1 1
Note
In the first example, we can choose 20 from array [20] and 20 from array [10, 20]. Number 40 = 20 + 20 doesn't belong to any of those arrays. However, it is possible to choose 10 from the second array too.
In the second example, we can choose 3 from array [3, 2, 2] and 1 from array [1, 5, 7, 7, 9]. Number 4 = 3 + 1 doesn't belong to any of those arrays.
In the third example, we can choose 1 from array [1, 3, 5, 7] and 1 from array [7, 5, 3, 1]. Number 2 = 1 + 1 doesn't belong to any of those arrays. | instruction | 0 | 102,091 | 12 | 204,182 |
Tags: math, sortings
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
p=int(input())
b=list(map(int,input().split()))
print(max(a),max(b))
``` | output | 1 | 102,091 | 12 | 204,183 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array A, consisting of n positive integers a_1, a_2, ..., a_n, and an array B, consisting of m positive integers b_1, b_2, ..., b_m.
Choose some element a of A and some element b of B such that a+b doesn't belong to A and doesn't belong to B.
For example, if A = [2, 1, 7] and B = [1, 3, 4], we can choose 1 from A and 4 from B, as number 5 = 1 + 4 doesn't belong to A and doesn't belong to B. However, we can't choose 2 from A and 1 from B, as 3 = 2 + 1 belongs to B.
It can be shown that such a pair exists. If there are multiple answers, print any.
Choose and print any such two numbers.
Input
The first line contains one integer n (1β€ n β€ 100) β the number of elements of A.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 200) β the elements of A.
The third line contains one integer m (1β€ m β€ 100) β the number of elements of B.
The fourth line contains m different integers b_1, b_2, ..., b_m (1 β€ b_i β€ 200) β the elements of B.
It can be shown that the answer always exists.
Output
Output two numbers a and b such that a belongs to A, b belongs to B, but a+b doesn't belong to nor A neither B.
If there are multiple answers, print any.
Examples
Input
1
20
2
10 20
Output
20 20
Input
3
3 2 2
5
1 5 7 7 9
Output
3 1
Input
4
1 3 5 7
4
7 5 3 1
Output
1 1
Note
In the first example, we can choose 20 from array [20] and 20 from array [10, 20]. Number 40 = 20 + 20 doesn't belong to any of those arrays. However, it is possible to choose 10 from the second array too.
In the second example, we can choose 3 from array [3, 2, 2] and 1 from array [1, 5, 7, 7, 9]. Number 4 = 3 + 1 doesn't belong to any of those arrays.
In the third example, we can choose 1 from array [1, 3, 5, 7] and 1 from array [7, 5, 3, 1]. Number 2 = 1 + 1 doesn't belong to any of those arrays. | instruction | 0 | 102,092 | 12 | 204,184 |
Tags: math, sortings
Correct Solution:
```
n1=int(input())
l1=[int(x) for x in input().split()]
n2=int(input())
l2=[int(x) for x in input().split()]
for i in range(0,n1):
for j in range(0,n2):
if(l1[i]+l2[j] not in l1 and l1[i]+l2[j] not in l2):
s=l1[i]
s1=l2[j]
print(s,s1)
``` | output | 1 | 102,092 | 12 | 204,185 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array A, consisting of n positive integers a_1, a_2, ..., a_n, and an array B, consisting of m positive integers b_1, b_2, ..., b_m.
Choose some element a of A and some element b of B such that a+b doesn't belong to A and doesn't belong to B.
For example, if A = [2, 1, 7] and B = [1, 3, 4], we can choose 1 from A and 4 from B, as number 5 = 1 + 4 doesn't belong to A and doesn't belong to B. However, we can't choose 2 from A and 1 from B, as 3 = 2 + 1 belongs to B.
It can be shown that such a pair exists. If there are multiple answers, print any.
Choose and print any such two numbers.
Input
The first line contains one integer n (1β€ n β€ 100) β the number of elements of A.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 200) β the elements of A.
The third line contains one integer m (1β€ m β€ 100) β the number of elements of B.
The fourth line contains m different integers b_1, b_2, ..., b_m (1 β€ b_i β€ 200) β the elements of B.
It can be shown that the answer always exists.
Output
Output two numbers a and b such that a belongs to A, b belongs to B, but a+b doesn't belong to nor A neither B.
If there are multiple answers, print any.
Examples
Input
1
20
2
10 20
Output
20 20
Input
3
3 2 2
5
1 5 7 7 9
Output
3 1
Input
4
1 3 5 7
4
7 5 3 1
Output
1 1
Note
In the first example, we can choose 20 from array [20] and 20 from array [10, 20]. Number 40 = 20 + 20 doesn't belong to any of those arrays. However, it is possible to choose 10 from the second array too.
In the second example, we can choose 3 from array [3, 2, 2] and 1 from array [1, 5, 7, 7, 9]. Number 4 = 3 + 1 doesn't belong to any of those arrays.
In the third example, we can choose 1 from array [1, 3, 5, 7] and 1 from array [7, 5, 3, 1]. Number 2 = 1 + 1 doesn't belong to any of those arrays. | instruction | 0 | 102,093 | 12 | 204,186 |
Tags: math, sortings
Correct Solution:
```
n = int(input())
arr = list(map(int,input().split()))
m = int(input())
arr1 = list(map(int,input().split()))
x = max(arr)
y = max(arr1)
print(f"{x} {y}")
``` | output | 1 | 102,093 | 12 | 204,187 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array A, consisting of n positive integers a_1, a_2, ..., a_n, and an array B, consisting of m positive integers b_1, b_2, ..., b_m.
Choose some element a of A and some element b of B such that a+b doesn't belong to A and doesn't belong to B.
For example, if A = [2, 1, 7] and B = [1, 3, 4], we can choose 1 from A and 4 from B, as number 5 = 1 + 4 doesn't belong to A and doesn't belong to B. However, we can't choose 2 from A and 1 from B, as 3 = 2 + 1 belongs to B.
It can be shown that such a pair exists. If there are multiple answers, print any.
Choose and print any such two numbers.
Input
The first line contains one integer n (1β€ n β€ 100) β the number of elements of A.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 200) β the elements of A.
The third line contains one integer m (1β€ m β€ 100) β the number of elements of B.
The fourth line contains m different integers b_1, b_2, ..., b_m (1 β€ b_i β€ 200) β the elements of B.
It can be shown that the answer always exists.
Output
Output two numbers a and b such that a belongs to A, b belongs to B, but a+b doesn't belong to nor A neither B.
If there are multiple answers, print any.
Examples
Input
1
20
2
10 20
Output
20 20
Input
3
3 2 2
5
1 5 7 7 9
Output
3 1
Input
4
1 3 5 7
4
7 5 3 1
Output
1 1
Note
In the first example, we can choose 20 from array [20] and 20 from array [10, 20]. Number 40 = 20 + 20 doesn't belong to any of those arrays. However, it is possible to choose 10 from the second array too.
In the second example, we can choose 3 from array [3, 2, 2] and 1 from array [1, 5, 7, 7, 9]. Number 4 = 3 + 1 doesn't belong to any of those arrays.
In the third example, we can choose 1 from array [1, 3, 5, 7] and 1 from array [7, 5, 3, 1]. Number 2 = 1 + 1 doesn't belong to any of those arrays. | instruction | 0 | 102,094 | 12 | 204,188 |
Tags: math, sortings
Correct Solution:
```
n=int(input())
a=[]
a=list(map(int,input().strip().split()))[:n]
m=int(input())
b=list(map(int,input().strip().split()))[:m]
print(max(a),max(b))
``` | output | 1 | 102,094 | 12 | 204,189 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array A, consisting of n positive integers a_1, a_2, ..., a_n, and an array B, consisting of m positive integers b_1, b_2, ..., b_m.
Choose some element a of A and some element b of B such that a+b doesn't belong to A and doesn't belong to B.
For example, if A = [2, 1, 7] and B = [1, 3, 4], we can choose 1 from A and 4 from B, as number 5 = 1 + 4 doesn't belong to A and doesn't belong to B. However, we can't choose 2 from A and 1 from B, as 3 = 2 + 1 belongs to B.
It can be shown that such a pair exists. If there are multiple answers, print any.
Choose and print any such two numbers.
Input
The first line contains one integer n (1β€ n β€ 100) β the number of elements of A.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 200) β the elements of A.
The third line contains one integer m (1β€ m β€ 100) β the number of elements of B.
The fourth line contains m different integers b_1, b_2, ..., b_m (1 β€ b_i β€ 200) β the elements of B.
It can be shown that the answer always exists.
Output
Output two numbers a and b such that a belongs to A, b belongs to B, but a+b doesn't belong to nor A neither B.
If there are multiple answers, print any.
Examples
Input
1
20
2
10 20
Output
20 20
Input
3
3 2 2
5
1 5 7 7 9
Output
3 1
Input
4
1 3 5 7
4
7 5 3 1
Output
1 1
Note
In the first example, we can choose 20 from array [20] and 20 from array [10, 20]. Number 40 = 20 + 20 doesn't belong to any of those arrays. However, it is possible to choose 10 from the second array too.
In the second example, we can choose 3 from array [3, 2, 2] and 1 from array [1, 5, 7, 7, 9]. Number 4 = 3 + 1 doesn't belong to any of those arrays.
In the third example, we can choose 1 from array [1, 3, 5, 7] and 1 from array [7, 5, 3, 1]. Number 2 = 1 + 1 doesn't belong to any of those arrays. | instruction | 0 | 102,095 | 12 | 204,190 |
Tags: math, sortings
Correct Solution:
```
#codeforces
a = int(input())
A = list(map(int,input().split()))
b = int(input())
B = list(map(int,input().split()))
A.sort()
B.sort()
print(A[a-1],B[b-1])
``` | output | 1 | 102,095 | 12 | 204,191 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning. | instruction | 0 | 102,104 | 12 | 204,208 |
Tags: dp, greedy, two pointers
Correct Solution:
```
import sys as _sys
def main():
q = int(input())
for i_q in range(q):
n, = _read_ints()
a = tuple(_read_ints())
result = find_min_sorting_cost(sequence=a)
print(result)
def _read_line():
result = _sys.stdin.readline()
assert result[-1] == "\n"
return result[:-1]
def _read_ints():
return map(int, _read_line().split(" "))
def find_min_sorting_cost(sequence):
sequence = tuple(sequence)
if not sequence:
return 0
indices_by_values = {x: [] for x in sequence}
for i, x in enumerate(sequence):
indices_by_values[x].append(i)
borders_by_values = {
x: (indices[0], indices[-1]) for x, indices in indices_by_values.items()
}
borders_sorted_by_values = [borders for x, borders in sorted(borders_by_values.items())]
max_cost_can_keep_n = curr_can_keep_n = 1
for prev_border, curr_border in zip(borders_sorted_by_values, borders_sorted_by_values[1:]):
if curr_border[0] > prev_border[1]:
curr_can_keep_n += 1
else:
if curr_can_keep_n > max_cost_can_keep_n:
max_cost_can_keep_n = curr_can_keep_n
curr_can_keep_n = 1
if curr_can_keep_n > max_cost_can_keep_n:
max_cost_can_keep_n = curr_can_keep_n
return len(set(sequence)) - max_cost_can_keep_n
if __name__ == '__main__':
main()
``` | output | 1 | 102,104 | 12 | 204,209 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.