message stringlengths 2 433k | message_type stringclasses 2
values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by paying as little money as possible.
More formally, we have two permutations, p and s of numbers from 1 to n. We can swap pi and pj, by paying |i - j| coins for it. Find and print the smallest number of coins required to obtain permutation s from permutation p. Also print the sequence of swap operations at which we obtain a solution.
Input
The first line contains a single number n (1 β€ n β€ 2000) β the length of the permutations.
The second line contains a sequence of n numbers from 1 to n β permutation p. Each number from 1 to n occurs exactly once in this line.
The third line contains a sequence of n numbers from 1 to n β permutation s. Each number from 1 to n occurs once in this line.
Output
In the first line print the minimum number of coins that you need to spend to transform permutation p into permutation s.
In the second line print number k (0 β€ k β€ 2Β·106) β the number of operations needed to get the solution.
In the next k lines print the operations. Each line must contain two numbers i and j (1 β€ i, j β€ n, i β j), which means that you need to swap pi and pj.
It is guaranteed that the solution exists.
Examples
Input
4
4 2 1 3
3 2 4 1
Output
3
2
4 3
3 1
Note
In the first sample test we swap numbers on positions 3 and 4 and permutation p becomes 4 2 3 1. We pay |3 - 4| = 1 coins for that. On second turn we swap numbers on positions 1 and 3 and get permutation 3241 equal to s. We pay |3 - 1| = 2 coins for that. In total we pay three coins.
Submitted Solution:
```
n = int(input())
a = list(map(int,input().split()))
b= list(map(int,input().split()))
ans = []
for i in range(n):
for k in range(n):
if a[i] == b[k]:
if i == k:
ans.append(-1)
break
ans.append(k+1)
break
j = 0
otv = []
t = 0
i = 0
while i < n:
if t == 1:
t = 0
i -= 1
for k in range(n-i-1):
if ans[k] > ans[k+1+i] and min(ans[k],ans[k+1+i]) != -1:
otv.append([k+1,k+i+2])
ans[k],ans[k+i+1] = ans[k+1+i],ans[k]
j += (abs(k-(k+1+i)))
t = 1
i += 1
print(j)
print(len(otv))
for i in range(len(otv)):
print(*otv[i])
``` | instruction | 0 | 68,567 | 12 | 137,134 |
No | output | 1 | 68,567 | 12 | 137,135 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by paying as little money as possible.
More formally, we have two permutations, p and s of numbers from 1 to n. We can swap pi and pj, by paying |i - j| coins for it. Find and print the smallest number of coins required to obtain permutation s from permutation p. Also print the sequence of swap operations at which we obtain a solution.
Input
The first line contains a single number n (1 β€ n β€ 2000) β the length of the permutations.
The second line contains a sequence of n numbers from 1 to n β permutation p. Each number from 1 to n occurs exactly once in this line.
The third line contains a sequence of n numbers from 1 to n β permutation s. Each number from 1 to n occurs once in this line.
Output
In the first line print the minimum number of coins that you need to spend to transform permutation p into permutation s.
In the second line print number k (0 β€ k β€ 2Β·106) β the number of operations needed to get the solution.
In the next k lines print the operations. Each line must contain two numbers i and j (1 β€ i, j β€ n, i β j), which means that you need to swap pi and pj.
It is guaranteed that the solution exists.
Examples
Input
4
4 2 1 3
3 2 4 1
Output
3
2
4 3
3 1
Note
In the first sample test we swap numbers on positions 3 and 4 and permutation p becomes 4 2 3 1. We pay |3 - 4| = 1 coins for that. On second turn we swap numbers on positions 1 and 3 and get permutation 3241 equal to s. We pay |3 - 1| = 2 coins for that. In total we pay three coins.
Submitted Solution:
```
n, p, s = int(input()), list(map(int, input().split())), list(map(int, input().split()))
zp = [-1] * n
pos = [-1] * n
def swap(i, j):
pos[p[i]], pos[p[j]] = pos[p[j]], pos[p[i]]
p[i], p[j] = p[j], p[i]
for i in range(n):
zp[s[i] - 1] = i
for i in range(n):
p[i] = zp[p[i] - 1]
pos[p[i]] = i
s[i] = i
ans, ansv = 0, []
for i in range(n - 1, -1, -1):
if p == s:
break
pl, pr = pos[i], pos[i] + 1
if pl == i:
continue
ans += i - pos[i]
while pl < i:
while p[pr] > pl:
pr += 1
swap(pl, pr)
pl, pr = pr, pr + 1
ansv.append((pl, pr))
print(ans)
print(len(ansv))
for i, j in ansv:
print(i + 1, j + 1)
``` | instruction | 0 | 68,568 | 12 | 137,136 |
No | output | 1 | 68,568 | 12 | 137,137 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by paying as little money as possible.
More formally, we have two permutations, p and s of numbers from 1 to n. We can swap pi and pj, by paying |i - j| coins for it. Find and print the smallest number of coins required to obtain permutation s from permutation p. Also print the sequence of swap operations at which we obtain a solution.
Input
The first line contains a single number n (1 β€ n β€ 2000) β the length of the permutations.
The second line contains a sequence of n numbers from 1 to n β permutation p. Each number from 1 to n occurs exactly once in this line.
The third line contains a sequence of n numbers from 1 to n β permutation s. Each number from 1 to n occurs once in this line.
Output
In the first line print the minimum number of coins that you need to spend to transform permutation p into permutation s.
In the second line print number k (0 β€ k β€ 2Β·106) β the number of operations needed to get the solution.
In the next k lines print the operations. Each line must contain two numbers i and j (1 β€ i, j β€ n, i β j), which means that you need to swap pi and pj.
It is guaranteed that the solution exists.
Examples
Input
4
4 2 1 3
3 2 4 1
Output
3
2
4 3
3 1
Note
In the first sample test we swap numbers on positions 3 and 4 and permutation p becomes 4 2 3 1. We pay |3 - 4| = 1 coins for that. On second turn we swap numbers on positions 1 and 3 and get permutation 3241 equal to s. We pay |3 - 1| = 2 coins for that. In total we pay three coins.
Submitted Solution:
```
n, p, s = int(input()), list(map(int, input().split())), list(map(int, input().split()))
ind = [-1] * n
for i in range(n):
p[i] -= 1
s[i] -= 1
ind[p[i]] = i
ans, ansv = 0, []
for i in range(n - 1, -1, -1):
if p == s:
break
ans += abs(ind[s[i]] - i)
ansv.append((ind[s[i]], i))
k = ind[s[i]]
ind[p[i]], ind[s[i]] = ind[s[i]], i
p[k], p[i] = p[i], p[k]
print(ans)
print(len(ansv))
for x in ansv:
print(x[0] + 1, x[1] + 1)
``` | instruction | 0 | 68,569 | 12 | 137,138 |
No | output | 1 | 68,569 | 12 | 137,139 |
Provide a correct Python 3 solution for this coding contest problem.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES | instruction | 0 | 68,729 | 12 | 137,458 |
"Correct Solution:
```
N=int(input())
P=list(map(int,input().split()))
cnt=0
for n in range(N):
if (n+1)!=P[n]:
cnt+=1
if cnt<=2:
print("YES")
else:
print("NO")
``` | output | 1 | 68,729 | 12 | 137,459 |
Provide a correct Python 3 solution for this coding contest problem.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES | instruction | 0 | 68,730 | 12 | 137,460 |
"Correct Solution:
```
n=int(input())
p=list(map(int,input().split()))
q=sorted(p)
cnt=0
for i in range(n):
if p[i]!=q[i]:
cnt+=1
if cnt<=2:
print("YES")
else:
print("NO")
``` | output | 1 | 68,730 | 12 | 137,461 |
Provide a correct Python 3 solution for this coding contest problem.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES | instruction | 0 | 68,731 | 12 | 137,462 |
"Correct Solution:
```
N=int(input())
p=list(map(int,input().split()))
count=0
for i in range(N):
if p[i]!=i+1:
count+=1
print("YES" if count<=2 else "NO")
``` | output | 1 | 68,731 | 12 | 137,463 |
Provide a correct Python 3 solution for this coding contest problem.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES | instruction | 0 | 68,732 | 12 | 137,464 |
"Correct Solution:
```
N=int(input())
p=list(map(int,input().split()))
cnt=0
for i in range(N):
if p[i]!=i+1:
cnt=cnt+1
print("YES" if cnt<=2 else "NO")
``` | output | 1 | 68,732 | 12 | 137,465 |
Provide a correct Python 3 solution for this coding contest problem.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES | instruction | 0 | 68,733 | 12 | 137,466 |
"Correct Solution:
```
n=int(input())
p=list(map(int,input().split()))
c=0
for i in range(n):
if p[i]!=i+1:
c+=1
print('YES' if c<3 else 'NO')
``` | output | 1 | 68,733 | 12 | 137,467 |
Provide a correct Python 3 solution for this coding contest problem.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES | instruction | 0 | 68,734 | 12 | 137,468 |
"Correct Solution:
```
N=int(input())
n=list(map(int,input().split()))
count=0
for i in range(N):
count+=(n[i]!=i+1)
print(("YES","NO")[count>2])
``` | output | 1 | 68,734 | 12 | 137,469 |
Provide a correct Python 3 solution for this coding contest problem.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES | instruction | 0 | 68,735 | 12 | 137,470 |
"Correct Solution:
```
n = int(input())
p = list(map(int,input().split()))
cnt = 0
for i in range(1,n+1):
if p[i-1]!=i:
cnt += 1
print('YES' if cnt<=2 else 'NO')
``` | output | 1 | 68,735 | 12 | 137,471 |
Provide a correct Python 3 solution for this coding contest problem.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES | instruction | 0 | 68,736 | 12 | 137,472 |
"Correct Solution:
```
n = int(input())
p = map(int, input().split())
judge = [(v != k+1) for k, v in enumerate(p)]
if sum(judge) <= 2:
print('YES')
else:
print('NO')
``` | output | 1 | 68,736 | 12 | 137,473 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES
Submitted Solution:
```
input();print("YNEOS"[len([1 for i,q in enumerate(map(int,input().split()),1)if q!=i])>2::2])
``` | instruction | 0 | 68,737 | 12 | 137,474 |
Yes | output | 1 | 68,737 | 12 | 137,475 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES
Submitted Solution:
```
n=int(input())
p=list(map(int,input().split()))
g=sum((1 for i in range(n) if i+1 !=p[i]))
print("YNEOS"[::2] if g==2 or g==0 else "YNEOS"[1::2])
``` | instruction | 0 | 68,738 | 12 | 137,476 |
Yes | output | 1 | 68,738 | 12 | 137,477 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES
Submitted Solution:
```
n=int(input())
l=list(map(int,input().split()))
m=sum([(i+1)!=l[i] for i in range(n)])
print("YES" if m==0 or m==2 else "NO")
``` | instruction | 0 | 68,739 | 12 | 137,478 |
Yes | output | 1 | 68,739 | 12 | 137,479 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES
Submitted Solution:
```
N = int(input())
P = list(map(int, input().split()))
CNT = 0
for i in range(N):
if P[i] != i+1:
CNT += 1
print('YES' if CNT <= 2 else 'NO')
``` | instruction | 0 | 68,740 | 12 | 137,480 |
Yes | output | 1 | 68,740 | 12 | 137,481 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES
Submitted Solution:
```
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque
# import fractions
sys.setrecursionlimit(10000000)
ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]
iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]
lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)
MOD = 10 ** 9 + 7
MAX = float('inf')
def main():
if os.getenv("LOCAL"):
sys.stdin = open("input.txt", "r")
N = ii()
P = il()
cnt = 0
for n in range(N):
if P[n] != n + 1:
cnt += 1
if P[P[n]] != n+1:
print('NO')
exit()
print('NO' if cnt > 3 else 'YES')
if __name__ == '__main__':
main()
``` | instruction | 0 | 68,741 | 12 | 137,482 |
No | output | 1 | 68,741 | 12 | 137,483 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES
Submitted Solution:
```
n=int(input())
l=list(map(int,input().rstrip().split()))
f=1
a=0
y=0
for i in range(1,n+1):
if(l[i]==i):
f=1
else:
a=l[i]
if((l[a]==i)and(y==0)):
l[a]=a
l[i]=i
f=1
y=1
else:
f=0
break
if(f==1):
print("YES")
else:
print("NO")
``` | instruction | 0 | 68,742 | 12 | 137,484 |
No | output | 1 | 68,742 | 12 | 137,485 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES
Submitted Solution:
```
N = int(input())
A = list(map(int,input().split()))
cnt = 0
for i in range(N-1):
if A[i]+1 != A[i+1]:
cnt += 1
if cnt <= 2:
print("YES")
elif cnt >= 3:
print("NO")
``` | instruction | 0 | 68,743 | 12 | 137,486 |
No | output | 1 | 68,743 | 12 | 137,487 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print `YES` if you can sort p in ascending order in this way, and `NO` otherwise.
Constraints
* All values in input are integers.
* 2 \leq N \leq 50
* p is a permutation of {1,\ 2,\ ...,\ N}.
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N
Output
Print `YES` if you can sort p in ascending order in the way stated in the problem statement, and `NO` otherwise.
Examples
Input
5
5 2 3 4 1
Output
YES
Input
5
2 4 3 5 1
Output
NO
Input
7
1 2 3 4 5 6 7
Output
YES
Submitted Solution:
```
N = int(input())
p = list(map(int, input().split()))
sorted_p = sorted(p)
cnt = 0
for i in range(N):
if p[i] != sorted_p[i]:
cnt += 1
if cnt == 0 or cnt == 2:
print('Yes')
else:
print('No')
``` | instruction | 0 | 68,744 | 12 | 137,488 |
No | output | 1 | 68,744 | 12 | 137,489 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers, and q queries to it. i-th query is denoted by two integers l_i and r_i. For each query, you have to find any integer that occurs exactly once in the subarray of a from index l_i to index r_i (a subarray is a contiguous subsegment of an array). For example, if a = [1, 1, 2, 3, 2, 4], then for query (l_i = 2, r_i = 6) the subarray we are interested in is [1, 2, 3, 2, 4], and possible answers are 1, 3 and 4; for query (l_i = 1, r_i = 2) the subarray we are interested in is [1, 1], and there is no such element that occurs exactly once.
Can you answer all of the queries?
Input
The first line contains one integer n (1 β€ n β€ 5 β
10^5).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 5 β
10^5).
The third line contains one integer q (1 β€ q β€ 5 β
10^5).
Then q lines follow, i-th line containing two integers l_i and r_i representing i-th query (1 β€ l_i β€ r_i β€ n).
Output
Answer the queries as follows:
If there is no integer such that it occurs in the subarray from index l_i to index r_i exactly once, print 0. Otherwise print any such integer.
Example
Input
6
1 1 2 3 2 4
2
2 6
1 2
Output
4
0
Submitted Solution:
```
n=int(input())
numbers=list(map(int,list(input().split(" "))))
query=int(input())
#ranj=list(map(int,list(input().split(" "))))
def FindSingle(low,high,hash):
for i in range(low,high):
if hash[numbers[i]]==1:
return numbers[i]
return 0
def SingleOcc(low,high,hash):
if low==high:
if numbers[low] in hash.keys():
hash[numbers[low]]+=1
else:
hash[numbers[low]]=1
else:
mid=(low+high)//2
SingleOcc(low,mid,hash)
SingleOcc(mid+1,high,hash)
for k in range(query):
hash={}
ranj=list(map(int,list(input().split(" "))))
SingleOcc(ranj[0]-1,ranj[1]-1,hash)
#print("Hash is"+str(hash))
print(FindSingle(ranj[0]-1,ranj[1]-1,hash))
``` | instruction | 0 | 68,910 | 12 | 137,820 |
No | output | 1 | 68,910 | 12 | 137,821 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers, and q queries to it. i-th query is denoted by two integers l_i and r_i. For each query, you have to find any integer that occurs exactly once in the subarray of a from index l_i to index r_i (a subarray is a contiguous subsegment of an array). For example, if a = [1, 1, 2, 3, 2, 4], then for query (l_i = 2, r_i = 6) the subarray we are interested in is [1, 2, 3, 2, 4], and possible answers are 1, 3 and 4; for query (l_i = 1, r_i = 2) the subarray we are interested in is [1, 1], and there is no such element that occurs exactly once.
Can you answer all of the queries?
Input
The first line contains one integer n (1 β€ n β€ 5 β
10^5).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 5 β
10^5).
The third line contains one integer q (1 β€ q β€ 5 β
10^5).
Then q lines follow, i-th line containing two integers l_i and r_i representing i-th query (1 β€ l_i β€ r_i β€ n).
Output
Answer the queries as follows:
If there is no integer such that it occurs in the subarray from index l_i to index r_i exactly once, print 0. Otherwise print any such integer.
Example
Input
6
1 1 2 3 2 4
2
2 6
1 2
Output
4
0
Submitted Solution:
```
size = int(input())
arr = [int(x) for x in input().split()]
s = set()
for i in arr:
s.add(i)
freq = {i:0 for i in s}
for i in arr:
freq[i] += 1
query = int(input())
for u in range (query):
status = 0
index = [int(x) for x in input().split()]
for i in range (index[0]-1,index[1]) :
if freq[arr[i]] == 1:
status = arr[i]
break
print(status)
#freq = {i:0 for i in arr}
``` | instruction | 0 | 68,911 | 12 | 137,822 |
No | output | 1 | 68,911 | 12 | 137,823 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers, and q queries to it. i-th query is denoted by two integers l_i and r_i. For each query, you have to find any integer that occurs exactly once in the subarray of a from index l_i to index r_i (a subarray is a contiguous subsegment of an array). For example, if a = [1, 1, 2, 3, 2, 4], then for query (l_i = 2, r_i = 6) the subarray we are interested in is [1, 2, 3, 2, 4], and possible answers are 1, 3 and 4; for query (l_i = 1, r_i = 2) the subarray we are interested in is [1, 1], and there is no such element that occurs exactly once.
Can you answer all of the queries?
Input
The first line contains one integer n (1 β€ n β€ 5 β
10^5).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 5 β
10^5).
The third line contains one integer q (1 β€ q β€ 5 β
10^5).
Then q lines follow, i-th line containing two integers l_i and r_i representing i-th query (1 β€ l_i β€ r_i β€ n).
Output
Answer the queries as follows:
If there is no integer such that it occurs in the subarray from index l_i to index r_i exactly once, print 0. Otherwise print any such integer.
Example
Input
6
1 1 2 3 2 4
2
2 6
1 2
Output
4
0
Submitted Solution:
```
a = int(input())
li = [int(x) for x in input().split()]
num = int(input())
for i in range (num):
s = set()
t = set()
l = 0
index = [int(x)-1 for x in input().split()]
for u in range (index[0]-1,index[1]+1):
if li[u] not in s and li[u] not in t:
s.add(li[u])
l += 1
else :
if li[u] in s :
s.remove(li[u])
l -= 1
t.add(li[u])
if l == 0 :
print(0)
else :
for e in s :
print(e)
break
``` | instruction | 0 | 68,912 | 12 | 137,824 |
No | output | 1 | 68,912 | 12 | 137,825 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers, and q queries to it. i-th query is denoted by two integers l_i and r_i. For each query, you have to find any integer that occurs exactly once in the subarray of a from index l_i to index r_i (a subarray is a contiguous subsegment of an array). For example, if a = [1, 1, 2, 3, 2, 4], then for query (l_i = 2, r_i = 6) the subarray we are interested in is [1, 2, 3, 2, 4], and possible answers are 1, 3 and 4; for query (l_i = 1, r_i = 2) the subarray we are interested in is [1, 1], and there is no such element that occurs exactly once.
Can you answer all of the queries?
Input
The first line contains one integer n (1 β€ n β€ 5 β
10^5).
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 5 β
10^5).
The third line contains one integer q (1 β€ q β€ 5 β
10^5).
Then q lines follow, i-th line containing two integers l_i and r_i representing i-th query (1 β€ l_i β€ r_i β€ n).
Output
Answer the queries as follows:
If there is no integer such that it occurs in the subarray from index l_i to index r_i exactly once, print 0. Otherwise print any such integer.
Example
Input
6
1 1 2 3 2 4
2
2 6
1 2
Output
4
0
Submitted Solution:
```
n=int(input())
numbers=list(map(int,list(input().split(" "))))
#print(numbers)
query=int(input())
#ranj=list(map(int,list(input().split(" "))))
dynamic=[0 for i in range(50006)]
def FindSingle(low,high):
for i in range(low,high):
if dynamic[numbers[i]]==1:
return numbers[i]
return 0
def SingleOcc(low,high):
if low==high:
dynamic[numbers[low]]+=1
else:
mid=(low+high)//2
SingleOcc(low,mid)
SingleOcc(mid+1,high)
for k in range(query):
ranj=list(map(int,list(input().split(" "))))
SingleOcc(ranj[0]-1,ranj[1]-1)
#print("Hash is"+str(hash))
print(FindSingle(ranj[0]-1,ranj[1]-1))
#print(dynamic)
for i in range(ranj[0]-1,ranj[1]-1):
dynamic[numbers[i]]=0
``` | instruction | 0 | 68,913 | 12 | 137,826 |
No | output | 1 | 68,913 | 12 | 137,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2. | instruction | 0 | 69,104 | 12 | 138,208 |
Tags: constructive algorithms, data structures, greedy, implementation
Correct Solution:
```
#BECAUSE ONE TRUTH PREVAILS
import sys
def arr():
return list(map(int,input().split()))
input=sys.stdin.readline
from collections import defaultdict
import math
N=int(input())
L1=arr()
L2=arr()
Hash=defaultdict(lambda:0)
for i in range(N):
Hash[L2[i]]=i
Ha=defaultdict(lambda:0)
for i in range(N):
diff=Hash[L1[i]]-i
if diff<0:
diff=diff+N
Ha[diff]+=1
ma=0
for i,j in Ha.items():
ma=max(ma,j)
print(ma)
``` | output | 1 | 69,104 | 12 | 138,209 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2. | instruction | 0 | 69,105 | 12 | 138,210 |
Tags: constructive algorithms, data structures, greedy, implementation
Correct Solution:
```
n = int(input())
a_trans = [0] * (n + 1)
for i, ai in enumerate(input().split()):
a_trans[int(ai)] = i
matches = [0] * n
for i, bi in enumerate(input().split()):
matches[(i - a_trans[int(bi)]) % n] += 1
print(max(matches))
``` | output | 1 | 69,105 | 12 | 138,211 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2. | instruction | 0 | 69,106 | 12 | 138,212 |
Tags: constructive algorithms, data structures, greedy, implementation
Correct Solution:
```
n = int(input())
first = {key: value for key, value in zip(map(int, input().split()), range(n))}
second = list(map(int, input().split()))
values = {}
for i in range(n):
p = second[i]
value_right = f"{abs(n - abs(i - first[p]))} {'r' if i - first[p] > 0 else 'l'}"
value_left = f"{abs(i - first[p])} {'l' if i - first[p] > 0 else 'r'}"
if value_left not in values:
values[value_left] = 0
if value_right not in values:
values[value_right] = 0
values[value_left] += 1
values[value_right] += 1
print(max(values.values()))
``` | output | 1 | 69,106 | 12 | 138,213 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2. | instruction | 0 | 69,107 | 12 | 138,214 |
Tags: constructive algorithms, data structures, greedy, implementation
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
inda = {}
for i in range(n):
inda[a[i]] = i
indb = {}
for i in range(n):
indb[b[i]] = i
shift = [0]*n
for i in range(n):
if a[i] in indb:
ind = indb[a[i]]
if ind<i:
val = n-(i+1) + ind + 1
else:
val = ind-i
shift[val]+=1
print(max(shift))
``` | output | 1 | 69,107 | 12 | 138,215 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2. | instruction | 0 | 69,108 | 12 | 138,216 |
Tags: constructive algorithms, data structures, greedy, implementation
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
count=[0]*(n+1)
d={}
for i in range(n):
d[b[i]]=i+1
for i in range(1,n+1):
count[(n-d[a[i-1]]+i)%n]+=1
print(max(count))
``` | output | 1 | 69,108 | 12 | 138,217 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2. | instruction | 0 | 69,109 | 12 | 138,218 |
Tags: constructive algorithms, data structures, greedy, implementation
Correct Solution:
```
def answer(n,A,B):
dp1=[0]*(n+1)
dp2=[0]*(n+1)
for i in range(n):
dp1[A[i]]=i
dp2[B[i]]=i
d=[0]*(n)
for i in range(1,n+1):
if dp1[i]-dp2[i]>=0:
d[dp1[i]-dp2[i]]+=1
else:
d[n-dp2[i]+dp1[i]]+=1
return max(d)
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
print(answer(n,a,b))
``` | output | 1 | 69,109 | 12 | 138,219 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2. | instruction | 0 | 69,110 | 12 | 138,220 |
Tags: constructive algorithms, data structures, greedy, implementation
Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 5 16:06:35 2020
@author: Mridul Garg
"""
#q = int(input())
#for _ in range(q):
n = int(input())
A = list(map(int, input().split(" ")))
B = list(map(int, input().split(" ")))
dica, dicb = {}, {}
for i in range(n):
dica[A[i]] = i
dicb[B[i]] = i
re = {}
for i in range(1, n+1):
if dica[i] >= dicb[i]:
temp = dica[i]-dicb[i]
else:
temp = n - dicb[i] + dica[i]
if temp not in re:
re[temp] = 1
else:
re[temp] += 1
ans = list(re.values())
print(max(ans))
``` | output | 1 | 69,110 | 12 | 138,221 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2. | instruction | 0 | 69,111 | 12 | 138,222 |
Tags: constructive algorithms, data structures, greedy, implementation
Correct Solution:
```
#https://codeforces.com/problemset/problem/1365/C
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
shifts = {}
ans = 1
alpha = {}
for i in range(n):
alpha[b[i]]=[i]
for i in range(n):
alpha[a[i]].append(i)
for i in range(n):
index = alpha[b[i]][1]
shift = 0
if index<i:
index = n+index
shift = index-i
if shift in shifts:
shifts[shift]+=1
else:
shifts[shift]=1
for s in shifts:
if shifts[s]>ans:
ans = shifts[s]
print(ans)
``` | output | 1 | 69,111 | 12 | 138,223 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2.
Submitted Solution:
```
r=lambda:map(int,input().split())
n=int(input());a={};b={};g=[0]*n
for x,i,j in zip(range(n),r(),r()):a[i]=b[j]=x
for i in range(1,n+1):g[(b[i]-a[i])%n]+=1
print(max(g))
``` | instruction | 0 | 69,112 | 12 | 138,224 |
Yes | output | 1 | 69,112 | 12 | 138,225 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2.
Submitted Solution:
```
import math
from collections import defaultdict, Counter, deque
INF = float('inf')
def gcd(a, b):
while b:
a, b = b, a%b
return a
def primeFactor(n):
if n % 2 == 0:
return 2
i = 3
while (i ** 2) <= n:
if n % i == 0:
return i
i += 1
return n
def main():
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
index = {}
for i in range(n):
index[b[i]] = i
r = defaultdict(int)
for i in range(n):
shift = index[a[i]] - i
if shift < 0:
r[n + shift] += 1
else:
r[shift] += 1
res = 0
# print(r)
for k in r:
if r[k] > res:
res = r[k]
print(res)
if __name__ == "__main__":
# t = int(input())
t = 1
for _ in range(t):
main()
``` | instruction | 0 | 69,113 | 12 | 138,226 |
Yes | output | 1 | 69,113 | 12 | 138,227 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2.
Submitted Solution:
```
n = int(input())
arr = [0]+list(map(int, input().split()))
bbs = [0]+list(map(int, input().split()))
d = {}
for i in range(1, n+1):
d[arr[i]] = i
count = {}
for i in range(1, n+1):
curr = d[bbs[i]]-i
if curr < 0:
curr += n
count[curr] = count.get(curr, 0)+1
res = max(count.values())
print(res)
``` | instruction | 0 | 69,114 | 12 | 138,228 |
Yes | output | 1 | 69,114 | 12 | 138,229 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2.
Submitted Solution:
```
n = int(input())
a = list( map(int, input().split()))
b = list( map(int, input().split()))
hash_map = {}
for i,j in enumerate(a):
hash_map[j] = i
count_array = [0] * n
for i,j in enumerate(b):
diff = hash_map[j] - i
if diff <0:
diff +=n
count_array[diff] += 1
print(max(count_array))
``` | instruction | 0 | 69,115 | 12 | 138,230 |
Yes | output | 1 | 69,115 | 12 | 138,231 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2.
Submitted Solution:
```
n = int(input())
l = list(map(int,input().split()))
o = list(map(int,input().split()))
d = {}
for i in range(1,n+1):
d[i] = []
for i in range(1,n+1):
d[l[i-1]].append(i)
for i in range(1,n+1):
d[o[i-1]].append(i)
li = []
for key,value in d.items():
if d[key][1]==n:
d[key][0]+=n
dif = d[key][0]-d[key][1]
li.append(dif)
dct = {}
for i in li:
dct[i] = 0
for i in li:
dct[i] += 1
maxi = 0
for key,value in dct.items():
maxi = max(maxi,dct[key])
print(maxi)
``` | instruction | 0 | 69,116 | 12 | 138,232 |
No | output | 1 | 69,116 | 12 | 138,233 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2.
Submitted Solution:
```
def rotmatch(n, a, b):
ai = a.index(1)
bi = b.index(1)
count = 0
l = n
while(n!=0):
if(ai>l-1):
ai=0
if(bi>l-1):
bi = 0
if(a[ai]==b[bi]):
count+=1
#print(ai, bi)
ai+=1
bi+=1
n-=1
return count
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(rotmatch(n, a, b))
``` | instruction | 0 | 69,117 | 12 | 138,234 |
No | output | 1 | 69,117 | 12 | 138,235 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
i=0
j=0
count=0
if(a[0] in b):
d=b.index(a[i])
b=b[d:]+b[:d]
print(a,b)
for i in range(n):
if(a[i]==b[i]):
count+=1
print(count)
``` | instruction | 0 | 69,118 | 12 | 138,236 |
No | output | 1 | 69,118 | 12 | 138,237 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, β¦, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, β¦, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, β¦, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 β€ n β€ 2 β
10^5) β the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ n) β the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2.
Submitted Solution:
```
from sys import stdin
import copy
input = stdin.readline
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
d = dict()
for x in range(n):
t = a[x] - b[x]
if t < 0:
t += n
if t in d.keys():
d[t] += 1
else:
d[t] = 1
print(max(d.values()))
``` | instruction | 0 | 69,119 | 12 | 138,238 |
No | output | 1 | 69,119 | 12 | 138,239 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a_1, a_2, ..., a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a_1, a_2, ..., a_k] (0 β€ k β€ n).
The array b of length m is called good, if you can obtain a non-decreasing array c (c_1 β€ c_2 β€ ... β€ c_{m}) from it, repeating the following operation m times (initially, c is empty):
* select either the first or the last element of b, remove it from b, and append it to the end of the array c.
For example, if we do 4 operations: take b_1, then b_{m}, then b_{m-1} and at last b_2, then b becomes [b_3, b_4, ..., b_{m-3}] and c =[b_1, b_{m}, b_{m-1}, b_2].
Consider the following example: b = [1, 2, 3, 4, 4, 2, 1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:
1. take the first element of b, so b = [2, 3, 4, 4, 2, 1], c = [1];
2. take the last element of b, so b = [2, 3, 4, 4, 2], c = [1, 1];
3. take the last element of b, so b = [2, 3, 4, 4], c = [1, 1, 2];
4. take the first element of b, so b = [3, 4, 4], c = [1, 1, 2, 2];
5. take the first element of b, so b = [4, 4], c = [1, 1, 2, 2, 3];
6. take the last element of b, so b = [4], c = [1, 1, 2, 2, 3, 4];
7. take the only element of b, so b = [], c = [1, 1, 2, 2, 3, 4, 4] β c is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from a to make it a good array.
Example
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
Output
0
4
0
2
3
Note
In the first test case of the example, the array a is already good, so we don't need to erase any prefix.
In the second test case of the example, the initial array a is not good. Let's erase first 4 elements of a, the result is [4, 5, 2]. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good. | instruction | 0 | 69,120 | 12 | 138,240 |
Tags: greedy
Correct Solution:
```
for _ in range(int(input())):
i_liczb = int(input())
liczby = [0] + [int(x) for x in input().split()]
poz = i_liczb
while poz > 1 and liczby[poz - 1] >= liczby[poz]:
poz -= 1
while poz > 1 and liczby[poz - 1] <= liczby[poz]:
poz -= 1
print(poz - 1)
``` | output | 1 | 69,120 | 12 | 138,241 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a_1, a_2, ..., a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a_1, a_2, ..., a_k] (0 β€ k β€ n).
The array b of length m is called good, if you can obtain a non-decreasing array c (c_1 β€ c_2 β€ ... β€ c_{m}) from it, repeating the following operation m times (initially, c is empty):
* select either the first or the last element of b, remove it from b, and append it to the end of the array c.
For example, if we do 4 operations: take b_1, then b_{m}, then b_{m-1} and at last b_2, then b becomes [b_3, b_4, ..., b_{m-3}] and c =[b_1, b_{m}, b_{m-1}, b_2].
Consider the following example: b = [1, 2, 3, 4, 4, 2, 1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:
1. take the first element of b, so b = [2, 3, 4, 4, 2, 1], c = [1];
2. take the last element of b, so b = [2, 3, 4, 4, 2], c = [1, 1];
3. take the last element of b, so b = [2, 3, 4, 4], c = [1, 1, 2];
4. take the first element of b, so b = [3, 4, 4], c = [1, 1, 2, 2];
5. take the first element of b, so b = [4, 4], c = [1, 1, 2, 2, 3];
6. take the last element of b, so b = [4], c = [1, 1, 2, 2, 3, 4];
7. take the only element of b, so b = [], c = [1, 1, 2, 2, 3, 4, 4] β c is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from a to make it a good array.
Example
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
Output
0
4
0
2
3
Note
In the first test case of the example, the array a is already good, so we don't need to erase any prefix.
In the second test case of the example, the initial array a is not good. Let's erase first 4 elements of a, the result is [4, 5, 2]. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good. | instruction | 0 | 69,121 | 12 | 138,242 |
Tags: greedy
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
back=n-1
while(back>0 and a[back-1]>=a[back]):
back-=1
while(back>0 and a[back-1]<=a[back]):
back-=1
print(back)
``` | output | 1 | 69,121 | 12 | 138,243 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a_1, a_2, ..., a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a_1, a_2, ..., a_k] (0 β€ k β€ n).
The array b of length m is called good, if you can obtain a non-decreasing array c (c_1 β€ c_2 β€ ... β€ c_{m}) from it, repeating the following operation m times (initially, c is empty):
* select either the first or the last element of b, remove it from b, and append it to the end of the array c.
For example, if we do 4 operations: take b_1, then b_{m}, then b_{m-1} and at last b_2, then b becomes [b_3, b_4, ..., b_{m-3}] and c =[b_1, b_{m}, b_{m-1}, b_2].
Consider the following example: b = [1, 2, 3, 4, 4, 2, 1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:
1. take the first element of b, so b = [2, 3, 4, 4, 2, 1], c = [1];
2. take the last element of b, so b = [2, 3, 4, 4, 2], c = [1, 1];
3. take the last element of b, so b = [2, 3, 4, 4], c = [1, 1, 2];
4. take the first element of b, so b = [3, 4, 4], c = [1, 1, 2, 2];
5. take the first element of b, so b = [4, 4], c = [1, 1, 2, 2, 3];
6. take the last element of b, so b = [4], c = [1, 1, 2, 2, 3, 4];
7. take the only element of b, so b = [], c = [1, 1, 2, 2, 3, 4, 4] β c is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from a to make it a good array.
Example
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
Output
0
4
0
2
3
Note
In the first test case of the example, the array a is already good, so we don't need to erase any prefix.
In the second test case of the example, the initial array a is not good. Let's erase first 4 elements of a, the result is [4, 5, 2]. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good. | instruction | 0 | 69,122 | 12 | 138,244 |
Tags: greedy
Correct Solution:
```
def answer(n,A):
count=1
flag=0
for i in range(n-1,0,-1):
if A[i]-A[i-1]<=0 and flag==0:
count+=1
elif A[i]-A[i-1]>=0:
flag=1
count+=1
else:
break
return n-count
t=int(input())
for i in range(t):
n=int(input())
arr=list(map(int,input().split()))
print(answer(n,arr))
``` | output | 1 | 69,122 | 12 | 138,245 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a_1, a_2, ..., a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a_1, a_2, ..., a_k] (0 β€ k β€ n).
The array b of length m is called good, if you can obtain a non-decreasing array c (c_1 β€ c_2 β€ ... β€ c_{m}) from it, repeating the following operation m times (initially, c is empty):
* select either the first or the last element of b, remove it from b, and append it to the end of the array c.
For example, if we do 4 operations: take b_1, then b_{m}, then b_{m-1} and at last b_2, then b becomes [b_3, b_4, ..., b_{m-3}] and c =[b_1, b_{m}, b_{m-1}, b_2].
Consider the following example: b = [1, 2, 3, 4, 4, 2, 1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:
1. take the first element of b, so b = [2, 3, 4, 4, 2, 1], c = [1];
2. take the last element of b, so b = [2, 3, 4, 4, 2], c = [1, 1];
3. take the last element of b, so b = [2, 3, 4, 4], c = [1, 1, 2];
4. take the first element of b, so b = [3, 4, 4], c = [1, 1, 2, 2];
5. take the first element of b, so b = [4, 4], c = [1, 1, 2, 2, 3];
6. take the last element of b, so b = [4], c = [1, 1, 2, 2, 3, 4];
7. take the only element of b, so b = [], c = [1, 1, 2, 2, 3, 4, 4] β c is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from a to make it a good array.
Example
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
Output
0
4
0
2
3
Note
In the first test case of the example, the array a is already good, so we don't need to erase any prefix.
In the second test case of the example, the initial array a is not good. Let's erase first 4 elements of a, the result is [4, 5, 2]. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good. | instruction | 0 | 69,123 | 12 | 138,246 |
Tags: greedy
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
a = [int(i) for i in input().split()]
ind = 0
bad_elements = 0
state = 0
border = 0
seq = 0
while ind < n:
if state == 0:
# print('state =', state, 'ind =', ind, 'a[ind] =', a[ind])
if ind == 0:
ind += 1
continue
if a[ind - 1] <= a[ind]:
ind += 1
else:
ind -= 1
state = 1
# border = ind
# ind += 1
if state == 1:
# print('state =', state, 'ind =', ind, 'a[ind] =', a[ind])
if ind == n - 1:
break
if a[ind] > a[ind + 1]:
ind += 1
seq = 0
elif a[ind] == a[ind + 1]:
ind += 1
seq += 1
else:
state = 0
bad_elements = ind - seq
ind += 1
print(bad_elements)
``` | output | 1 | 69,123 | 12 | 138,247 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a_1, a_2, ..., a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a_1, a_2, ..., a_k] (0 β€ k β€ n).
The array b of length m is called good, if you can obtain a non-decreasing array c (c_1 β€ c_2 β€ ... β€ c_{m}) from it, repeating the following operation m times (initially, c is empty):
* select either the first or the last element of b, remove it from b, and append it to the end of the array c.
For example, if we do 4 operations: take b_1, then b_{m}, then b_{m-1} and at last b_2, then b becomes [b_3, b_4, ..., b_{m-3}] and c =[b_1, b_{m}, b_{m-1}, b_2].
Consider the following example: b = [1, 2, 3, 4, 4, 2, 1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:
1. take the first element of b, so b = [2, 3, 4, 4, 2, 1], c = [1];
2. take the last element of b, so b = [2, 3, 4, 4, 2], c = [1, 1];
3. take the last element of b, so b = [2, 3, 4, 4], c = [1, 1, 2];
4. take the first element of b, so b = [3, 4, 4], c = [1, 1, 2, 2];
5. take the first element of b, so b = [4, 4], c = [1, 1, 2, 2, 3];
6. take the last element of b, so b = [4], c = [1, 1, 2, 2, 3, 4];
7. take the only element of b, so b = [], c = [1, 1, 2, 2, 3, 4, 4] β c is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from a to make it a good array.
Example
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
Output
0
4
0
2
3
Note
In the first test case of the example, the array a is already good, so we don't need to erase any prefix.
In the second test case of the example, the initial array a is not good. Let's erase first 4 elements of a, the result is [4, 5, 2]. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good. | instruction | 0 | 69,124 | 12 | 138,248 |
Tags: greedy
Correct Solution:
```
# @author --> ajaymodi
# Naive approach
import sys
# sys.stdin=open("input.in","r")
# sys.stdout=open("output.out","w")
input=lambda : sys.stdin.readline().strip()
char = [chr(i) for i in range(97,123)]
CHAR = [chr(i) for i in range(65,91)]
mp = lambda:list(map(int,input().split()))
INT = lambda:int(input())
rn = lambda:range(INT())
from math import ceil,sqrt,factorial,gcd
for _ in rn():
n = INT()
l = mp()
l = l[::-1]
flag = 0
c = 1
for i in range(1,n):
if l[i]>l[i-1]:
c+=1
if flag==1:
c-=1
break
elif l[i]==l[i-1]:
c+=1
else:
if flag==0:
flag+=1
c+=1
# print(c,"----")
print(n-c)
``` | output | 1 | 69,124 | 12 | 138,249 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a_1, a_2, ..., a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a_1, a_2, ..., a_k] (0 β€ k β€ n).
The array b of length m is called good, if you can obtain a non-decreasing array c (c_1 β€ c_2 β€ ... β€ c_{m}) from it, repeating the following operation m times (initially, c is empty):
* select either the first or the last element of b, remove it from b, and append it to the end of the array c.
For example, if we do 4 operations: take b_1, then b_{m}, then b_{m-1} and at last b_2, then b becomes [b_3, b_4, ..., b_{m-3}] and c =[b_1, b_{m}, b_{m-1}, b_2].
Consider the following example: b = [1, 2, 3, 4, 4, 2, 1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:
1. take the first element of b, so b = [2, 3, 4, 4, 2, 1], c = [1];
2. take the last element of b, so b = [2, 3, 4, 4, 2], c = [1, 1];
3. take the last element of b, so b = [2, 3, 4, 4], c = [1, 1, 2];
4. take the first element of b, so b = [3, 4, 4], c = [1, 1, 2, 2];
5. take the first element of b, so b = [4, 4], c = [1, 1, 2, 2, 3];
6. take the last element of b, so b = [4], c = [1, 1, 2, 2, 3, 4];
7. take the only element of b, so b = [], c = [1, 1, 2, 2, 3, 4, 4] β c is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from a to make it a good array.
Example
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
Output
0
4
0
2
3
Note
In the first test case of the example, the array a is already good, so we don't need to erase any prefix.
In the second test case of the example, the initial array a is not good. Let's erase first 4 elements of a, the result is [4, 5, 2]. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good. | instruction | 0 | 69,125 | 12 | 138,250 |
Tags: greedy
Correct Solution:
```
import math
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
pos = n - 1
while pos > 0 and a[pos - 1] >= a[pos]:
pos -= 1
while pos > 0 and a[pos - 1] <= a[pos]:
pos -= 1
print(pos)
``` | output | 1 | 69,125 | 12 | 138,251 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a_1, a_2, ..., a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a_1, a_2, ..., a_k] (0 β€ k β€ n).
The array b of length m is called good, if you can obtain a non-decreasing array c (c_1 β€ c_2 β€ ... β€ c_{m}) from it, repeating the following operation m times (initially, c is empty):
* select either the first or the last element of b, remove it from b, and append it to the end of the array c.
For example, if we do 4 operations: take b_1, then b_{m}, then b_{m-1} and at last b_2, then b becomes [b_3, b_4, ..., b_{m-3}] and c =[b_1, b_{m}, b_{m-1}, b_2].
Consider the following example: b = [1, 2, 3, 4, 4, 2, 1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:
1. take the first element of b, so b = [2, 3, 4, 4, 2, 1], c = [1];
2. take the last element of b, so b = [2, 3, 4, 4, 2], c = [1, 1];
3. take the last element of b, so b = [2, 3, 4, 4], c = [1, 1, 2];
4. take the first element of b, so b = [3, 4, 4], c = [1, 1, 2, 2];
5. take the first element of b, so b = [4, 4], c = [1, 1, 2, 2, 3];
6. take the last element of b, so b = [4], c = [1, 1, 2, 2, 3, 4];
7. take the only element of b, so b = [], c = [1, 1, 2, 2, 3, 4, 4] β c is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from a to make it a good array.
Example
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
Output
0
4
0
2
3
Note
In the first test case of the example, the array a is already good, so we don't need to erase any prefix.
In the second test case of the example, the initial array a is not good. Let's erase first 4 elements of a, the result is [4, 5, 2]. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good. | instruction | 0 | 69,126 | 12 | 138,252 |
Tags: greedy
Correct Solution:
```
for t in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
pos = n-1
while pos>0 and a[pos]<=a[pos-1]:pos-=1
while pos>0 and a[pos]>=a[pos-1]:pos-=1
print(pos)
``` | output | 1 | 69,126 | 12 | 138,253 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a_1, a_2, ..., a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a_1, a_2, ..., a_k] (0 β€ k β€ n).
The array b of length m is called good, if you can obtain a non-decreasing array c (c_1 β€ c_2 β€ ... β€ c_{m}) from it, repeating the following operation m times (initially, c is empty):
* select either the first or the last element of b, remove it from b, and append it to the end of the array c.
For example, if we do 4 operations: take b_1, then b_{m}, then b_{m-1} and at last b_2, then b becomes [b_3, b_4, ..., b_{m-3}] and c =[b_1, b_{m}, b_{m-1}, b_2].
Consider the following example: b = [1, 2, 3, 4, 4, 2, 1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:
1. take the first element of b, so b = [2, 3, 4, 4, 2, 1], c = [1];
2. take the last element of b, so b = [2, 3, 4, 4, 2], c = [1, 1];
3. take the last element of b, so b = [2, 3, 4, 4], c = [1, 1, 2];
4. take the first element of b, so b = [3, 4, 4], c = [1, 1, 2, 2];
5. take the first element of b, so b = [4, 4], c = [1, 1, 2, 2, 3];
6. take the last element of b, so b = [4], c = [1, 1, 2, 2, 3, 4];
7. take the only element of b, so b = [], c = [1, 1, 2, 2, 3, 4, 4] β c is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from a to make it a good array.
Example
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
Output
0
4
0
2
3
Note
In the first test case of the example, the array a is already good, so we don't need to erase any prefix.
In the second test case of the example, the initial array a is not good. Let's erase first 4 elements of a, the result is [4, 5, 2]. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good. | instruction | 0 | 69,127 | 12 | 138,254 |
Tags: greedy
Correct Solution:
```
t = int(input())
def solve(arr):
n = len(arr)
x = sorted(arr)
if arr == x or arr == x[::-1] or n == 1:
return 0
arr = arr[::-1]
i = 0
while i < n-1 and arr[i+1]>=arr[i]:
i += 1
ans1 = i
i = 0
while i < n-1 and arr[i+1]<=arr[i]:
i += 1
ans2 = i
i = 0
ascend = True
descend = False
while i < n-1:
if ascend and arr[i+1] >= arr[i]:
i += 1
else:
descend = True
ascend = False
if descend and arr[i+1]<=arr[i]:
i += 1
else:
descend = False
if not ascend and not descend:
break
ans3 = i
# print(ans1, ans2, ans3)
ans = max(ans1, max(ans2, ans3)) + 1
return n - ans
while t > 0:
n = int(input())
arr = list(map(int, input().split()))
# x, y, z = map(int, input().split())
print(solve(arr))
t -= 1
``` | output | 1 | 69,127 | 12 | 138,255 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a_1, a_2, ..., a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a_1, a_2, ..., a_k] (0 β€ k β€ n).
The array b of length m is called good, if you can obtain a non-decreasing array c (c_1 β€ c_2 β€ ... β€ c_{m}) from it, repeating the following operation m times (initially, c is empty):
* select either the first or the last element of b, remove it from b, and append it to the end of the array c.
For example, if we do 4 operations: take b_1, then b_{m}, then b_{m-1} and at last b_2, then b becomes [b_3, b_4, ..., b_{m-3}] and c =[b_1, b_{m}, b_{m-1}, b_2].
Consider the following example: b = [1, 2, 3, 4, 4, 2, 1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:
1. take the first element of b, so b = [2, 3, 4, 4, 2, 1], c = [1];
2. take the last element of b, so b = [2, 3, 4, 4, 2], c = [1, 1];
3. take the last element of b, so b = [2, 3, 4, 4], c = [1, 1, 2];
4. take the first element of b, so b = [3, 4, 4], c = [1, 1, 2, 2];
5. take the first element of b, so b = [4, 4], c = [1, 1, 2, 2, 3];
6. take the last element of b, so b = [4], c = [1, 1, 2, 2, 3, 4];
7. take the only element of b, so b = [], c = [1, 1, 2, 2, 3, 4, 4] β c is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from a to make it a good array.
Example
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
Output
0
4
0
2
3
Note
In the first test case of the example, the array a is already good, so we don't need to erase any prefix.
In the second test case of the example, the initial array a is not good. Let's erase first 4 elements of a, the result is [4, 5, 2]. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good.
Submitted Solution:
```
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
pos,neg,c=0,0,0
for i in range(n-1,0,-1):
if a[i-1]-a[i]>0 and neg!=0:
c=i
break
if a[i-1]-a[i]>0:
pos+=1
if a[i-1]-a[i]<0:
neg+=1
print(n-(n-c))
``` | instruction | 0 | 69,128 | 12 | 138,256 |
Yes | output | 1 | 69,128 | 12 | 138,257 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a_1, a_2, ..., a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a_1, a_2, ..., a_k] (0 β€ k β€ n).
The array b of length m is called good, if you can obtain a non-decreasing array c (c_1 β€ c_2 β€ ... β€ c_{m}) from it, repeating the following operation m times (initially, c is empty):
* select either the first or the last element of b, remove it from b, and append it to the end of the array c.
For example, if we do 4 operations: take b_1, then b_{m}, then b_{m-1} and at last b_2, then b becomes [b_3, b_4, ..., b_{m-3}] and c =[b_1, b_{m}, b_{m-1}, b_2].
Consider the following example: b = [1, 2, 3, 4, 4, 2, 1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:
1. take the first element of b, so b = [2, 3, 4, 4, 2, 1], c = [1];
2. take the last element of b, so b = [2, 3, 4, 4, 2], c = [1, 1];
3. take the last element of b, so b = [2, 3, 4, 4], c = [1, 1, 2];
4. take the first element of b, so b = [3, 4, 4], c = [1, 1, 2, 2];
5. take the first element of b, so b = [4, 4], c = [1, 1, 2, 2, 3];
6. take the last element of b, so b = [4], c = [1, 1, 2, 2, 3, 4];
7. take the only element of b, so b = [], c = [1, 1, 2, 2, 3, 4, 4] β c is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from a to make it a good array.
Example
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
Output
0
4
0
2
3
Note
In the first test case of the example, the array a is already good, so we don't need to erase any prefix.
In the second test case of the example, the initial array a is not good. Let's erase first 4 elements of a, the result is [4, 5, 2]. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good.
Submitted Solution:
```
import sys
import math
import heapq
import collections
def inputnum():
return(int(input()))
def inputnums():
return(map(int,input().split()))
def inputlist():
return(list(map(int,input().split())))
def inputstring():
return([x for x in input()])
def inputmatrixchar(rows):
arr2d = [[j for j in input().strip()] for i in range(rows)]
return arr2d
def inputmatrixint(rows):
arr2d = []
for _ in range(rows):
arr2d.append([int(i) for i in input().split()])
return arr2d
t=int(input())
for q in range(t):
n = inputnum()
a = inputlist()
inc = True
ind = 0
for i in reversed(range(n-1)):
if inc and a[i] < a[i+1]:
inc = False
continue
if not inc and a[i] > a[i+1]:
ind = i+1
break
print(ind)
``` | instruction | 0 | 69,129 | 12 | 138,258 |
Yes | output | 1 | 69,129 | 12 | 138,259 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a_1, a_2, ..., a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a_1, a_2, ..., a_k] (0 β€ k β€ n).
The array b of length m is called good, if you can obtain a non-decreasing array c (c_1 β€ c_2 β€ ... β€ c_{m}) from it, repeating the following operation m times (initially, c is empty):
* select either the first or the last element of b, remove it from b, and append it to the end of the array c.
For example, if we do 4 operations: take b_1, then b_{m}, then b_{m-1} and at last b_2, then b becomes [b_3, b_4, ..., b_{m-3}] and c =[b_1, b_{m}, b_{m-1}, b_2].
Consider the following example: b = [1, 2, 3, 4, 4, 2, 1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:
1. take the first element of b, so b = [2, 3, 4, 4, 2, 1], c = [1];
2. take the last element of b, so b = [2, 3, 4, 4, 2], c = [1, 1];
3. take the last element of b, so b = [2, 3, 4, 4], c = [1, 1, 2];
4. take the first element of b, so b = [3, 4, 4], c = [1, 1, 2, 2];
5. take the first element of b, so b = [4, 4], c = [1, 1, 2, 2, 3];
6. take the last element of b, so b = [4], c = [1, 1, 2, 2, 3, 4];
7. take the only element of b, so b = [], c = [1, 1, 2, 2, 3, 4, 4] β c is non-decreasing.
Note that the array consisting of one element is good.
Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from a to make it a good array.
Example
Input
5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3
Output
0
4
0
2
3
Note
In the first test case of the example, the array a is already good, so we don't need to erase any prefix.
In the second test case of the example, the initial array a is not good. Let's erase first 4 elements of a, the result is [4, 5, 2]. The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good.
Submitted Solution:
```
t=int(input())
for q in range(t):
n=int(input())
l=list(map(int,input().split()))
i=n-1
while l[i]<=l[i-1] and i>0:
i-=1
while l[i]>=l[i-1] and i>0:
i-=1
print(i)
``` | instruction | 0 | 69,130 | 12 | 138,260 |
Yes | output | 1 | 69,130 | 12 | 138,261 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.