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 |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal β compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
Input
The first line contains an single integer n (2 β€ n β€ 200 000) β the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ n) β the description of Ivan's array.
Output
In the first line print q β the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
Examples
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1
Input
10
6 8 4 6 7 1 6 3 4 5
Output
3
2 8 4 6 7 1 9 3 10 5
Note
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers β this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation. | instruction | 0 | 82,273 | 12 | 164,546 |
Tags: greedy, implementation, math
Correct Solution:
```
import math
import sys
from collections import deque
import heapq
import time
input = sys.stdin.readline
mod=10**9+7
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
n=inp()
l=inlt()
d={}
for i in range(n):
d[i+1]=deque()
v=[0]*n
x=[]
for i in range(n):
d[l[i]].append(l[i])
v[l[i]-1]=1
p=0
x=[0]*n
for i in range(n):
if v[i]==0:
for j in range(p,n):
if len(d[l[p]])>1:
if i+1<l[p] or x[l[p]-1]==1:
d[l[p]].popleft()
l[p]=i+1
p+=1
break
else:
x[l[p]-1]=1
p+=1
print(n-sum(v))
print(*l)
``` | output | 1 | 82,273 | 12 | 164,547 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal β compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
Input
The first line contains an single integer n (2 β€ n β€ 200 000) β the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ n) β the description of Ivan's array.
Output
In the first line print q β the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
Examples
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1
Input
10
6 8 4 6 7 1 6 3 4 5
Output
3
2 8 4 6 7 1 9 3 10 5
Note
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers β this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation. | instruction | 0 | 82,274 | 12 | 164,548 |
Tags: greedy, implementation, math
Correct Solution:
```
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
b=dict()
for i in c.keys():
b[i]=0
d = list(sorted((Counter(range(1,n+1))-c).keys()))
r = n-len(c)
j=0
print(r)
for i in range(n):
if j==r:
break
if c[a[i]]>1:
if d[j]<a[i]:
c[a[i]]-=1
a[i]=d[j]
j+=1
else:
if b[a[i]]!=0:
c[a[i]]-=1
a[i]=d[j]
j+=1
else:
b[a[i]]+=1
print(*a)
``` | output | 1 | 82,274 | 12 | 164,549 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal β compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
Input
The first line contains an single integer n (2 β€ n β€ 200 000) β the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ n) β the description of Ivan's array.
Output
In the first line print q β the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
Examples
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1
Input
10
6 8 4 6 7 1 6 3 4 5
Output
3
2 8 4 6 7 1 9 3 10 5
Note
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers β this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation.
Submitted Solution:
```
# Why do we fall ? So we can learn to pick ourselves up.
from collections import Counter
n = int(input())
a = [int(i) for i in input().split()]
c = Counter(a)
left = [int(i) for i in range(1,n+1) if i not in c]
ll,cc = 0,0
visited = [0]*(n+1)
for i in range(0,n):
if c[a[i]]>1:
if left[ll] < a[i] or visited[a[i]]:
c[a[i]] -= 1
a[i] = left[ll]
cc += 1
ll += 1
else:
visited[a[i]] = 1
print(cc)
print(*a)
``` | instruction | 0 | 82,275 | 12 | 164,550 |
Yes | output | 1 | 82,275 | 12 | 164,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal β compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
Input
The first line contains an single integer n (2 β€ n β€ 200 000) β the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ n) β the description of Ivan's array.
Output
In the first line print q β the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
Examples
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1
Input
10
6 8 4 6 7 1 6 3 4 5
Output
3
2 8 4 6 7 1 9 3 10 5
Note
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers β this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation.
Submitted Solution:
```
n=int(input())
arr=list(map(int,input().strip().split(' ')))
a=[0 for i in range(200001)]
b=[0 for i in range(200001)]
for i in arr:
a[i]+=1
j=1
t=0
for i in range(1,n+1):
if(a[i]>0):
t+=1
for i in range(n):
if(a[arr[i]]>1):
while(a[j]!=0):
j+=1
if(arr[i]>j or b[arr[i]]==1 ):
a[arr[i]]-=1
arr[i]=j
a[j]=1
else:
b[arr[i]]=1
print(n-t)
for i in arr:
print(i,end=' ')
print()
``` | instruction | 0 | 82,276 | 12 | 164,552 |
Yes | output | 1 | 82,276 | 12 | 164,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal β compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
Input
The first line contains an single integer n (2 β€ n β€ 200 000) β the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ n) β the description of Ivan's array.
Output
In the first line print q β the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
Examples
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1
Input
10
6 8 4 6 7 1 6 3 4 5
Output
3
2 8 4 6 7 1 9 3 10 5
Note
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers β this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation.
Submitted Solution:
```
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
need = sorted(set(range(1, n + 1)) - set(a))
C = Counter(a)
was = [False for el in range(n + 1)]
c = 0
ans = list()
for i in range(n):
if C[a[i]] == 1:
ans.append(a[i])
C[a[i]] -= 1
was[a[i]] = True
elif len(need):
m = need[c]
if a[i] > m or was[a[i]]:
ans.append(m)
c += 1
C[a[i]] -= 1
else:
ans.append(a[i])
was[a[i]] = True
print(n - len(set(a)))
print(' '.join(map(str, ans)))
``` | instruction | 0 | 82,277 | 12 | 164,554 |
Yes | output | 1 | 82,277 | 12 | 164,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal β compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
Input
The first line contains an single integer n (2 β€ n β€ 200 000) β the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ n) β the description of Ivan's array.
Output
In the first line print q β the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
Examples
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1
Input
10
6 8 4 6 7 1 6 3 4 5
Output
3
2 8 4 6 7 1 9 3 10 5
Note
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers β this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation.
Submitted Solution:
```
n=int(input())
l=list(map(int,input().split()))
L=[0]*200001
L1=[0]*200001
p=0
k=0
for i in range(n) :
L[l[i]]+=1
for i in range(1,n+1) :
if L[i]==0 :
for j in range(p,n) :
if i<l[j] and L[l[j]]>1 :
L[l[j]]-=1
k=k+1
l[j]=i
break
if i>l[j] and L[l[j]]>1 and L1[l[j]]==1 :
L[l[j]]-=1
k=k+1
l[j]=i
break
if i>l[j] and L[l[j]]>1 and L1[l[j]]!=1 :
L1[l[j]]=1
p=j+1
print(k)
print(*l)
``` | instruction | 0 | 82,278 | 12 | 164,556 |
Yes | output | 1 | 82,278 | 12 | 164,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal β compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
Input
The first line contains an single integer n (2 β€ n β€ 200 000) β the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ n) β the description of Ivan's array.
Output
In the first line print q β the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
Examples
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1
Input
10
6 8 4 6 7 1 6 3 4 5
Output
3
2 8 4 6 7 1 9 3 10 5
Note
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers β this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation.
Submitted Solution:
```
import sys
import random
input = sys.stdin.readline
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
def lcm(a, b):
return (a * b) / gcd(a, b)
def main():
n=int(input())
a=list(map(int, input().split()))
isthere=[0]*(n+1)
for i in a:
isthere[i]+=1
have=[]
for i in range(1,n+1):
if not isthere[i]:
have.append(i)
j=0
c=0
for i in range(n):
if isthere[a[i]]>1:
if have[j]<a[i]:
c+=1
isthere[a[i]]-=1
isthere[have[j]]=1
a[i]=have[j]
j+=1
if j!=len(have):
j=len(have)-1
for i in range(n-1,-1,-1):
if isthere[a[i]] > 1:
c += 1
isthere[a[i]] -= 1
isthere[have[j]] = 1
a[i] = have[j]
j -= 1
print(c)
print(*a)
return
if __name__=="__main__":
main()
``` | instruction | 0 | 82,279 | 12 | 164,558 |
No | output | 1 | 82,279 | 12 | 164,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal β compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
Input
The first line contains an single integer n (2 β€ n β€ 200 000) β the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ n) β the description of Ivan's array.
Output
In the first line print q β the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
Examples
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1
Input
10
6 8 4 6 7 1 6 3 4 5
Output
3
2 8 4 6 7 1 9 3 10 5
Note
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers β this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation.
Submitted Solution:
```
n=int(input())
l=[int(i) for i in input().split()]
if len(set(l))==n and min(l)==1 and max(l)==n:
print(0)
print(*l)
exit()
miss=[]
cnt=0
from collections import Counter
c=Counter(l)
for i in range(1,n+1):
if c[i]==0:
miss.append(i)
extra=[]
for i in range(1,n+1):
if c[i]>1:
extra.append([i,c[i]-1])
miss.sort()
for i in range(n):
if c[l[i]]>1:
c[l[i]]-=1
l[i]=miss.pop(0)
cnt+=1
print(cnt)
print(*l)
``` | instruction | 0 | 82,280 | 12 | 164,560 |
No | output | 1 | 82,280 | 12 | 164,561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal β compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
Input
The first line contains an single integer n (2 β€ n β€ 200 000) β the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ n) β the description of Ivan's array.
Output
In the first line print q β the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
Examples
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1
Input
10
6 8 4 6 7 1 6 3 4 5
Output
3
2 8 4 6 7 1 9 3 10 5
Note
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers β this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation.
Submitted Solution:
```
n = int(input()) + 1
t = [0] + list(map(int, input().split()))
s = [0] * n
for j in t: s[j] += 1
p = [0] * n
k = 1
for i, j in enumerate(t):
if s[j] > 1:
while s[k]: k += 1
if j > k or p[j]:
t[i] = k
s[j] -= 1
k += 1
else:
p[i] = 1
print(s.count(0))
print(' '.join(map(str, t[1:])))
``` | instruction | 0 | 82,281 | 12 | 164,562 |
No | output | 1 | 82,281 | 12 | 164,563 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal β compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
Input
The first line contains an single integer n (2 β€ n β€ 200 000) β the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ n) β the description of Ivan's array.
Output
In the first line print q β the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
Examples
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1
Input
10
6 8 4 6 7 1 6 3 4 5
Output
3
2 8 4 6 7 1 9 3 10 5
Note
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers β this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation.
Submitted Solution:
```
n = int(input())
a = [int(x) for x in input().split(" ")]
s = set(a)
cnt = []
need = []
for i in range(1,n+1):
cnt.append(a.count(i))
if cnt[-1] == 0:
need.append(i)
need = sorted(need)
#print(a,cnt)
pos = 0
for i in range(n):
term = a[i]
if cnt[term - 1] > 1:
if need[pos] < term or term in a[:i]:
a[i] = need[pos]
cnt[term - 1] -= 1
cnt[need[pos] - 1] += 1
pos += 1
if set(need) == {1}:
break
#print(a,cnt,i)
for i in a:
print(i, end = " ")
``` | instruction | 0 | 82,282 | 12 | 164,564 |
No | output | 1 | 82,282 | 12 | 164,565 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO | instruction | 0 | 82,645 | 12 | 165,290 |
Tags: dp, greedy
Correct Solution:
```
#!/usr/bin/env python
n, a = int(input()), list(map(int, input().split())) + [0, ]
ans, inc, dec = [0] * n, -1, 1e7
for i in range(n):
if inc < a[i] < dec:
if a[i] < a[i + 1]:
inc = a[i]
else:
dec = a[i]
ans[i] = 1
elif inc < a[i]:
inc = a[i]
elif dec > a[i]:
dec = a[i]
ans[i] = 1
else:
print('NO')
break
else:
print('YES')
print(*ans)
``` | output | 1 | 82,645 | 12 | 165,291 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO | instruction | 0 | 82,646 | 12 | 165,292 |
Tags: dp, greedy
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
inf = 10 ** 6
inc = [inf for i in range(0, n + 1)]
dec = [-inf for i in range(0, n + 1)]
trinc = [-1 for i in range(0, n + 1)]
trdec = [-1 for i in range(0, n + 1)]
inc[0] = -inf
dec[0] = inf
#inc means last dec was in i and we want minimize last inc
for i in range(0, n - 1):
if a[i + 1] < a[i]:
if inc[i + 1] > inc[i]:
inc[i + 1] = inc[i]
trinc[i + 1] = 1
if a[i + 1] > a[i]:
if dec[i + 1] < dec[i]:
dec[i + 1] = dec[i]
trdec[i + 1] = 1
if a[i + 1] > inc[i]:
if dec[i + 1] < a[i]:
dec[i + 1] = a[i]
trdec[i + 1] = 0
if a[i + 1] < dec[i]:
if inc[i + 1] > a[i]:
inc[i + 1] = a[i]
trinc[i + 1] = 0
if inc[n - 1] == inf and dec[n - 1] == -inf:
print("NO")
exit(0)
now_inc = False
ans = [0 for i in range(0, n)]
if inc[n - 1] != inf:
now_inc = True
for i in range(n - 1, -1, -1):
if now_inc:
ans[i] = 1
if trinc[i] == 0:
now_inc = False
else:
ans[i] = 0
if trdec[i] == 0:
now_inc = True
print("YES")
print(" ".join(str(x) for x in ans))
``` | output | 1 | 82,646 | 12 | 165,293 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO | instruction | 0 | 82,647 | 12 | 165,294 |
Tags: dp, greedy
Correct Solution:
```
n = int(input())
a = list(map(int, input().split())) + [0]
answer = [0] * n
current_increase_max = -1
current_decrease_min = 1e6
for i in range(n):
if current_increase_max < a[i] and a[i] < current_decrease_min:
if a[i] < a[i + 1]:
current_increase_max = a[i]
answer[i] = 0
else:
current_decrease_min = a[i]
answer[i] = 1
elif current_increase_max < a[i]:
current_increase_max = a[i]
answer[i] = 0
elif current_decrease_min > a[i]:
current_decrease_min = a[i]
answer[i] = 1
else:
print("NO")
exit(0)
print("YES")
print(*answer)
``` | output | 1 | 82,647 | 12 | 165,295 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO | instruction | 0 | 82,648 | 12 | 165,296 |
Tags: dp, greedy
Correct Solution:
```
n, a = int(input()), [int(i) for i in input().split()] + [0]
ans, inc, dec = [0 for _ in range(n)], -1,float('inf')
for i in range(n):
if inc < a[i] < dec:
if a[i] < a[i+1]:
inc = a[i]
else:
dec = a[i]
ans[i] = 1
elif inc < a[i]:
inc = a[i]
elif dec > a[i]:
dec = a[i]
ans[i] = 1
else:
print('NO')
break
else:
print('YES')
print(*ans)
``` | output | 1 | 82,648 | 12 | 165,297 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO | instruction | 0 | 82,649 | 12 | 165,298 |
Tags: dp, greedy
Correct Solution:
```
def main():
n = int(input())
a = list(map(int, input().split()))
result = [0] * n
last_increasing = -1
last_decreasing = 200001
for i, x in enumerate(a):
if i == n - 1:
if x < last_decreasing:
result[i] = 1
elif x <= last_increasing:
print('NO')
return
else:
if last_increasing >= x >= last_decreasing:
print('NO')
return
if x > last_increasing and x >= last_decreasing:
last_increasing = x
if x <= last_increasing and x < last_decreasing:
last_decreasing = x
result[i] = 1
if last_increasing < x < last_decreasing:
if a[i + 1] > x:
last_increasing = x
else:
last_decreasing = x
result[i] = 1
print('YES')
print(' '.join(map(str, result)))
if __name__ == '__main__':
main()
``` | output | 1 | 82,649 | 12 | 165,299 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO | instruction | 0 | 82,650 | 12 | 165,300 |
Tags: dp, greedy
Correct Solution:
```
from collections import defaultdict as dd
import math
def nn():
return int(input())
def li():
return list(input())
def mi():
return map(int, input().split())
def lm():
return list(map(int, input().split()))
def getanswers():
n=nn()
seq=lm()
lower=None
upper=None
answers=[]
for i in range(n-1):
if seq[i+1]>seq[i]:
if lower==None or seq[i]>lower:
lower=seq[i]
answers.append(0)
elif upper==None or seq[i]<upper:
upper=seq[i]
answers.append(1)
else:
print('No')
return
if seq[i+1]<=seq[i]:
if upper==None or seq[i]<upper:
upper=seq[i]
answers.append(1)
elif upper==None or seq[i]>lower:
lower=seq[i]
answers.append(0)
else:
print('No')
return
i=n-1
if upper==None or seq[i]<upper:
upper=seq[i]
answers.append(1)
elif lower==None or seq[i]>lower:
lower=seq[i]
answers.append(0)
else:
print('No')
return
print('Yes')
print(*answers)
return
getanswers()
``` | output | 1 | 82,650 | 12 | 165,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO | instruction | 0 | 82,651 | 12 | 165,302 |
Tags: dp, greedy
Correct Solution:
```
import math
def main():
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print('YES')
print(0)
else:
res = [0] * n
ok = True
last_inc = -math.inf
last_dec = math.inf
for i in range(n):
if a[i] > last_inc and a[i] >= last_dec:
res[i] = 0
last_inc = a[i]
elif a[i] < last_dec and a[i] <= last_inc:
res[i] = 1
last_dec = a[i]
elif last_dec <= a[i] <= last_inc:
ok = False
break
else:
if i < n-1:
next_val = a[i+1]
if next_val >= a[i]:
res[i] = 0
last_inc = a[i]
else:
res[i] = 1
last_dec = a[i]
else:
res[i] = 0
last_inc = a[i]
if not ok:
print('NO')
else:
print('YES')
print(' '.join(map(str, res)))
if __name__ == '__main__':
main()
``` | output | 1 | 82,651 | 12 | 165,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO | instruction | 0 | 82,652 | 12 | 165,304 |
Tags: dp, greedy
Correct Solution:
```
n, a = int(input()), [int(ai) for ai in input().split()] + [0]
res, inc, dec = [0] * n, -1, 10**6
for i in range(n):
if inc < a[i] and a[i] < dec:
if a[i] < a[i + 1]:
inc = a[i]
else:
dec = a[i]
res[i] = 1
elif inc < a[i]:
inc = a[i]
elif dec > a[i]:
dec = a[i]
res[i] = 1
else:
print('NO')
break
else:
print('YES')
print(*res)
``` | output | 1 | 82,652 | 12 | 165,305 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO
Submitted Solution:
```
n, a = int(input()), list(map(int, input().split())) + [0, ]
ans, inc, dec = [0] * n, -1, 1e7
for i in range(n):
if inc < a[i] < dec:
if a[i] < a[i + 1]:
inc = a[i]
else:
dec = a[i]
ans[i] = 1
elif inc < a[i]:
inc = a[i]
elif dec > a[i]:
dec = a[i]
ans[i] = 1
else:
print('NO')
break
else:
print('YES')
print(*ans)
``` | instruction | 0 | 82,653 | 12 | 165,306 |
Yes | output | 1 | 82,653 | 12 | 165,307 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO
Submitted Solution:
```
# testing https://codeforces.com/contest/1144/submission/52125086
n = int(input())
a = [int(ai) for ai in input().split()] + [0]
inc, dec = -1, 10**6
marker = [0] * n
for i in range(n):
if inc < a[i] and a[i] < dec:
if a[i] < a[i + 1]:
inc = a[i]
else:
dec = a[i]
marker[i] = 1
elif inc < a[i]:
inc = a[i]
elif dec > a[i]:
dec = a[i]
marker[i] = 1
else:
print('NO')
break
else:
print('YES')
print(*marker)
``` | instruction | 0 | 82,654 | 12 | 165,308 |
Yes | output | 1 | 82,654 | 12 | 165,309 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO
Submitted Solution:
```
import sys
import os
def main(args):
states = []
queue = [(min(args)-1,max(args)+1,None,None)]
answer_flag=True
for i in range(len(args)):
# print(queue)
x = args[i]
h_queue = []
l_queue = []
for j in range(len(queue)):
if queue[j][1] is None or x < queue[j][1]:
l_queue.append((queue[j][0], x, 1, j))
else:
break
for j in range(len(queue)-1, -1, -1):
if queue[j][0] is None or x > queue[j][0]:
h_queue.append((x, queue[j][1], 0, j))
else:
break
queue = []
if len(h_queue) > 0:
queue.append(h_queue[-1])
if len(l_queue) > 0:
queue.append(l_queue[-1])
if len(queue) > 1:
if queue[0][0] < queue[1][0]:
t = queue[0]
queue[0]=queue[1]
queue[1]=t
if len(queue) == 0:
answer_flag=False
break
states.append(queue)
if answer_flag:
print("YES")
else:
print("NO")
if answer_flag:
p = 0
result = [None for i in range(len(states))]
for i in range(len(states) -1, -1, -1):
result[i] = states[i][p][2]
p = states[i][p][3]
print(" ".join(map(str, result)))
input()
args=list(map(int,input().strip().split()))
main(args)
``` | instruction | 0 | 82,655 | 12 | 165,310 |
Yes | output | 1 | 82,655 | 12 | 165,311 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO
Submitted Solution:
```
from collections import defaultdict as dd
import math
def nn():
return int(input())
def li():
return list(input())
def mi():
return map(int, input().split())
def lm():
return list(map(int, input().split()))
def getanswers():
n=nn()
seq=lm()
lower=None
upper=None
answers=[]
for i in range(n-1):
if seq[i+1]>seq[i]:
if lower==None or seq[i]>lower:
lower=seq[i]
answers.append(0)
elif upper==None or seq[i]<upper:
upper=seq[i]
answers.append(1)
else:
return 'No'
if seq[i+1]<=seq[i]:
if upper==None or seq[i]<upper:
upper=seq[i]
answers.append(1)
elif upper==None or seq[i]>lower:
lower=seq[i]
answers.append(0)
else:
return 'No'
i=n-1
if upper==None or seq[i]<upper:
upper=seq[i]
answers.append(1)
elif lower==None or seq[i]>lower:
lower=seq[i]
answers.append(0)
else:
return 'No'
return answers
print(getanswers())
``` | instruction | 0 | 82,656 | 12 | 165,312 |
No | output | 1 | 82,656 | 12 | 165,313 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO
Submitted Solution:
```
from collections import defaultdict as dd
import math
def nn():
return int(input())
def li():
return list(input())
def mi():
return map(int, input().split())
def lm():
return list(map(int, input().split()))
def getanswers():
n=nn()
seq=lm()
lower=None
upper=None
answers=[]
for i in range(n-1):
if seq[i+1]>seq[i]:
if lower==None or seq[i]>lower:
lower=seq[i]
answers.append(0)
elif upper==None or seq[i]<upper:
upper=seq[i]
answers.append(1)
else:
print('No')
return
if seq[i+1]<=seq[i]:
if upper==None or seq[i]<upper:
upper=seq[i]
answers.append(1)
elif upper==None or seq[i]>lower:
lower=seq[i]
answers.append(0)
else:
print('No')
return
i=n-1
if upper==None or seq[i]<upper:
upper=seq[i]
answers.append(1)
elif lower==None or seq[i]>lower:
lower=seq[i]
answers.append(0)
else:
print('No')
return
print(*answers)
return
getanswers()
``` | instruction | 0 | 82,657 | 12 | 165,314 |
No | output | 1 | 82,657 | 12 | 165,315 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO
Submitted Solution:
```
def main():
n = int(input())
a = list(map(int, input().split()))
result = [0] * n
last_increasing = -1
last_decreasing = 200001
for i, x in enumerate(a):
if i == n - 1:
if x < last_decreasing:
result[i] = 1
elif x <= last_increasing:
print('NO')
return
else:
if last_increasing >= x >= last_decreasing:
print('NO')
return
if x > last_increasing and x > last_decreasing:
last_increasing = x
if x < last_increasing and x < last_decreasing:
last_decreasing = x
result[i] = 1
if last_increasing < x < last_decreasing:
if a[i + 1] > x:
last_increasing = x
else:
last_decreasing = x
result[i] = 1
print('YES')
print(' '.join(map(str, result)))
if __name__ == '__main__':
main()
``` | instruction | 0 | 82,658 | 12 | 165,316 |
No | output | 1 | 82,658 | 12 | 165,317 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially, one of them was strictly increasing, and another one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2]. The following sequence cannot be the result of these insertions: [1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.
Let the obtained sequence be a. This sequence a is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of n integers res_1, res_2, ..., res_n, where res_i should be either 0 or 1 for each i from 1 to n. The i-th element of this sequence should be 0 if the i-th element of a belongs to the increasing sequence, and 1 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Examples
Input
9
5 1 3 6 8 2 9 0 10
Output
YES
1 0 0 0 0 1 0 1 0
Input
5
1 2 4 0 2
Output
NO
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
inf = 10 ** 6
inc = [inf for i in range(0, n + 1)]
dec = [-inf for i in range(0, n + 1)]
trinc = [-1 for i in range(0, n + 1)]
trdec = [-1 for i in range(0, n + 1)]
inc[0] = -inf
dec[0] = inf
#inc means last dec was in i and we want minimize last inc
for i in range(0, n - 1):
if a[i + 1] < a[i]:
if inc[i + 1] > inc[i]:
inc[i + 1] = inc[i]
trinc[i + 1] = 1
if a[i + 1] > a[i]:
if dec[i + 1] < dec[i]:
dec[i + 1] = dec[i]
trdec[i + 1] = 1
if a[i + 1] > inc[i]:
if dec[i + 1] < a[i]:
dec[i + 1] = a[i]
trdec[i + 1] = 0
if a[i + 1] < dec[i]:
if inc[i + 1] > a[i]:
inc[i + 1] = a[i]
trinc[i + 1] = 0
if inc[n - 1] == inf and dec[n - 1] == -inf:
print("NO")
exit(0)
now_inc = False
ans = [0 for i in range(0, n)]
if inc[n - 1] != inf:
now_inc = True
for i in range(n - 1, -1, -1):
if now_inc:
ans[i] = 0
if trinc[i] == 0:
now_inc = False
else:
ans[i] = 1
if trdec[i] == 0:
now_inc = True
print("YES")
print(" ".join(str(x) for x in ans))
``` | instruction | 0 | 82,659 | 12 | 165,318 |
No | output | 1 | 82,659 | 12 | 165,319 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2 | instruction | 0 | 82,745 | 12 | 165,490 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n = input()
li = list(map(int, input().split(" ")))
li.sort()
if li[-1] != 1: li.insert(0, 1)
else: li.insert(0, 2)
del li[-1]
li.sort()
print(" ".join(map(str, li)))
``` | output | 1 | 82,745 | 12 | 165,491 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2 | instruction | 0 | 82,746 | 12 | 165,492 |
Tags: greedy, implementation, sortings
Correct Solution:
```
def solve():
n=int(input())
l=[int(x) for x in input().split()]
if max(l)==1:
l[0]=2
l.sort()
for num in l:print(num,end=" ")
else:
l[l.index(max(l))]=1
l.sort()
for num in l:print(num,end=" ")
# n=int(input())
# while n>0:
# solve()
# n-=1
solve()
``` | output | 1 | 82,746 | 12 | 165,493 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2 | instruction | 0 | 82,747 | 12 | 165,494 |
Tags: greedy, implementation, sortings
Correct Solution:
```
def solve(array):
if max(array) == 1:
return array[:-1] + [2]
array.sort()
return [1] + array[:-1]
if __name__ == "__main__":
n = int(input())
array = [int(x) for x in input().split()]
print(" ".join(map(str, solve(array))))
``` | output | 1 | 82,747 | 12 | 165,495 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2 | instruction | 0 | 82,748 | 12 | 165,496 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
if(len(set(a))==1 and a[0]==1):
a[0]=2
a.sort()
print(" ".join(map(str,a)))
else:
a[a.index(max(a))]=1
a.sort()
print(" ".join(map(str,a)))
``` | output | 1 | 82,748 | 12 | 165,497 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2 | instruction | 0 | 82,749 | 12 | 165,498 |
Tags: greedy, implementation, sortings
Correct Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
#vsInput()
n=Int()
a=sorted(array())
if(a[-1]!=1): print(1,*a[:-1])
else: print('1 '*(n-1),2,sep="")
``` | output | 1 | 82,749 | 12 | 165,499 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2 | instruction | 0 | 82,750 | 12 | 165,500 |
Tags: greedy, implementation, sortings
Correct Solution:
```
from sys import stdin, stdout
n = int(input())
t = list(map(int, stdin.readline().split()))
t= sorted(t)
m = t[n-1]
t.pop(n-1)
i = 1
while i == m:
i+=1
t.insert(0, i)
t= sorted(t)
for u in t:
stdout.write(str(u)+" ")
``` | output | 1 | 82,750 | 12 | 165,501 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2 | instruction | 0 | 82,751 | 12 | 165,502 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n=int(input())
l=list(map(int,input().split()))
if (len(l)==l.count(max(l)) and max(l)==1) or (len(l)==1 and l[0]==1):
l.remove(l[0])
l.append(2)
else:
l.remove(max(l))
if n!=1:
l.sort()
l=[1]+l
for i in l:
print(i,end=" ")
``` | output | 1 | 82,751 | 12 | 165,503 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2 | instruction | 0 | 82,752 | 12 | 165,504 |
Tags: greedy, implementation, sortings
Correct Solution:
```
def main():
n=int(input())
L=list(map(int,input().split()))
if max(L)==1:
L[L.index(max(L))]=2
else:
L[L.index(max(L))]=1
L.sort()
print(' '.join(map(str, L)))
main()
``` | output | 1 | 82,752 | 12 | 165,505 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2
Submitted Solution:
```
import sys
input = sys.stdin.readline
out = sys.stdout
n = int(input())
a = list(map(int,input().split()))
if max(a) == 1:
a[a.index(max(a))] = 2
else:
a[a.index(max(a))] = 1
a.sort()
for i in a:
out.write(str(i) + ' ')
``` | instruction | 0 | 82,753 | 12 | 165,506 |
Yes | output | 1 | 82,753 | 12 | 165,507 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2
Submitted Solution:
```
n = int(input())
arr = list(map(int, input().split()))
v = max(arr)
i = arr.index(v)
arr[i] = 2 if v == 1 else 1
print(" ".join(map(str, sorted(arr))))
``` | instruction | 0 | 82,754 | 12 | 165,508 |
Yes | output | 1 | 82,754 | 12 | 165,509 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2
Submitted Solution:
```
n = int(input())
A = list(map(int, input().split()))
if max(A) == 1:
A[A.index(max(A))] = 2
else:
A[A.index(max(A))] = 1
A = sorted(A)
for i in A:
print(i, end = ' ')
``` | instruction | 0 | 82,755 | 12 | 165,510 |
Yes | output | 1 | 82,755 | 12 | 165,511 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2
Submitted Solution:
```
input()
a=sorted(list(map(int,input().split())))
a[-1]=[1,2][a[-1]==1]
print(*(sorted(a)))
``` | instruction | 0 | 82,756 | 12 | 165,512 |
Yes | output | 1 | 82,756 | 12 | 165,513 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2
Submitted Solution:
```
n = input()
li = list(map(int, input().split(" ")))
li.sort; del li[-1]; li.insert(0, 1)
print(" ".join(map(str, li)))
``` | instruction | 0 | 82,757 | 12 | 165,514 |
No | output | 1 | 82,757 | 12 | 165,515 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2
Submitted Solution:
```
n,inp = int(input()), [int(i) for i in input().split(" ")]
if min(inp) == max(inp): inp[inp.index(max(inp))] = max(inp) + 1
else: inp[inp.index(max(inp))] = 1
inp.sort()
print(" ".join(map(str,inp)))
``` | instruction | 0 | 82,758 | 12 | 165,516 |
No | output | 1 | 82,758 | 12 | 165,517 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
#vsInput()
n=Int()
a=array()
print(1,*a[:-1])
``` | instruction | 0 | 82,759 | 12 | 165,518 |
No | output | 1 | 82,759 | 12 | 165,519 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all.
After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.
Input
The first line contains a single integer n (1 β€ n β€ 105), which represents how many numbers the array has. The next line contains n space-separated integers β the array's description. All elements of the array lie in the range from 1 to 109, inclusive.
Output
Print n space-separated integers β the minimum possible values of each array element after one replacement and the sorting are performed.
Examples
Input
5
1 2 3 4 5
Output
1 1 2 3 4
Input
5
2 3 4 5 6
Output
1 2 3 4 5
Input
3
2 2 2
Output
1 2 2
Submitted Solution:
```
n = int(input())
seq = list(map(int,input().split()))
seq.sort()
res = "1"
for i in seq[:-1]:
res += " " + str(i)
print(res)
``` | instruction | 0 | 82,760 | 12 | 165,520 |
No | output | 1 | 82,760 | 12 | 165,521 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a square matrix of size n. Every row and every column of this matrix is a permutation of 1, 2, β¦, n. Let a_{i, j} be the element at the intersection of i-th row and j-th column for every 1 β€ i, j β€ n. Rows are numbered 1, β¦, n top to bottom, and columns are numbered 1, β¦, n left to right.
There are six types of operations:
* R: cyclically shift all columns to the right, formally, set the value of each a_{i, j} to a_{i, ((j - 2)mod n) + 1};
* L: cyclically shift all columns to the left, formally, set the value of each a_{i, j} to a_{i, (jmod n) + 1};
* D: cyclically shift all rows down, formally, set the value of each a_{i, j} to a_{((i - 2)mod n) + 1, j};
* U: cyclically shift all rows up, formally, set the value of each a_{i, j} to a_{(imod n) + 1, j};
* I: replace the permutation read left to right in each row with its inverse.
* C: replace the permutation read top to bottom in each column with its inverse.
Inverse of a permutation p_1, p_2, β¦, p_n is a permutation q_1, q_2, β¦, q_n, such that p_{q_i} = i for every 1 β€ i β€ n.
One can see that after any sequence of operations every row and every column of the matrix will still be a permutation of 1, 2, β¦, n.
Given the initial matrix description, you should process m operations and output the final matrix.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β number of test cases. t test case descriptions follow.
The first line of each test case description contains two integers n and m (1 β€ n β€ 1000, 1 β€ m β€ 10^5) β size of the matrix and number of operations.
Each of the next n lines contains n integers separated by single spaces β description of the matrix a (1 β€ a_{i, j} β€ n).
The last line of the description contains a string of m characters describing the operations in order, according to the format above.
The sum of n does not exceed 1000, and the sum of m does not exceed 10^5.
Output
For each test case, print n lines with n integers each β the final matrix after m operations.
Example
Input
5
3 2
1 2 3
2 3 1
3 1 2
DR
3 2
1 2 3
2 3 1
3 1 2
LU
3 1
1 2 3
2 3 1
3 1 2
I
3 1
1 2 3
2 3 1
3 1 2
C
3 16
1 2 3
2 3 1
3 1 2
LDICRUCILDICRUCI
Output
2 3 1
3 1 2
1 2 3
3 1 2
1 2 3
2 3 1
1 2 3
3 1 2
2 3 1
1 3 2
2 1 3
3 2 1
2 3 1
3 1 2
1 2 3
Note
Line breaks between sample test case answers are only for clarity, and don't have to be printed. | instruction | 0 | 82,793 | 12 | 165,586 |
Tags: math, matrices
Correct Solution:
```
import sys,io,os;input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
t = int(input())
out = []
for _ in range(t):
n, m = map(int, input().split())
MAT = [list(map(int, input().split())) for i in range(n)]
dx = 0
dy = 0
order = [0,1,2]
d = [0,0,0]
s = input()
for c in s:
if c == ord('R'):
d[1] += 1
elif c == ord('L'):
d[1] -= 1
elif c == ord('D'):
d[0] += 1
elif c == ord('U'):
d[0] -= 1
elif c == ord('I'):
order[1],order[2]=order[2],order[1]
d[1],d[2]=d[2],d[1]
elif c == ord('C'):
order[0],order[2]=order[2],order[0]
d[0],d[2]=d[2],d[0]
om = [[0] * n for i in range(n)]
for i in range(n):
for j in range(n):
r, c, v = i, j, MAT[i][j] - 1
trip = [-1,-1,-1]
for u in range(3):
trip[u] = [r,c,v][order[u]]
trip[u] += d[u]
trip[u] %= n
om[trip[0]][trip[1]] = trip[2] + 1
for line in om:
out.append(' '.join(map(str,line)))
print('\n'.join(out))
``` | output | 1 | 82,793 | 12 | 165,587 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a square matrix of size n. Every row and every column of this matrix is a permutation of 1, 2, β¦, n. Let a_{i, j} be the element at the intersection of i-th row and j-th column for every 1 β€ i, j β€ n. Rows are numbered 1, β¦, n top to bottom, and columns are numbered 1, β¦, n left to right.
There are six types of operations:
* R: cyclically shift all columns to the right, formally, set the value of each a_{i, j} to a_{i, ((j - 2)mod n) + 1};
* L: cyclically shift all columns to the left, formally, set the value of each a_{i, j} to a_{i, (jmod n) + 1};
* D: cyclically shift all rows down, formally, set the value of each a_{i, j} to a_{((i - 2)mod n) + 1, j};
* U: cyclically shift all rows up, formally, set the value of each a_{i, j} to a_{(imod n) + 1, j};
* I: replace the permutation read left to right in each row with its inverse.
* C: replace the permutation read top to bottom in each column with its inverse.
Inverse of a permutation p_1, p_2, β¦, p_n is a permutation q_1, q_2, β¦, q_n, such that p_{q_i} = i for every 1 β€ i β€ n.
One can see that after any sequence of operations every row and every column of the matrix will still be a permutation of 1, 2, β¦, n.
Given the initial matrix description, you should process m operations and output the final matrix.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β number of test cases. t test case descriptions follow.
The first line of each test case description contains two integers n and m (1 β€ n β€ 1000, 1 β€ m β€ 10^5) β size of the matrix and number of operations.
Each of the next n lines contains n integers separated by single spaces β description of the matrix a (1 β€ a_{i, j} β€ n).
The last line of the description contains a string of m characters describing the operations in order, according to the format above.
The sum of n does not exceed 1000, and the sum of m does not exceed 10^5.
Output
For each test case, print n lines with n integers each β the final matrix after m operations.
Example
Input
5
3 2
1 2 3
2 3 1
3 1 2
DR
3 2
1 2 3
2 3 1
3 1 2
LU
3 1
1 2 3
2 3 1
3 1 2
I
3 1
1 2 3
2 3 1
3 1 2
C
3 16
1 2 3
2 3 1
3 1 2
LDICRUCILDICRUCI
Output
2 3 1
3 1 2
1 2 3
3 1 2
1 2 3
2 3 1
1 2 3
3 1 2
2 3 1
1 3 2
2 1 3
3 2 1
2 3 1
3 1 2
1 2 3
Note
Line breaks between sample test case answers are only for clarity, and don't have to be printed. | instruction | 0 | 82,794 | 12 | 165,588 |
Tags: math, matrices
Correct Solution:
```
import sys,io,os
Z=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
Y=lambda:[*map(int,Z().split())]
def I(M):
n=len(M)
nM=[[0]*n for i in range(n)]
for x in range(n):
for y in range(n):
nM[x][M[x][y]-1]=y+1
return nM
def C(M):
n=len(M)
nM=[[0]*n for i in range(n)]
for x in range(n):
for y in range(n):
nM[M[x][y]-1][y]=x+1
return nM
def T(M):
n=len(M)
nM=[[0]*n for i in range(n)]
for x in range(n):
for y in range(n):
nM[x][y]=M[y][x]
return nM
o=[]
for _ in range(int(Z())):
n,m=Y();M=[]
for i in range(n):M.append(Y())
s=Z().strip()
s=s[::-1]
dx=dy=dv=0
ui=t=0
for i in s:
if ui==67:
if i==68:i=65
elif i==85:i=83
elif i==73:i=84
elif i==67:ui=i=0
elif ui==73:
if i==82:i=65
elif i==76:i=83
elif i==67:i=84
elif i==73:ui=i=0
if t:
if i==82:i=68
elif i==76:i=85
elif i==68:i=82
elif i==85:i=76
if i==82:dx+=1
elif i==76:dx-=1
elif i==68:dy+=1
elif i==85:dy-=1
elif i==73:ui=73
elif i==67:ui=67
elif i==84:t=1-t
elif i==83:dv-=1
elif i==65:dv+=1
if ui==67:M=C(M)
elif ui==73:M=I(M)
if t:M=T(M)
nM=[[0]*n for i in range(n)]
for x in range(n):
for y in range(n):
nM[x][y]=(M[(x-dy)%n][(y-dx)%n]-1+dv)%n+1
o.append('\n'.join(' '.join(map(str,r))for r in nM))
print('\n\n'.join(o))
``` | output | 1 | 82,794 | 12 | 165,589 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a square matrix of size n. Every row and every column of this matrix is a permutation of 1, 2, β¦, n. Let a_{i, j} be the element at the intersection of i-th row and j-th column for every 1 β€ i, j β€ n. Rows are numbered 1, β¦, n top to bottom, and columns are numbered 1, β¦, n left to right.
There are six types of operations:
* R: cyclically shift all columns to the right, formally, set the value of each a_{i, j} to a_{i, ((j - 2)mod n) + 1};
* L: cyclically shift all columns to the left, formally, set the value of each a_{i, j} to a_{i, (jmod n) + 1};
* D: cyclically shift all rows down, formally, set the value of each a_{i, j} to a_{((i - 2)mod n) + 1, j};
* U: cyclically shift all rows up, formally, set the value of each a_{i, j} to a_{(imod n) + 1, j};
* I: replace the permutation read left to right in each row with its inverse.
* C: replace the permutation read top to bottom in each column with its inverse.
Inverse of a permutation p_1, p_2, β¦, p_n is a permutation q_1, q_2, β¦, q_n, such that p_{q_i} = i for every 1 β€ i β€ n.
One can see that after any sequence of operations every row and every column of the matrix will still be a permutation of 1, 2, β¦, n.
Given the initial matrix description, you should process m operations and output the final matrix.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β number of test cases. t test case descriptions follow.
The first line of each test case description contains two integers n and m (1 β€ n β€ 1000, 1 β€ m β€ 10^5) β size of the matrix and number of operations.
Each of the next n lines contains n integers separated by single spaces β description of the matrix a (1 β€ a_{i, j} β€ n).
The last line of the description contains a string of m characters describing the operations in order, according to the format above.
The sum of n does not exceed 1000, and the sum of m does not exceed 10^5.
Output
For each test case, print n lines with n integers each β the final matrix after m operations.
Example
Input
5
3 2
1 2 3
2 3 1
3 1 2
DR
3 2
1 2 3
2 3 1
3 1 2
LU
3 1
1 2 3
2 3 1
3 1 2
I
3 1
1 2 3
2 3 1
3 1 2
C
3 16
1 2 3
2 3 1
3 1 2
LDICRUCILDICRUCI
Output
2 3 1
3 1 2
1 2 3
3 1 2
1 2 3
2 3 1
1 2 3
3 1 2
2 3 1
1 3 2
2 1 3
3 2 1
2 3 1
3 1 2
1 2 3
Note
Line breaks between sample test case answers are only for clarity, and don't have to be printed. | instruction | 0 | 82,795 | 12 | 165,590 |
Tags: math, matrices
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
t=int(input())
for _ in range(t):
n,m=map(int,input().split())
grid=[]
for i in range(n):
grid.append(list(map(int,input().split())))
s=input()
a=0
b=0
c=0
for i in s:
if i=='R':
b+=1
elif i=='L':
b-=1
elif i=='D':
a+=1
elif i=='U':
a-=1
elif i=='I':
tmp=b
b=c
c=tmp
else:
tmp=a
a=c
c=tmp
pt=[a,b,c]
dest0=0
dest1=1
dest2=2
for i in s:
if i=='I':
tmp=dest1
dest1=dest2
dest2=tmp
elif i=='C':
tmp=dest0
dest0=dest2
dest2=tmp
st=[]
for i in range(3):
if dest0==i:
st.append(0)
if dest1==i:
st.append(1)
if dest2==i:
st.append(2)
ans=[]
for i in range(n):
ans.append([])
for j in range(n):
ans[-1].append(0)
for i in range(n):
for j in range(n):
start=[i,j,grid[i][j]-1]
tmpans=[0,0,0]
for k in range(3):
tmpans[st[k]]=(pt[st[k]]+start[k])%n
ans[tmpans[0]][tmpans[1]]=str(tmpans[2]+1)
for a in ans:
print(' '.join(a))
``` | output | 1 | 82,795 | 12 | 165,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a square matrix of size n. Every row and every column of this matrix is a permutation of 1, 2, β¦, n. Let a_{i, j} be the element at the intersection of i-th row and j-th column for every 1 β€ i, j β€ n. Rows are numbered 1, β¦, n top to bottom, and columns are numbered 1, β¦, n left to right.
There are six types of operations:
* R: cyclically shift all columns to the right, formally, set the value of each a_{i, j} to a_{i, ((j - 2)mod n) + 1};
* L: cyclically shift all columns to the left, formally, set the value of each a_{i, j} to a_{i, (jmod n) + 1};
* D: cyclically shift all rows down, formally, set the value of each a_{i, j} to a_{((i - 2)mod n) + 1, j};
* U: cyclically shift all rows up, formally, set the value of each a_{i, j} to a_{(imod n) + 1, j};
* I: replace the permutation read left to right in each row with its inverse.
* C: replace the permutation read top to bottom in each column with its inverse.
Inverse of a permutation p_1, p_2, β¦, p_n is a permutation q_1, q_2, β¦, q_n, such that p_{q_i} = i for every 1 β€ i β€ n.
One can see that after any sequence of operations every row and every column of the matrix will still be a permutation of 1, 2, β¦, n.
Given the initial matrix description, you should process m operations and output the final matrix.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β number of test cases. t test case descriptions follow.
The first line of each test case description contains two integers n and m (1 β€ n β€ 1000, 1 β€ m β€ 10^5) β size of the matrix and number of operations.
Each of the next n lines contains n integers separated by single spaces β description of the matrix a (1 β€ a_{i, j} β€ n).
The last line of the description contains a string of m characters describing the operations in order, according to the format above.
The sum of n does not exceed 1000, and the sum of m does not exceed 10^5.
Output
For each test case, print n lines with n integers each β the final matrix after m operations.
Example
Input
5
3 2
1 2 3
2 3 1
3 1 2
DR
3 2
1 2 3
2 3 1
3 1 2
LU
3 1
1 2 3
2 3 1
3 1 2
I
3 1
1 2 3
2 3 1
3 1 2
C
3 16
1 2 3
2 3 1
3 1 2
LDICRUCILDICRUCI
Output
2 3 1
3 1 2
1 2 3
3 1 2
1 2 3
2 3 1
1 2 3
3 1 2
2 3 1
1 3 2
2 1 3
3 2 1
2 3 1
3 1 2
1 2 3
Note
Line breaks between sample test case answers are only for clarity, and don't have to be printed.
Submitted Solution:
```
import sys,io,os;input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
t = int(input())
out = []
for _ in range(t):
n, m = map(int, input().split())
MAT = [list(map(int, input().split())) for i in range(n)]
dx = 0
dy = 0
order = [0,1,2]
d = [0,0,0]
s = input()
for c in s:
if c == ord('R'):
d[1] -= 1
elif c == ord('L'):
d[1] += 1
elif c == ord('D'):
d[0] -= 1
elif c == ord('U'):
d[0] += 1
elif c == ord('I'):
order[1],order[2]=order[2],order[1]
d[1],d[2]=d[2],d[1]
elif c == ord('C'):
order[0],order[2]=order[2],order[0]
d[0],d[2]=d[2],d[0]
om = [[0] * n for i in range(n)]
for i in range(n):
for j in range(n):
r, c, v = i, j, MAT[i][j] - 1
trip = [-1,-1,-1]
trip[order[0]] = r
trip[order[1]] = c
trip[order[2]] = v
for u in range(3):
trip[u] -= d[u]
trip[u] %= n
om[trip[0]][trip[1]] = trip[2] + 1
for line in om:
out.append(' '.join(map(str,line)))
print('\n'.join(out))
``` | instruction | 0 | 82,796 | 12 | 165,592 |
No | output | 1 | 82,796 | 12 | 165,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a square matrix of size n. Every row and every column of this matrix is a permutation of 1, 2, β¦, n. Let a_{i, j} be the element at the intersection of i-th row and j-th column for every 1 β€ i, j β€ n. Rows are numbered 1, β¦, n top to bottom, and columns are numbered 1, β¦, n left to right.
There are six types of operations:
* R: cyclically shift all columns to the right, formally, set the value of each a_{i, j} to a_{i, ((j - 2)mod n) + 1};
* L: cyclically shift all columns to the left, formally, set the value of each a_{i, j} to a_{i, (jmod n) + 1};
* D: cyclically shift all rows down, formally, set the value of each a_{i, j} to a_{((i - 2)mod n) + 1, j};
* U: cyclically shift all rows up, formally, set the value of each a_{i, j} to a_{(imod n) + 1, j};
* I: replace the permutation read left to right in each row with its inverse.
* C: replace the permutation read top to bottom in each column with its inverse.
Inverse of a permutation p_1, p_2, β¦, p_n is a permutation q_1, q_2, β¦, q_n, such that p_{q_i} = i for every 1 β€ i β€ n.
One can see that after any sequence of operations every row and every column of the matrix will still be a permutation of 1, 2, β¦, n.
Given the initial matrix description, you should process m operations and output the final matrix.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β number of test cases. t test case descriptions follow.
The first line of each test case description contains two integers n and m (1 β€ n β€ 1000, 1 β€ m β€ 10^5) β size of the matrix and number of operations.
Each of the next n lines contains n integers separated by single spaces β description of the matrix a (1 β€ a_{i, j} β€ n).
The last line of the description contains a string of m characters describing the operations in order, according to the format above.
The sum of n does not exceed 1000, and the sum of m does not exceed 10^5.
Output
For each test case, print n lines with n integers each β the final matrix after m operations.
Example
Input
5
3 2
1 2 3
2 3 1
3 1 2
DR
3 2
1 2 3
2 3 1
3 1 2
LU
3 1
1 2 3
2 3 1
3 1 2
I
3 1
1 2 3
2 3 1
3 1 2
C
3 16
1 2 3
2 3 1
3 1 2
LDICRUCILDICRUCI
Output
2 3 1
3 1 2
1 2 3
3 1 2
1 2 3
2 3 1
1 2 3
3 1 2
2 3 1
1 3 2
2 1 3
3 2 1
2 3 1
3 1 2
1 2 3
Note
Line breaks between sample test case answers are only for clarity, and don't have to be printed.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
t=int(input())
for _ in range(t):
n,m=map(int,input().split())
grid=[]
for i in range(n):
grid.append(list(map(int,input().split())))
s=input()
a=0
b=0
c=0
for i in s:
if i=='R':
a+=1
elif i=='L':
a-=1
elif i=='D':
b+=1
elif i=='U':
b-=1
elif i=='I':
tmp=a
a=c
c=tmp
else:
tmp=b
b=c
c=tmp
pt=[a,b,c]
dest0=0
dest1=1
dest2=2
for i in s:
if i=='I':
tmp=dest0
dest0=dest2
dest2=tmp
elif i=='C':
tmp=dest1
dest1=dest2
dest2=tmp
st=[]
for i in range(3):
if dest0==i:
st.append(0)
if dest1==i:
st.append(1)
if dest2==i:
st.append(2)
ans=[]
for i in range(n):
ans.append([])
for j in range(n):
ans[-1].append(0)
for i in range(n):
for j in range(n):
start=[i,j,grid[i][j]-1]
tmpans=[0,0,0]
for k in range(3):
tmpans[st[k]]=(pt[st[k]]+start[k])%n
ans[tmpans[0]][tmpans[1]]=str(tmpans[2]+1)
for a in ans:
print(' '.join(a))
``` | instruction | 0 | 82,797 | 12 | 165,594 |
No | output | 1 | 82,797 | 12 | 165,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a square matrix of size n. Every row and every column of this matrix is a permutation of 1, 2, β¦, n. Let a_{i, j} be the element at the intersection of i-th row and j-th column for every 1 β€ i, j β€ n. Rows are numbered 1, β¦, n top to bottom, and columns are numbered 1, β¦, n left to right.
There are six types of operations:
* R: cyclically shift all columns to the right, formally, set the value of each a_{i, j} to a_{i, ((j - 2)mod n) + 1};
* L: cyclically shift all columns to the left, formally, set the value of each a_{i, j} to a_{i, (jmod n) + 1};
* D: cyclically shift all rows down, formally, set the value of each a_{i, j} to a_{((i - 2)mod n) + 1, j};
* U: cyclically shift all rows up, formally, set the value of each a_{i, j} to a_{(imod n) + 1, j};
* I: replace the permutation read left to right in each row with its inverse.
* C: replace the permutation read top to bottom in each column with its inverse.
Inverse of a permutation p_1, p_2, β¦, p_n is a permutation q_1, q_2, β¦, q_n, such that p_{q_i} = i for every 1 β€ i β€ n.
One can see that after any sequence of operations every row and every column of the matrix will still be a permutation of 1, 2, β¦, n.
Given the initial matrix description, you should process m operations and output the final matrix.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β number of test cases. t test case descriptions follow.
The first line of each test case description contains two integers n and m (1 β€ n β€ 1000, 1 β€ m β€ 10^5) β size of the matrix and number of operations.
Each of the next n lines contains n integers separated by single spaces β description of the matrix a (1 β€ a_{i, j} β€ n).
The last line of the description contains a string of m characters describing the operations in order, according to the format above.
The sum of n does not exceed 1000, and the sum of m does not exceed 10^5.
Output
For each test case, print n lines with n integers each β the final matrix after m operations.
Example
Input
5
3 2
1 2 3
2 3 1
3 1 2
DR
3 2
1 2 3
2 3 1
3 1 2
LU
3 1
1 2 3
2 3 1
3 1 2
I
3 1
1 2 3
2 3 1
3 1 2
C
3 16
1 2 3
2 3 1
3 1 2
LDICRUCILDICRUCI
Output
2 3 1
3 1 2
1 2 3
3 1 2
1 2 3
2 3 1
1 2 3
3 1 2
2 3 1
1 3 2
2 1 3
3 2 1
2 3 1
3 1 2
1 2 3
Note
Line breaks between sample test case answers are only for clarity, and don't have to be printed.
Submitted Solution:
```
import sys,io,os
Z=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
Y=lambda:[*map(int,Z().split())]
def I(M):
n=len(M)
nM=[[0]*n for i in range(n)]
for x in range(n):
for y in range(n):
nM[x][M[x][y]-1]=y+1
return nM
def C(M):
n=len(M)
nM=[[0]*n for i in range(n)]
for x in range(n):
for y in range(n):
nM[M[x][y]-1][y]=x+1
return nM
def T(M):
n=len(M)
nM=[[0]*n for i in range(n)]
for x in range(n):
for y in range(n):
nM[x][y]=M[y][x]
return nM
o=[]
for _ in range(int(Z())):
n,m=Y();M=[]
for i in range(n):M.append(Y())
s=Z().strip()
dx=dy=dv=0
ui=t=0
for i in s:
if ui==67:
if i==68:i=65
elif i==85:i=83
elif i==73:i=84
elif i==67:ui=i=0
elif ui==73:
if i==82:i=65
elif i==76:i=83
elif i==67:i=84
elif i==73:ui=i=0
if t:
if i==82:i=68
elif i==76:i=85
elif i==68:i=82
elif i==85:i=76
if i==82:dx+=1
elif i==76:dx-=1
elif i==68:dy+=1
elif i==85:dy-=1
elif i==73:ui=73
elif i==67:ui=67
elif i==84:t=1-t
elif i==83:dv-=1
elif i==65:dv+=1
if ui==67:M=C(M)
elif ui==73:M=I(M)
if t:M=T(M)
nM=[[0]*n for i in range(n)]
for x in range(n):
for y in range(n):
nM[x][y]=(M[(x-dx)%n][(y-dy)%n]-1+dv)%n+1
o.append('\n'.join(' '.join(map(str,r))for r in nM))
print('\n\n'.join(o))
``` | instruction | 0 | 82,798 | 12 | 165,596 |
No | output | 1 | 82,798 | 12 | 165,597 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a square matrix of size n. Every row and every column of this matrix is a permutation of 1, 2, β¦, n. Let a_{i, j} be the element at the intersection of i-th row and j-th column for every 1 β€ i, j β€ n. Rows are numbered 1, β¦, n top to bottom, and columns are numbered 1, β¦, n left to right.
There are six types of operations:
* R: cyclically shift all columns to the right, formally, set the value of each a_{i, j} to a_{i, ((j - 2)mod n) + 1};
* L: cyclically shift all columns to the left, formally, set the value of each a_{i, j} to a_{i, (jmod n) + 1};
* D: cyclically shift all rows down, formally, set the value of each a_{i, j} to a_{((i - 2)mod n) + 1, j};
* U: cyclically shift all rows up, formally, set the value of each a_{i, j} to a_{(imod n) + 1, j};
* I: replace the permutation read left to right in each row with its inverse.
* C: replace the permutation read top to bottom in each column with its inverse.
Inverse of a permutation p_1, p_2, β¦, p_n is a permutation q_1, q_2, β¦, q_n, such that p_{q_i} = i for every 1 β€ i β€ n.
One can see that after any sequence of operations every row and every column of the matrix will still be a permutation of 1, 2, β¦, n.
Given the initial matrix description, you should process m operations and output the final matrix.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β number of test cases. t test case descriptions follow.
The first line of each test case description contains two integers n and m (1 β€ n β€ 1000, 1 β€ m β€ 10^5) β size of the matrix and number of operations.
Each of the next n lines contains n integers separated by single spaces β description of the matrix a (1 β€ a_{i, j} β€ n).
The last line of the description contains a string of m characters describing the operations in order, according to the format above.
The sum of n does not exceed 1000, and the sum of m does not exceed 10^5.
Output
For each test case, print n lines with n integers each β the final matrix after m operations.
Example
Input
5
3 2
1 2 3
2 3 1
3 1 2
DR
3 2
1 2 3
2 3 1
3 1 2
LU
3 1
1 2 3
2 3 1
3 1 2
I
3 1
1 2 3
2 3 1
3 1 2
C
3 16
1 2 3
2 3 1
3 1 2
LDICRUCILDICRUCI
Output
2 3 1
3 1 2
1 2 3
3 1 2
1 2 3
2 3 1
1 2 3
3 1 2
2 3 1
1 3 2
2 1 3
3 2 1
2 3 1
3 1 2
1 2 3
Note
Line breaks between sample test case answers are only for clarity, and don't have to be printed.
Submitted Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
n,m = map(int,input().split())
square = []
for _ in range(n):
square.append(list(map(int,input().split())))
oper = input()
iCnt = 0
cCnt = 0
rCnt = 0
dCnt = 0
ICStack = []
for elem in oper:
if elem == "I":
iCnt = 1 - iCnt
if ICStack and ICStack[-1] == "I":
ICStack.pop()
else:
ICStack.append("I")
if elem == "C":
cCnt = 1 - cCnt
if ICStack and ICStack[-1] == "C":
ICStack.pop()
else:
ICStack.append("C")
if elem == "R":
if iCnt == 1:
rCnt -= 1
else:
rCnt += 1
if elem == "L":
if iCnt == 1:
rCnt += 1
else:
rCnt -= 1
if elem == "D":
if cCnt == 1:
dCnt -= 1
else:
dCnt += 1
if elem == "U":
if cCnt == 1:
dCnt += 1
else:
dCnt -= 1
newSquare = []
for i in range(n):
newSquare.append([])
for j in range(n):
newSquare[i].append(square[(i - dCnt) % n][(j - rCnt) % n])
square = newSquare
for i in range(len(ICStack) % 6):
if ICStack[i] == "I":
newSquare = []
for i in range(n):
newSquare.append([0] * n)
for i in range(n):
for j in range(n):
newSquare[i][square[i][j] - 1] = j + 1
else:
newSquare = []
for i in range(n):
newSquare.append([0] * n)
for i in range(n):
for j in range(n):
newSquare[square[i][j] - 1][j] = i + 1
square = newSquare
for i in range(n):
print(" ".join(map(str,square[i])))
print()
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | instruction | 0 | 82,799 | 12 | 165,598 |
No | output | 1 | 82,799 | 12 | 165,599 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
Note: the XOR-sum of an array a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9) is defined as a_1 β a_2 β β¦ β a_n, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Little Dormi received an array of n integers a_1, a_2, β¦, a_n for Christmas. However, while playing with it over the winter break, he accidentally dropped it into his XOR machine, and the array got lost.
The XOR machine is currently configured with a query size of k (which you cannot change), and allows you to perform the following type of query: by giving the machine k distinct indices x_1, x_2, β¦, x_k, it will output a_{x_1} β a_{x_2} β β¦ β a_{x_k}.
As Little Dormi's older brother, you would like to help him recover the XOR-sum of his array a_1, a_2, β¦, a_n by querying the XOR machine.
Little Dormi isn't very patient, so to be as fast as possible, you must query the XOR machine the minimum number of times to find the XOR-sum of his array. Formally, let d be the minimum number of queries needed to find the XOR-sum of any array of length n with a query size of k. Your program will be accepted if you find the correct XOR-sum in at most d queries.
Lastly, you also noticed that with certain configurations of the machine k and values of n, it may not be possible to recover the XOR-sum of Little Dormi's lost array. If that is the case, you should report it as well.
The array a_1, a_2, β¦, a_n is fixed before you start querying the XOR machine and does not change with the queries.
Input
The only line of input contains the integers n and k (1 β€ n β€ 500, 1 β€ k β€ n), the length of the lost array and the configured query size of the XOR machine.
Elements of the original array satisfy 1 β€ a_i β€ 10^9.
It can be proven that that if it is possible to recover the XOR sum under the given constraints, it can be done in at most 500 queries. That is, d β€ 500.
After taking n and k, begin interaction.
Output
If it is impossible to recover the XOR-sum of the array, output -1 immediately after taking n and k. Do not begin interaction.
Otherwise, when your program finds the XOR-sum of the lost array a_1, a_2, β¦, a_n, report the answer in the following format: "! x", where x is the XOR sum of the array a_1, a_2, β¦, a_n, and terminate your program normally immediately after flushing the output stream.
Note that answering does not count as a query.
Interaction
Each query is made in the format "? b", where b is an array of exactly k distinct integers from 1 to n denoting the indices of the elements in the lost array that you want to query the XOR sum of.
You will then receive an integer x, the XOR sum of the queried elements. It can be proven that 0 β€ x β€ 2 β
10^9 will always be true.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
If at any point you make an invalid query or try to make more than 500 queries (which is the hard limit), the interaction will terminate immediately and give you a Wrong Answer verdict. Note that if you exceed d queries, the interaction will continue normally unless you also exceed the 500 query hard limit, though you will still receive a Wrong Answer verdict either way.
Hacks
To hack a solution, use the following format.
The first line contains the integers n and k (1 β€ n β€ 500, 1 β€ k β€ n).
The second line contains the the array a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9).
Examples
Input
5 3
4
0
1
Output
? 1 2 3
? 2 3 5
? 4 1 5
! 7
Input
3 2
Output
-1
Note
In the first example interaction, the array a_1, a_2, β¦, a_n is 2, 1, 7, 5, 6 and its XOR-sum is 7.
The first query made asks for indices 1,2,3, so the response is a_1 β a_2 β a_3 = 2 β 1 β 7 = 4.
The second query made asks for indices 2,3,5, so the response is a_2 β a_3 β a_5 = 1 β 7 β 6 = 0.
The third query made asks for indices 4,1,5, so the response is a_4 β a_1 β a_5 = 5 β 2 β 6 = 1. Note that the indices may be output in any order.
Additionally, even though three queries were made in the example interaction, it is just meant to demonstrate the interaction format and does not necessarily represent an optimal strategy.
In the second example interaction, there is no way to recover the XOR-sum of Little Dormi's array no matter what is queried, so the program immediately outputs -1 and exits. | instruction | 0 | 82,819 | 12 | 165,638 |
Tags: brute force, constructive algorithms, dp, flows, graphs, greedy, implementation, interactive, shortest paths
Correct Solution:
```
from collections import deque, defaultdict
from sys import stdout
def searchPath(n, k):
def getNexts(curr):
lb = max(k - curr, curr - k)
ub = min(n - max(0, k + curr - n), curr + k)
return range(lb, ub + 1, 2)
processed = [False for _ in range(n + 1)]
deq = deque()
deq.append(0)
prev = [None for _ in range(n + 1)]
while len(deq) > 0 and not processed[n]:
x = deq.popleft()
processed[x] = True
nexts = getNexts(x)
for xNext in nexts:
if not processed[xNext]:
processed[xNext] = True
prev[xNext] = x
deq.append(xNext)
if not processed[n]:
return None
result = []
x = n
while x > 0:
result.append(x)
x = prev[x]
return result[::-1]
def solve(n, k):
fullSet = set(range(1, n+1))
path = searchPath(n, k)
if path is None:
return None
currSet = set()
result = 0
for length in path:
currLen = len(currSet)
inSet = int((currLen + k - length)/ 2)
outSet = k - inSet
a = []
for i, x in enumerate(currSet):
if i == inSet:
break
a.append(x)
currAntiSet = fullSet - currSet
for x in a:
currSet.remove(x)
for i, x in enumerate(currAntiSet):
if i == outSet:
break
a.append(x)
currSet.add(x)
print('?', *a)
result = result ^ int(input())
return result
def printResult(result):
if result is None:
print(-1)
else:
print('!',result)
#t = int(input())
t = 1
results = deque()
for _ in range(t):
n, k = list(map(int, input().split(' ')))
result = solve(n, k)
printResult(result)
``` | output | 1 | 82,819 | 12 | 165,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
Note: the XOR-sum of an array a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9) is defined as a_1 β a_2 β β¦ β a_n, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Little Dormi received an array of n integers a_1, a_2, β¦, a_n for Christmas. However, while playing with it over the winter break, he accidentally dropped it into his XOR machine, and the array got lost.
The XOR machine is currently configured with a query size of k (which you cannot change), and allows you to perform the following type of query: by giving the machine k distinct indices x_1, x_2, β¦, x_k, it will output a_{x_1} β a_{x_2} β β¦ β a_{x_k}.
As Little Dormi's older brother, you would like to help him recover the XOR-sum of his array a_1, a_2, β¦, a_n by querying the XOR machine.
Little Dormi isn't very patient, so to be as fast as possible, you must query the XOR machine the minimum number of times to find the XOR-sum of his array. Formally, let d be the minimum number of queries needed to find the XOR-sum of any array of length n with a query size of k. Your program will be accepted if you find the correct XOR-sum in at most d queries.
Lastly, you also noticed that with certain configurations of the machine k and values of n, it may not be possible to recover the XOR-sum of Little Dormi's lost array. If that is the case, you should report it as well.
The array a_1, a_2, β¦, a_n is fixed before you start querying the XOR machine and does not change with the queries.
Input
The only line of input contains the integers n and k (1 β€ n β€ 500, 1 β€ k β€ n), the length of the lost array and the configured query size of the XOR machine.
Elements of the original array satisfy 1 β€ a_i β€ 10^9.
It can be proven that that if it is possible to recover the XOR sum under the given constraints, it can be done in at most 500 queries. That is, d β€ 500.
After taking n and k, begin interaction.
Output
If it is impossible to recover the XOR-sum of the array, output -1 immediately after taking n and k. Do not begin interaction.
Otherwise, when your program finds the XOR-sum of the lost array a_1, a_2, β¦, a_n, report the answer in the following format: "! x", where x is the XOR sum of the array a_1, a_2, β¦, a_n, and terminate your program normally immediately after flushing the output stream.
Note that answering does not count as a query.
Interaction
Each query is made in the format "? b", where b is an array of exactly k distinct integers from 1 to n denoting the indices of the elements in the lost array that you want to query the XOR sum of.
You will then receive an integer x, the XOR sum of the queried elements. It can be proven that 0 β€ x β€ 2 β
10^9 will always be true.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
If at any point you make an invalid query or try to make more than 500 queries (which is the hard limit), the interaction will terminate immediately and give you a Wrong Answer verdict. Note that if you exceed d queries, the interaction will continue normally unless you also exceed the 500 query hard limit, though you will still receive a Wrong Answer verdict either way.
Hacks
To hack a solution, use the following format.
The first line contains the integers n and k (1 β€ n β€ 500, 1 β€ k β€ n).
The second line contains the the array a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9).
Examples
Input
5 3
4
0
1
Output
? 1 2 3
? 2 3 5
? 4 1 5
! 7
Input
3 2
Output
-1
Note
In the first example interaction, the array a_1, a_2, β¦, a_n is 2, 1, 7, 5, 6 and its XOR-sum is 7.
The first query made asks for indices 1,2,3, so the response is a_1 β a_2 β a_3 = 2 β 1 β 7 = 4.
The second query made asks for indices 2,3,5, so the response is a_2 β a_3 β a_5 = 1 β 7 β 6 = 0.
The third query made asks for indices 4,1,5, so the response is a_4 β a_1 β a_5 = 5 β 2 β 6 = 1. Note that the indices may be output in any order.
Additionally, even though three queries were made in the example interaction, it is just meant to demonstrate the interaction format and does not necessarily represent an optimal strategy.
In the second example interaction, there is no way to recover the XOR-sum of Little Dormi's array no matter what is queried, so the program immediately outputs -1 and exits. | instruction | 0 | 82,820 | 12 | 165,640 |
Tags: brute force, constructive algorithms, dp, flows, graphs, greedy, implementation, interactive, shortest paths
Correct Solution:
```
from time import time
t0 = time()
from heapq import heappop, heappush, heapify
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
N, K = map(int, input().split())
Table = [1]*N
c = N
h = 1
idx = 0
while c%K != 0 or c//K < h:
if idx == 0:
h += 2
Table[idx] += 2
c += 2
idx = (idx+1)%N
if time()-t0 > 1.0:
print(-1)
exit()
q = []
for i, t in enumerate(Table):
q.append(-1000*t-i)
heapify(q)
ans = 0
for _ in range(c//K):
query = []
q_out = []
for _ in range(K):
v = -heappop(q)
i = v%1000
t = v//1000
query.append(i+1)
t -= 1
q_out.append(-(t*1000+i))
for v in q_out:
heappush(q, v)
print("?", " ".join(map(str, query)))
sys.stdout.flush()
a = int(input())
ans ^= a
print("!", ans)
sys.stdout.flush()
``` | output | 1 | 82,820 | 12 | 165,641 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.